Class: MaxApiClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/max_api_client/client.rb,
sig/max_api_client.rbs

Overview

HTTP transport client responsible for authenticated API requests.

Constant Summary collapse

DEFAULT_BASE_URL =

Returns:

  • (String)
"https://platform-api2.max.ru"
DEFAULT_CA_FILE =

Returns:

  • (String)
File.expand_path("../../certs/russian_trusted_root_ca.pem", __dir__)
REQUEST_CLASSES =
{
  get: Net::HTTP::Get,
  post: Net::HTTP::Post,
  put: Net::HTTP::Put,
  patch: Net::HTTP::Patch,
  delete: Net::HTTP::Delete
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:, base_url: DEFAULT_BASE_URL, adapter: nil, open_timeout: nil, read_timeout: nil, ca_file: DEFAULT_CA_FILE, verify_ssl: true, logger: nil) ⇒ Client

rubocop:disable Metrics/ParameterLists

Parameters:

  • token: (String)
  • base_url: (String) (defaults to: DEFAULT_BASE_URL)
  • adapter: (Object) (defaults to: nil)
  • open_timeout: (Numeric, nil) (defaults to: nil)
  • read_timeout: (Numeric, nil) (defaults to: nil)
  • ca_file: (String, nil) (defaults to: DEFAULT_CA_FILE)
  • verify_ssl: (Boolean) (defaults to: true)
  • logger: (Object) (defaults to: nil)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/max_api_client/client.rb', line 19

def initialize(token:, base_url: DEFAULT_BASE_URL, adapter: nil, open_timeout: nil, read_timeout: nil,
               ca_file: DEFAULT_CA_FILE, verify_ssl: true, logger: nil)
  @token = token
  @base_url = base_url
  @adapter = adapter
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @ca_file = ca_file
  @verify_ssl = verify_ssl
  @logger = logger || MaxApiClient.logger
end

Instance Attribute Details

#base_urlString (readonly)

Returns the value of attribute base_url.

Returns:

  • (String)


16
17
18
# File 'lib/max_api_client/client.rb', line 16

def base_url
  @base_url
end

#ca_fileString? (readonly)

Returns the value of attribute ca_file.

Returns:

  • (String, nil)


16
17
18
# File 'lib/max_api_client/client.rb', line 16

def ca_file
  @ca_file
end

#tokenString (readonly)

Returns the value of attribute token.

Returns:

  • (String)


16
17
18
# File 'lib/max_api_client/client.rb', line 16

def token
  @token
end

#verify_sslBoolean (readonly)

Returns the value of attribute verify_ssl.

Returns:

  • (Boolean)


16
17
18
# File 'lib/max_api_client/client.rb', line 16

def verify_ssl
  @verify_ssl
end

Instance Method Details

#call(method:, path: nil, query: nil, body: nil, path_params: nil, headers: nil, url: nil, raw_body: nil, parse_json: true, open_timeout: nil, read_timeout: nil) ⇒ Hash[Symbol, untyped]

rubocop:disable Metrics/ParameterLists

Parameters:

  • method: (Symbol, String)
  • path: (String, nil) (defaults to: nil)
  • query: (Object) (defaults to: nil)
  • body: (Object) (defaults to: nil)
  • path_params: (Object) (defaults to: nil)
  • headers: (Object) (defaults to: nil)
  • url: (String, nil) (defaults to: nil)
  • raw_body: (Object) (defaults to: nil)
  • parse_json: (Boolean) (defaults to: true)

Returns:

  • (Hash[Symbol, untyped])


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/max_api_client/client.rb', line 33

def call(method:, path: nil, query: nil, body: nil, path_params: nil, headers: nil, url: nil, raw_body: nil,
         parse_json: true, open_timeout: nil, read_timeout: nil)
  request = build_request(
    method:,
    path:,
    query:,
    body:,
    path_params:,
    headers:,
    url:,
    raw_body:,
    parse_json:,
    open_timeout:,
    read_timeout:
  )

  log_debug("max_api_client.request", loggable_request(request))

  response = @adapter ? @adapter.call(request) : perform_request(request)

  log_debug("max_api_client.response", loggable_response(response))
  response
end