Class: MastercardCoreSdk::Client::ApiClient

Inherits:
Object
  • Object
show all
Includes:
MastercardCoreSdk::Converters, MastercardCoreSdk::Core, Interceptors, Tracker
Defined in:
lib/mastercard_core_sdk/client/api_client.rb

Overview

ApiClient is the base class to invoke the API. It is responsible for to convert all request and response according to the content type.

Constant Summary collapse

@@logger =
Logging.logger[self]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_config = nil) ⇒ ApiClient

Initializing Api Client with user-agent, configuration and default headers. Validating MasterCardApiConfiguration and calling interceptor for Authorization signature and logger.



47
48
49
50
51
52
53
54
55
# File 'lib/mastercard_core_sdk/client/api_client.rb', line 47

def initialize(api_config = nil)
  @config = Configuration.default
  @user_agent = @config.user_agent
  @default_headers = @config.default_headers
  
  @mastercard_authenticator = MastercardAuthenticator.new
  @api_config = api_config || MasterCardApiConfiguration.api_config
  MasterCardApiConfiguration.validate_config(@api_config)
end

Instance Attribute Details

#api_configObject

The ApiConfig object holding environment configurations.



27
28
29
# File 'lib/mastercard_core_sdk/client/api_client.rb', line 27

def api_config
  @api_config
end

#api_trackerHash

Defines the api_tracker to be added in request header having parameters.

Returns:

  • (Hash)


37
38
39
# File 'lib/mastercard_core_sdk/client/api_client.rb', line 37

def api_tracker
  @api_tracker
end

#configObject

The Configuration object holding settings to be used in the API client.



24
25
26
# File 'lib/mastercard_core_sdk/client/api_client.rb', line 24

def config
  @config
end

#default_headersHash

Defines the headers to be used in HTTP requests of all API calls by default.

Returns:

  • (Hash)


32
33
34
# File 'lib/mastercard_core_sdk/client/api_client.rb', line 32

def default_headers
  @default_headers
end

#error_handlerObject

Defines the object that would handle error for all the requests.



40
41
42
# File 'lib/mastercard_core_sdk/client/api_client.rb', line 40

def error_handler
  @error_handler
end

#mastercard_authenticatorObject

Adds the authorization header, tracking headers and also logs the request/response.



43
44
45
# File 'lib/mastercard_core_sdk/client/api_client.rb', line 43

def mastercard_authenticator
  @mastercard_authenticator
end

Class Method Details

.defaultObject



57
58
59
# File 'lib/mastercard_core_sdk/client/api_client.rb', line 57

def self.default
  @@default ||= ApiClient.new
end

Instance Method Details

#call(path, service_request, http_method, return_type) ⇒ String

Execute Typhoeus request.

Parameters:

  • path

    URL for request

  • service_request

    Service Request object

  • http_method

    Specifies HTTP method

  • return_type

    Specifies response return type model

Returns:

  • (String)

    Response body



67
68
69
70
71
72
# File 'lib/mastercard_core_sdk/client/api_client.rb', line 67

def call(path, service_request, http_method, return_type)
  request = build_request(http_method, path, service_request, @api_config)
  response_body, response_code, response_headers = call_api(request, return_type)

  return response_body
end