Class: MastercardCoreSdk::Client::ApiClient

Inherits:
Object
  • Object
show all
Includes:
MastercardCoreSdk::Converters, MastercardCoreSdk::Core, Interceptors, Tracker, Util
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.



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

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.



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

def api_config
  @api_config
end

#api_trackerHash

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

Returns:

  • (Hash)


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

def api_tracker
  @api_tracker
end

#configObject

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



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

def config
  @config
end

#default_headersHash

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

Returns:

  • (Hash)


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

def default_headers
  @default_headers
end

#error_handlerObject

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



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

def error_handler
  @error_handler
end

#mastercard_authenticatorObject

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



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

def mastercard_authenticator
  @mastercard_authenticator
end

Class Method Details

.defaultObject



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

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



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

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

  return response_body
end