Class: Airwallex::Client
- Inherits:
-
Object
- Object
- Airwallex::Client
- 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
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#login_as ⇒ Object
readonly
Returns the value of attribute login_as.
-
#open_timeout ⇒ Object
readonly
Returns the value of attribute open_timeout.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#token_expires_at ⇒ Object
readonly
Returns the value of attribute token_expires_at.
Instance Method Summary collapse
- #auth_headers ⇒ Object
- #authenticate ⇒ Object
- #authenticated? ⇒ Boolean
- #authentication ⇒ Object
- #base_url ⇒ Object
- #delete(path, params = {}, headers = {}, authenticated: true) ⇒ Object
- #get(path, params = {}, headers = {}, authenticated: true) ⇒ Object
-
#initialize(**options) ⇒ Client
constructor
A new instance of Client.
- #patch(path, body = {}, headers = {}, authenticated: true, idempotency_key: nil) ⇒ Object
- #payment_intents ⇒ Object
- #post(path, body = {}, headers = {}, authenticated: true, idempotency_key: nil) ⇒ Object
- #refunds ⇒ Object
- #store_token!(response) ⇒ Object
- #validate_credentials! ⇒ Object
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(**) config = Airwallex.configuration @client_id = .fetch(:client_id, config.client_id) @api_key = .fetch(:api_key, config.api_key) @login_as = .key?(:login_as) ? [:login_as] : config.login_as @environment = Configuration.validate_environment!(.fetch(:environment, config.environment)) @timeout = .fetch(:timeout, config.timeout) @open_timeout = .fetch(:open_timeout, config.open_timeout) @logger = .fetch(:logger, config.logger) @access_token = nil @token_expires_at = nil end |
Instance Attribute Details
#access_token ⇒ Object (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_key ⇒ Object (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_id ⇒ Object (readonly)
Returns the value of attribute client_id.
16 17 18 |
# File 'lib/airwallex/client.rb', line 16 def client_id @client_id end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
16 17 18 |
# File 'lib/airwallex/client.rb', line 16 def environment @environment end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
16 17 18 |
# File 'lib/airwallex/client.rb', line 16 def logger @logger end |
#login_as ⇒ Object (readonly)
Returns the value of attribute login_as.
16 17 18 |
# File 'lib/airwallex/client.rb', line 16 def login_as @login_as end |
#open_timeout ⇒ Object (readonly)
Returns the value of attribute open_timeout.
16 17 18 |
# File 'lib/airwallex/client.rb', line 16 def open_timeout @open_timeout end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
16 17 18 |
# File 'lib/airwallex/client.rb', line 16 def timeout @timeout end |
#token_expires_at ⇒ Object (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_headers ⇒ Object
57 58 59 |
# File 'lib/airwallex/client.rb', line 57 def auth_headers { "Authorization" => "Bearer #{access_token}" } end |
#authenticate ⇒ Object
49 50 51 |
# File 'lib/airwallex/client.rb', line 49 def authenticate authentication.login end |
#authenticated? ⇒ Boolean
53 54 55 |
# File 'lib/airwallex/client.rb', line 53 def authenticated? !access_token.nil? && !token_expired? end |
#authentication ⇒ Object
37 38 39 |
# File 'lib/airwallex/client.rb', line 37 def authentication @authentication ||= Resources::Authentication.new(self) end |
#base_url ⇒ Object
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_intents ⇒ Object
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 |
#refunds ⇒ Object
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
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 |