Class: AxHub::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/axhub_sdk.rb,
lib/axhub_sdk/operations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **kwargs) ⇒ Client

Returns a new instance of Client.



254
255
256
# File 'lib/axhub_sdk.rb', line 254

def initialize(base_url: DEFAULT_BASE_URL, token: nil, token_type: nil, default_tenant_id: nil, default_tenant_slug: nil, timeout_seconds: 10)
  @base_url = base_url.sub(%r{/$}, ''); @token = token; @token_type = token_type&.to_sym; @default_tenant_id = default_tenant_id; @default_tenant_slug = default_tenant_slug; @timeout_seconds = timeout_seconds; @apps = AppsClient.new(self)
end

Instance Attribute Details

#appsObject (readonly)

Returns the value of attribute apps.



253
254
255
# File 'lib/axhub_sdk.rb', line 253

def apps
  @apps
end

#auditObject (readonly)

Returns the value of attribute audit.



19
20
21
# File 'lib/axhub_sdk/operations.rb', line 19

def audit
  @audit
end

#authzObject (readonly)

Returns the value of attribute authz.



19
20
21
# File 'lib/axhub_sdk/operations.rb', line 19

def authz
  @authz
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



253
254
255
# File 'lib/axhub_sdk.rb', line 253

def base_url
  @base_url
end

#dataObject (readonly)

Returns the value of attribute data.



19
20
21
# File 'lib/axhub_sdk/operations.rb', line 19

def data
  @data
end

#deploymentsObject (readonly)

Returns the value of attribute deployments.



19
20
21
# File 'lib/axhub_sdk/operations.rb', line 19

def deployments
  @deployments
end

#gatewayObject (readonly)

Returns the value of attribute gateway.



19
20
21
# File 'lib/axhub_sdk/operations.rb', line 19

def gateway
  @gateway
end

#identityObject (readonly)

Returns the value of attribute identity.



19
20
21
# File 'lib/axhub_sdk/operations.rb', line 19

def identity
  @identity
end

#tenantsObject (readonly)

Returns the value of attribute tenants.



19
20
21
# File 'lib/axhub_sdk/operations.rb', line 19

def tenants
  @tenants
end

Instance Method Details

#__axhub_original_initializeClient

Returns a new instance of Client.

Returns:

  • (Client)

    a new instance of Client



20
21
22
# File 'lib/axhub_sdk/operations.rb', line 20

def initialize(base_url: DEFAULT_BASE_URL, token: nil, token_type: nil, default_tenant_id: nil, default_tenant_slug: nil, timeout_seconds: 10)
  @base_url = base_url.sub(%r{/$}, ''); @token = token; @token_type = token_type&.to_sym; @default_tenant_id = default_tenant_id; @default_tenant_slug = default_tenant_slug; @timeout_seconds = timeout_seconds; @apps = AppsClient.new(self)
end

#redacted_tokenObject



257
258
259
# File 'lib/axhub_sdk.rb', line 257

def redacted_token
  @token.nil? || @token.empty? ? '' : '***REDACTED***'
end

#request(operation_id, path_params: {}, query: {}, body: nil) ⇒ Object

Raises:



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/axhub_sdk.rb', line 260

def request(operation_id, path_params: {}, query: {}, body: nil)
  route = ROUTE_BY_OP.fetch(operation_id); path = route['path'].dup; path_params.each { |k, v| path.gsub!("{#{k}}", URI.encode_www_form_component(v.to_s)) }; raise Error.new(category: 'validation', code: 'required', message: 'missing path parameter') if path.include?(123.chr) || path.include?(125.chr)
  uri = URI(@base_url + path); uri.query = URI.encode_www_form(query) unless query.empty?
  req_class = Net::HTTP.const_get(route['method'].capitalize); req = req_class.new(uri); req['X-Request-ID'] = request_id
  if body then req['Content-Type'] = 'application/json'; req.body = JSON.generate(body) end
  if @token
    case @token_type
    when :pat then req['X-Api-Key'] = @token
    when :jwt then req['Authorization'] = "Bearer #{@token}"
    else raise Error.new(category: 'validation', code: 'required', message: 'tokenType must be explicit')
    end
  end
  begin
    res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https', open_timeout: @timeout_seconds, read_timeout: @timeout_seconds) { |http| http.request(req) }
  rescue Timeout::Error, IOError, SocketError, SystemCallError => e
    raise Error.new(category: 'network', code: 'network_error', message: e.message, retryable: true)
  end
  parsed = if res.body && !res.body.empty?
             begin
               JSON.parse(res.body)
             rescue JSON::ParserError
               { 'raw' => res.body }
             end
           else
             {}
           end
  if res.code.to_i >= 400
    err = parsed.is_a?(Hash) ? (parsed['error'] || parsed) : {}
    err = {} unless err.is_a?(Hash)
    info = ERROR_CODES[err['code']]
    retryable = err.key?('retryable') ? !!err['retryable'] : !!info&.retryable
    raise Error.new(category: err['category'] || info&.category || 'unknown', code: err['code'] || "http_#{res.code}", message: err['message'], status: res.code.to_i, retryable: retryable, request_id: err['request_id'] || err['requestId'])
  end
  AxHub.camelize(parsed)
end