Class: Lockstep::Client

Inherits:
Object
  • Object
show all
Defined in:
app/concepts/lockstep/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Client

Construct a new Lockstep API client targeting the specified server.

Parameters:

  • env (string)

    Either “sbx”, “prd”, or the URI of the server, ending in a slash (/)



58
59
60
61
62
63
64
65
66
67
68
# File 'app/concepts/lockstep/client.rb', line 58

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_keyObject



28
29
30
# File 'app/concepts/lockstep/client.rb', line 28

def self.api_key
  RequestStore.store[:lockstep_api_key]
end

.bearer_tokenObject



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

.set_m2m_token(token) ⇒ Object



49
50
51
52
# File 'app/concepts/lockstep/client.rb', line 49

def self.set_m2m_token(token)
  # this method can be ommitted and we can use directly `set_bearer_token`
  set_bearer_token(token)
end

.set_x_group_key(x_group_key) ⇒ Object



40
41
42
43
# File 'app/concepts/lockstep/client.rb', line 40

def self.set_x_group_key(x_group_key)
  RequestStore.store[:lockstep_x_group_key] = x_group_key
  true
end

.x_group_keyObject



45
46
47
# File 'app/concepts/lockstep/client.rb', line 45

def self.x_group_key
  RequestStore.store[:lockstep_x_group_key]
end

Instance Method Details

#api_keyObject



94
95
96
# File 'app/concepts/lockstep/client.rb', line 94

def api_key
  self.class.api_key
end

#bearer_tokenObject



90
91
92
# File 'app/concepts/lockstep/client.rb', line 90

def bearer_token
  self.class.bearer_token
end

#delete(path, body: {}) ⇒ Object



184
185
186
# File 'app/concepts/lockstep/client.rb', line 184

def delete(path, body: {})
  request(:delete, path, body, {})
end

#get(path, params: {}) ⇒ Object



168
169
170
# File 'app/concepts/lockstep/client.rb', line 168

def get(path, params: {})
  request(:get, path, nil, params)
end

#internal_service_keyObject



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

Returns:

  • (Boolean)


192
193
194
# File 'app/concepts/lockstep/client.rb', line 192

def internal_service_key_set?
  !!internal_service_key
end

#patch(path, body: {}, params: {}) ⇒ Object



176
177
178
# File 'app/concepts/lockstep/client.rb', line 176

def patch(path, body: {}, params: {})
  request(:patch, path, body, params)
end

#post(path, body: {}, params: {}) ⇒ Object



172
173
174
# File 'app/concepts/lockstep/client.rb', line 172

def post(path, body: {}, params: {})
  request(:post, path, body, params)
end

#post_magic_link(path, body: {}, params: {}) ⇒ Object



188
189
190
# File 'app/concepts/lockstep/client.rb', line 188

def post_magic_link(path, body: {}, params: {})
  request(:post, path, body, params)
end

#put(path, body: {}, params: {}) ⇒ Object



180
181
182
# File 'app/concepts/lockstep/client.rb', line 180

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



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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/concepts/lockstep/client.rb', line 114

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

  if x_group_key != nil
    request["X-Group-Key"] = x_group_key
  end

  # Send the request
  http.request(request)
end

#with_api_key(api_key) ⇒ Object

Configure this API client to use API key authentication

Parameters:



74
75
76
77
78
# File 'app/concepts/lockstep/client.rb', line 74

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

Parameters:

  • app_name (string)

    The name of the application



106
107
108
# File 'app/concepts/lockstep/client.rb', line 106

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

Parameters:



84
85
86
87
88
# File 'app/concepts/lockstep/client.rb', line 84

def with_bearer_token(token)
  self.class.set_bearer_token(token)

  self
end

#with_logger(&block) ⇒ Object



196
197
198
# File 'app/concepts/lockstep/client.rb', line 196

def with_logger(&block)
  block.call
end

#x_group_keyObject



98
99
100
# File 'app/concepts/lockstep/client.rb', line 98

def x_group_key
  self.class.x_group_key
end