Class: Amorail::Client
- Inherits:
-
Object
- Object
- Amorail::Client
- Defined in:
- lib/amorail/client.rb
Overview
Amorail http client
Constant Summary collapse
- SUCCESS_STATUS_CODES =
[200, 204].freeze
Instance Attribute Summary collapse
-
#access ⇒ Object
readonly
Returns the value of attribute access.
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#api_endpoint ⇒ Object
readonly
Returns the value of attribute api_endpoint.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#redirect_uri ⇒ Object
readonly
Returns the value of attribute redirect_uri.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
-
#store ⇒ Object
Returns the value of attribute store.
Instance Method Summary collapse
- #auth_params ⇒ Object
- #authorize ⇒ Object
- #connect ⇒ Object
- #get(url, params = {}) ⇒ Object
-
#initialize(api_endpoint: Amorail.config.api_endpoint, client_id: Amorail.config.client_id, client_secret: Amorail.config.client_secret, code: Amorail.config.code, redirect_uri: Amorail.config.redirect_uri) ⇒ Client
constructor
A new instance of Client.
- #post(url, params = {}) ⇒ Object
- #properties ⇒ Object
- #refresh_params ⇒ Object
- #refresh_token! ⇒ Object
- #safe_request(method, url, params = {}) ⇒ Object
Constructor Details
#initialize(api_endpoint: Amorail.config.api_endpoint, client_id: Amorail.config.client_id, client_secret: Amorail.config.client_secret, code: Amorail.config.code, redirect_uri: Amorail.config.redirect_uri) ⇒ Client
Returns a new instance of Client.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/amorail/client.rb', line 23 def initialize(api_endpoint: Amorail.config.api_endpoint, client_id: Amorail.config.client_id, client_secret: Amorail.config.client_secret, code: Amorail.config.code, redirect_uri: Amorail.config.redirect_uri) @store = Amorail.token_store @api_endpoint = api_endpoint @client_id = client_id @client_secret = client_secret @code = code @redirect_uri = redirect_uri @access = AccessToken.find(@client_secret, store) @access_token = @access.token @refresh_token = @access.refresh_token @connect = Faraday.new(url: api_endpoint) do |faraday| faraday.response :json, content_type: /\bjson$/ faraday.use :instrumentation faraday.adapter Faraday.default_adapter end end |
Instance Attribute Details
#access ⇒ Object (readonly)
Returns the value of attribute access.
14 15 16 |
# File 'lib/amorail/client.rb', line 14 def access @access end |
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
14 15 16 |
# File 'lib/amorail/client.rb', line 14 def access_token @access_token end |
#api_endpoint ⇒ Object (readonly)
Returns the value of attribute api_endpoint.
14 15 16 |
# File 'lib/amorail/client.rb', line 14 def api_endpoint @api_endpoint end |
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
14 15 16 |
# File 'lib/amorail/client.rb', line 14 def client_id @client_id end |
#client_secret ⇒ Object (readonly)
Returns the value of attribute client_secret.
14 15 16 |
# File 'lib/amorail/client.rb', line 14 def client_secret @client_secret end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
14 15 16 |
# File 'lib/amorail/client.rb', line 14 def code @code end |
#redirect_uri ⇒ Object (readonly)
Returns the value of attribute redirect_uri.
14 15 16 |
# File 'lib/amorail/client.rb', line 14 def redirect_uri @redirect_uri end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
14 15 16 |
# File 'lib/amorail/client.rb', line 14 def refresh_token @refresh_token end |
#store ⇒ Object
Returns the value of attribute store.
13 14 15 |
# File 'lib/amorail/client.rb', line 13 def store @store end |
Instance Method Details
#auth_params ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/amorail/client.rb', line 65 def auth_params { client_id: client_id, client_secret: client_secret, grant_type: 'authorization_code', code: @code, redirect_uri: redirect_uri } end |
#authorize ⇒ Object
53 54 55 56 57 |
# File 'lib/amorail/client.rb', line 53 def response = post(Amorail.config.auth_url, auth_params) create_access_token(response) response end |
#connect ⇒ Object
49 50 51 |
# File 'lib/amorail/client.rb', line 49 def connect @connect || self.class.new end |
#get(url, params = {}) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/amorail/client.rb', line 91 def get(url, params = {}) response = connect.get(url, params) do |request| request.headers['Authorization'] = "Bearer #{access_token}" if access_token.present? end handle_response(response) end |
#post(url, params = {}) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/amorail/client.rb', line 98 def post(url, params = {}) response = connect.post(url) do |request| request.headers['Authorization'] = "Bearer #{access_token}" if access_token.present? request.headers['Content-Type'] = 'application/json' request.body = params.to_json end handle_response(response) end |
#properties ⇒ Object
45 46 47 |
# File 'lib/amorail/client.rb', line 45 def properties @properties ||= Property.new(self) end |
#refresh_params ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/amorail/client.rb', line 75 def refresh_params { client_id: client_id, client_secret: client_secret, grant_type: 'refresh_token', refresh_token: refresh_token, redirect_uri: redirect_uri } end |
#refresh_token! ⇒ Object
59 60 61 62 63 |
# File 'lib/amorail/client.rb', line 59 def refresh_token! response = post(Amorail.config.auth_url, refresh_params) update_access_token(response) response end |
#safe_request(method, url, params = {}) ⇒ Object
85 86 87 88 89 |
# File 'lib/amorail/client.rb', line 85 def safe_request(method, url, params = {}) if access_token.blank? refresh_token! if access.expired? public_send(method, url, params) end |