Class: Airwallex::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/airwallex/client.rb

Constant Summary collapse

DEFAULT_HEADERS =
{
  "Content-Type" => "application/json",
  "Accept" => "application/json"
}.freeze
TOKEN_EXPIRY_BUFFER =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Client

Returns a new instance of Client.



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

def initialize(**options)
  config = Airwallex.configuration

  @client_id = options.fetch(:client_id, config.client_id)
  @api_key = options.fetch(:api_key, config.api_key)
  @login_as = options.key?(:login_as) ? options[:login_as] : config.
  @environment = Configuration.validate_environment!(options.fetch(:environment, config.environment))
  @timeout = options.fetch(:timeout, config.timeout)
  @open_timeout = options.fetch(:open_timeout, config.open_timeout)
  @logger = options.fetch(:logger, config.logger)
  @access_token = nil
  @token_expires_at = nil
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#client_idObject (readonly)

Returns the value of attribute client_id.



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

def client_id
  @client_id
end

#environmentObject (readonly)

Returns the value of attribute environment.



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

def environment
  @environment
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#login_asObject (readonly)

Returns the value of attribute login_as.



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

def 
  @login_as
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



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

def open_timeout
  @open_timeout
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#token_expires_atObject (readonly)

Returns the value of attribute token_expires_at.



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

def token_expires_at
  @token_expires_at
end

Instance Method Details

#auth_headersObject



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

def auth_headers
  { "Authorization" => "Bearer #{access_token}" }
end

#authenticateObject



49
50
51
# File 'lib/airwallex/client.rb', line 49

def authenticate
  authentication.
end

#authenticated?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/airwallex/client.rb', line 53

def authenticated?
  !access_token.nil? && !token_expired?
end

#authenticationObject



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

def authentication
  @authentication ||= Resources::Authentication.new(self)
end

#base_urlObject



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

def base_url
  Configuration::ENVIRONMENTS.fetch(environment)
end

#delete(path, params = {}, headers = {}, authenticated: true) ⇒ Object



77
78
79
# File 'lib/airwallex/client.rb', line 77

def delete(path, params = {}, headers = {}, authenticated: true)
  request(:delete, path, params: params, headers: headers, authenticated: authenticated)
end

#get(path, params = {}, headers = {}, authenticated: true) ⇒ Object



61
62
63
# File 'lib/airwallex/client.rb', line 61

def get(path, params = {}, headers = {}, authenticated: true)
  request(:get, path, params: params, headers: headers, authenticated: authenticated)
end

#patch(path, body = {}, headers = {}, authenticated: true, idempotency_key: nil) ⇒ Object



71
72
73
74
75
# File 'lib/airwallex/client.rb', line 71

def patch(path, body = {}, headers = {}, authenticated: true, idempotency_key: nil)
  validate_idempotency_key!(idempotency_key)
  request(:patch, path, body: body, headers: headers, authenticated: authenticated,
                        idempotency_key: idempotency_key)
end

#payment_intentsObject



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

def payment_intents
  @payment_intents ||= Resources::PaymentIntents.new(self)
end

#post(path, body = {}, headers = {}, authenticated: true, idempotency_key: nil) ⇒ Object



65
66
67
68
69
# File 'lib/airwallex/client.rb', line 65

def post(path, body = {}, headers = {}, authenticated: true, idempotency_key: nil)
  validate_idempotency_key!(idempotency_key)
  request(:post, path, body: body, headers: headers, authenticated: authenticated,
                       idempotency_key: idempotency_key)
end

#refundsObject



45
46
47
# File 'lib/airwallex/client.rb', line 45

def refunds
  @refunds ||= Resources::Refunds.new(self)
end

#store_token!(response) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/airwallex/client.rb', line 86

def store_token!(response)
  token = response["token"]
  raise AuthenticationError, "Authentication response missing token" if token.nil? || token.to_s.empty?

  @access_token = token
  @token_expires_at = parse_expires_at(response["expires_at"])
end

#validate_credentials!Object

Raises:



81
82
83
84
# File 'lib/airwallex/client.rb', line 81

def validate_credentials!
  raise ConfigurationError, "client_id is required" if client_id.nil? || client_id.to_s.empty?
  raise ConfigurationError, "api_key is required" if api_key.nil? || api_key.to_s.empty?
end