Class: Lockstep::Client
- Inherits:
-
Object
- Object
- Lockstep::Client
- Defined in:
- app/concepts/lockstep/client.rb
Class Method Summary collapse
- .api_key ⇒ Object
- .bearer_token ⇒ Object
- .set_api_key(key) ⇒ Object
- .set_bearer_token(token) ⇒ Object
Instance Method Summary collapse
- #api_key ⇒ Object
- #bearer_token ⇒ Object
- #delete(path) ⇒ Object
- #get(path, params: {}) ⇒ Object
-
#initialize(env) ⇒ Client
constructor
Construct a new Lockstep API client targeting the specified server.
- #patch(path, body: {}, params: {}) ⇒ Object
- #post(path, body: {}, params: {}) ⇒ Object
- #put(path, body: {}, params: {}) ⇒ Object
-
#request(method, path, body, params) ⇒ Object
Send a request to the API and return the results.
-
#with_api_key(api_key) ⇒ Object
Configure this API client to use API key authentication.
-
#with_app_name(app_name) ⇒ Object
Configure this API to use an application name.
-
#with_bearer_token(token) ⇒ Object
Configure this API client to use JWT Bearer Token authentication.
- #with_logger(&block) ⇒ Object
Constructor Details
#initialize(env) ⇒ Client
Construct a new Lockstep API client targeting the specified server.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/concepts/lockstep/client.rb', line 37 def initialize(env) @version = "2022.4.32.0" @env = case env when "sbx" "https://api.sbx.lockstep.io/" when "prd" "https://api.lockstep.io/" else env end end |
Class Method Details
.api_key ⇒ Object
28 29 30 |
# File 'app/concepts/lockstep/client.rb', line 28 def self.api_key RequestStore.store[:lockstep_api_key] end |
.bearer_token ⇒ Object
17 18 19 |
# File 'app/concepts/lockstep/client.rb', line 17 def self.bearer_token RequestStore.store[:lockstep_bearer_token] end |
.set_api_key(key) ⇒ Object
21 22 23 24 25 26 |
# File 'app/concepts/lockstep/client.rb', line 21 def self.set_api_key(key) RequestStore.store[:lockstep_api_key] = key RequestStore.store[:lockstep_bearer_token] = nil true end |
.set_bearer_token(token) ⇒ Object
10 11 12 13 14 15 |
# File 'app/concepts/lockstep/client.rb', line 10 def self.set_bearer_token(token) RequestStore.store[:lockstep_bearer_token] = token RequestStore.store[:lockstep_api_key] = nil true end |
Instance Method Details
#api_key ⇒ Object
73 74 75 |
# File 'app/concepts/lockstep/client.rb', line 73 def api_key self.class.api_key end |
#bearer_token ⇒ Object
69 70 71 |
# File 'app/concepts/lockstep/client.rb', line 69 def bearer_token self.class.bearer_token end |
#delete(path) ⇒ Object
154 155 156 |
# File 'app/concepts/lockstep/client.rb', line 154 def delete(path) request(:delete, path, {}, {}) end |
#get(path, params: {}) ⇒ Object
138 139 140 |
# File 'app/concepts/lockstep/client.rb', line 138 def get(path, params: {}) request(:get, path, nil, params) end |
#patch(path, body: {}, params: {}) ⇒ Object
146 147 148 |
# File 'app/concepts/lockstep/client.rb', line 146 def patch(path, body: {}, params: {}) request(:patch, path, body, params) end |
#post(path, body: {}, params: {}) ⇒ Object
142 143 144 |
# File 'app/concepts/lockstep/client.rb', line 142 def post(path, body: {}, params: {}) request(:post, path, body, params) end |
#put(path, body: {}, params: {}) ⇒ Object
150 151 152 |
# File 'app/concepts/lockstep/client.rb', line 150 def put(path, body: {}, params: {}) request(:put, path, body, params) end |
#request(method, path, body, params) ⇒ Object
Send a request to the API and return the results
Sends a request to the
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/concepts/lockstep/client.rb', line 89 def request(method, path, body, params) url = URI(@env + path) if !params.nil? url.query = URI.encode_www_form(params) end http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = case method when :get Net::HTTP::Get.new(url) when :post Net::HTTP::Post.new(url) when :patch Net::HTTP::Patch.new(url) when :put Net::HTTP::Put.new(url) when :delete Net::HTTP::Delete.new(url) end # Set headers and body for request request["Accept"] = 'application/json' request["Content-Type"] = 'application/*+json' request["SdkType"] = 'Ruby' request["SdkVersion"] = '2022.4.32.0' request["MachineName"] = Socket.gethostname body = body.to_json unless body.is_a?(String) request.body = body # If there is an application name if @app_name != nil request["ApplicationName"] = @app_name end # Which authentication are we using? if api_key != nil request["Api-Key"] = api_key end if bearer_token != nil request["Authorization"] = 'Bearer ' + bearer_token end # Send the request http.request(request) end |
#with_api_key(api_key) ⇒ Object
Configure this API client to use API key authentication
53 54 55 56 57 |
# File 'app/concepts/lockstep/client.rb', line 53 def with_api_key(api_key) self.class.set_api_key(api_key) self end |
#with_app_name(app_name) ⇒ Object
Configure this API to use an application name
81 82 83 |
# File 'app/concepts/lockstep/client.rb', line 81 def with_app_name(app_name) @app_name = app_name end |
#with_bearer_token(token) ⇒ Object
Configure this API client to use JWT Bearer Token authentication
63 64 65 66 67 |
# File 'app/concepts/lockstep/client.rb', line 63 def with_bearer_token(token) self.class.set_bearer_token(token) self end |
#with_logger(&block) ⇒ Object
158 159 160 |
# File 'app/concepts/lockstep/client.rb', line 158 def with_logger(&block) block.call end |