Class: Airwallex::Client
- Inherits:
-
Object
- Object
- Airwallex::Client
- Defined in:
- lib/airwallex/client.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#token_expires_at ⇒ Object
readonly
Returns the value of attribute token_expires_at.
Instance Method Summary collapse
- #authenticate! ⇒ Object
- #connection ⇒ Object
- #delete(path, params = {}, headers = {}) ⇒ Object
- #ensure_authenticated! ⇒ Object
- #get(path, params = {}, headers = {}) ⇒ Object
-
#initialize(config = Airwallex.configuration) ⇒ Client
constructor
A new instance of Client.
- #patch(path, body = {}, headers = {}) ⇒ Object
- #post(path, body = {}, headers = {}) ⇒ Object
- #put(path, body = {}, headers = {}) ⇒ Object
- #token_expired? ⇒ Boolean
Constructor Details
#initialize(config = Airwallex.configuration) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 |
# File 'lib/airwallex/client.rb', line 12 def initialize(config = Airwallex.configuration) @config = config @config.validate! @access_token = nil @token_expires_at = nil @token_mutex = Mutex.new end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
10 11 12 |
# File 'lib/airwallex/client.rb', line 10 def access_token @access_token end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
10 11 12 |
# File 'lib/airwallex/client.rb', line 10 def config @config end |
#token_expires_at ⇒ Object (readonly)
Returns the value of attribute token_expires_at.
10 11 12 |
# File 'lib/airwallex/client.rb', line 10 def token_expires_at @token_expires_at end |
Instance Method Details
#authenticate! ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/airwallex/client.rb', line 56 def authenticate! @token_mutex.synchronize do response = connection.post("/api/v1/authentication/login") do |req| req.headers["x-client-id"] = config.client_id req.headers["x-api-key"] = config.api_key req.headers.delete("Authorization") end handle_response_errors(response) data = response.body @access_token = data["token"] @token_expires_at = Time.now + 1800 # 30 minutes @access_token end end |
#connection ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/airwallex/client.rb', line 20 def connection @connection ||= Faraday.new(url: config.api_url) do |conn| conn.request :json conn.request :multipart conn.request :retry, conn.response :json, content_type: /\bjson$/ conn.response :logger, config.logger, { headers: true, bodies: true } if config.logger conn.headers["Content-Type"] = "application/json" conn.headers["User-Agent"] = user_agent conn.headers["x-api-version"] = config.api_version conn.adapter Faraday.default_adapter end end |
#delete(path, params = {}, headers = {}) ⇒ Object
52 53 54 |
# File 'lib/airwallex/client.rb', line 52 def delete(path, params = {}, headers = {}) request(:delete, path, params, headers) end |
#ensure_authenticated! ⇒ Object
81 82 83 |
# File 'lib/airwallex/client.rb', line 81 def ensure_authenticated! authenticate! if token_expired? end |
#get(path, params = {}, headers = {}) ⇒ Object
36 37 38 |
# File 'lib/airwallex/client.rb', line 36 def get(path, params = {}, headers = {}) request(:get, path, params, headers) end |
#patch(path, body = {}, headers = {}) ⇒ Object
48 49 50 |
# File 'lib/airwallex/client.rb', line 48 def patch(path, body = {}, headers = {}) request(:patch, path, body, headers) end |
#post(path, body = {}, headers = {}) ⇒ Object
40 41 42 |
# File 'lib/airwallex/client.rb', line 40 def post(path, body = {}, headers = {}) request(:post, path, body, headers) end |
#put(path, body = {}, headers = {}) ⇒ Object
44 45 46 |
# File 'lib/airwallex/client.rb', line 44 def put(path, body = {}, headers = {}) request(:put, path, body, headers) end |
#token_expired? ⇒ Boolean
74 75 76 77 78 79 |
# File 'lib/airwallex/client.rb', line 74 def token_expired? return true if access_token.nil? || token_expires_at.nil? # Refresh if token expires in less than 5 minutes Time.now >= (token_expires_at - 300) end |