Class: Airwallex::Client

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

Constant Summary collapse

LOGIN_PATH =
"/api/v1/authentication/login"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Airwallex.configuration) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
# File 'lib/airwallex/client.rb', line 14

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_tokenObject (readonly)

Returns the value of attribute access_token.



12
13
14
# File 'lib/airwallex/client.rb', line 12

def access_token
  @access_token
end

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/airwallex/client.rb', line 12

def config
  @config
end

#token_expires_atObject (readonly)

Returns the value of attribute token_expires_at.



12
13
14
# File 'lib/airwallex/client.rb', line 12

def token_expires_at
  @token_expires_at
end

Instance Method Details

#authenticate!Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/airwallex/client.rb', line 54

def authenticate!
  @token_mutex.synchronize do
    response = connection.post(LOGIN_PATH) do |req|
      req.headers["x-client-id"] = config.client_id
      req.headers["x-api-key"] = config.api_key
    end

    handle_response_errors(response)

    data = response.body
    @access_token = data["token"]
    @token_expires_at = Time.now + 1800 # 30 minutes

    @access_token
  end
end

#connectionObject



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

def connection
  @connection ||= Faraday.new(url: config.api_url) do |conn|
    configure_middleware(conn)

    conn.headers["Content-Type"] = "application/json"
    conn.headers["User-Agent"] = user_agent
    conn.headers["x-api-version"] = config.api_version if config.api_version

    conn.adapter Faraday.default_adapter
  end
end

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



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

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

#ensure_authenticated!Object



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

def ensure_authenticated!
  authenticate! if token_expired?
end

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



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

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

#patch(path, body = {}, headers = {}) ⇒ Object



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

def patch(path, body = {}, headers = {})
  request(:patch, path, body, headers)
end

#post(path, body = {}, headers = {}) ⇒ Object



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

def post(path, body = {}, headers = {})
  request(:post, path, body, headers)
end

#put(path, body = {}, headers = {}) ⇒ Object



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

def put(path, body = {}, headers = {})
  request(:put, path, body, headers)
end

#token_expired?Boolean

Returns:

  • (Boolean)


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

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