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
- .set_internal_service_key(key) ⇒ Object
Instance Method Summary collapse
- #api_key ⇒ Object
- #bearer_token ⇒ Object
- #delete(path, body: {}) ⇒ Object
- #get(path, params: {}) ⇒ Object
-
#initialize(env) ⇒ Client
constructor
Construct a new Lockstep API client targeting the specified server.
- #internal_service_key ⇒ Object
- #internal_service_key_set? ⇒ Boolean
- #patch(path, body: {}, params: {}) ⇒ Object
- #post(path, body: {}, params: {}) ⇒ Object
- #post_magic_link(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.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/concepts/lockstep/client.rb', line 44 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 |
.set_internal_service_key(key) ⇒ Object
32 33 34 |
# File 'app/concepts/lockstep/client.rb', line 32 def self.set_internal_service_key(key) RequestStore.store[:internal_service_key] = key end |
Instance Method Details
#api_key ⇒ Object
80 81 82 |
# File 'app/concepts/lockstep/client.rb', line 80 def api_key self.class.api_key end |
#bearer_token ⇒ Object
76 77 78 |
# File 'app/concepts/lockstep/client.rb', line 76 def bearer_token self.class.bearer_token end |
#delete(path, body: {}) ⇒ Object
162 163 164 |
# File 'app/concepts/lockstep/client.rb', line 162 def delete(path, body: {}) request(:delete, path, body, {}) end |
#get(path, params: {}) ⇒ Object
146 147 148 |
# File 'app/concepts/lockstep/client.rb', line 146 def get(path, params: {}) request(:get, path, nil, params) end |
#internal_service_key ⇒ Object
36 37 38 |
# File 'app/concepts/lockstep/client.rb', line 36 def internal_service_key RequestStore.store[:internal_service_key] end |
#internal_service_key_set? ⇒ Boolean
170 171 172 |
# File 'app/concepts/lockstep/client.rb', line 170 def internal_service_key_set? !!internal_service_key end |
#patch(path, body: {}, params: {}) ⇒ Object
154 155 156 |
# File 'app/concepts/lockstep/client.rb', line 154 def patch(path, body: {}, params: {}) request(:patch, path, body, params) end |
#post(path, body: {}, params: {}) ⇒ Object
150 151 152 |
# File 'app/concepts/lockstep/client.rb', line 150 def post(path, body: {}, params: {}) request(:post, path, body, params) end |
#post_magic_link(path, body: {}, params: {}) ⇒ Object
166 167 168 |
# File 'app/concepts/lockstep/client.rb', line 166 def post_magic_link(path, body: {}, params: {}) request(:post, path, body, params) end |
#put(path, body: {}, params: {}) ⇒ Object
158 159 160 |
# File 'app/concepts/lockstep/client.rb', line 158 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
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 137 138 139 140 141 142 143 144 |
# File 'app/concepts/lockstep/client.rb', line 96 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 request["LS-InternalService"] = internal_service_key if internal_service_key_set? 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
60 61 62 63 64 |
# File 'app/concepts/lockstep/client.rb', line 60 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
88 89 90 |
# File 'app/concepts/lockstep/client.rb', line 88 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
70 71 72 73 74 |
# File 'app/concepts/lockstep/client.rb', line 70 def with_bearer_token(token) self.class.set_bearer_token(token) self end |
#with_logger(&block) ⇒ Object
174 175 176 |
# File 'app/concepts/lockstep/client.rb', line 174 def with_logger(&block) block.call end |