class Amorail::Client
Amorail
http client
Constants
- SUCCESS_STATUS_CODES
Attributes
access[R]
access_token[R]
api_endpoint[R]
client_id[R]
client_secret[R]
code[R]
redirect_uri[R]
refresh_token[R]
store[RW]
Public Class Methods
new(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)
click to toggle source
# 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
Public Instance Methods
auth_params()
click to toggle source
# 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
connect()
click to toggle source
# File lib/amorail/client.rb, line 49 def connect @connect || self.class.new end
get(url, params = {})
click to toggle source
# 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 = {})
click to toggle source
# 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()
click to toggle source
# File lib/amorail/client.rb, line 45 def properties @properties ||= Property.new(self) end
refresh_params()
click to toggle source
# 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!()
click to toggle source
# 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 = {})
click to toggle source
# File lib/amorail/client.rb, line 85 def safe_request(method, url, params = {}) authorize if access_token.blank? refresh_token! if access.expired? public_send(method, url, params) end
Private Instance Methods
create_access_token(response)
click to toggle source
# File lib/amorail/client.rb, line 134 def create_access_token(response) _access = AccessToken.create( client_secret, response.body['access_token'], response.body['refresh_token'], expiration(response.body['expires_in']), store ) @access_token = _access.token @refresh_token = _access.refresh_token end
expiration(expires_in)
click to toggle source
# File lib/amorail/client.rb, line 158 def expiration(expires_in) Time.now.to_i + expires_in.to_i end
handle_response(response)
click to toggle source
# File lib/amorail/client.rb, line 109 def handle_response(response) # rubocop:disable all return response if SUCCESS_STATUS_CODES.include?(response.status) case response.status when 301 fail ::Amorail::AmoMovedPermanentlyError when 400 fail ::Amorail::AmoBadRequestError when 401 fail ::Amorail::AmoUnauthorizedError when 403 fail ::Amorail::AmoForbiddenError when 404 fail ::Amorail::AmoNotFoundError when 500 fail ::Amorail::AmoInternalError when 502 fail ::Amorail::AmoBadGatewayError when 503 fail ::Amorail::AmoServiceUnaviableError else fail ::Amorail::AmoUnknownError, response.body end end
update_access_token(response)
click to toggle source
# File lib/amorail/client.rb, line 146 def update_access_token(response) _access = AccessToken.refresh( client_secret, response.body['access_token'], response.body['refresh_token'], expiration(response.body['expires_in']), store ) @access_token = _access.token @refresh_token = _access.refresh_token end