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.



274
275
276
# File 'lib/axhub_sdk.rb', line 274

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.



273
274
275
# File 'lib/axhub_sdk.rb', line 273

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.



273
274
275
# File 'lib/axhub_sdk.rb', line 273

def base_url
  @base_url
end

#costObject (readonly)

Returns the value of attribute cost.



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

def cost
  @cost
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



277
278
279
# File 'lib/axhub_sdk.rb', line 277

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

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

Raises:



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/axhub_sdk.rb', line 280

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
    if FORM_ENCODED_OPERATIONS.include?(operation_id)
      req['Content-Type'] = 'application/x-www-form-urlencoded'
      req.body = URI.encode_www_form(body.transform_keys(&:to_s).transform_values { |v| v.nil? ? '' : v.to_s })
    else
      req['Content-Type'] = 'application/json'
      req.body = JSON.generate(body)
    end
  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
  return { 'status' => res.code.to_i, 'location' => res['location'] } if res.code.to_i >= 300 && res.code.to_i < 400
  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