diff --git a/lib/beyond_api/connection.rb b/lib/beyond_api/connection.rb index 65a38b6..a35360e 100644 --- a/lib/beyond_api/connection.rb +++ b/lib/beyond_api/connection.rb @@ -8,39 +8,46 @@ module Connection def get(path, params = {}) parsed_response agent.get(path, params) end - + def post(path, body = {}, params = {}) - response = agent.post(path, body) do |request| - request.params = params - end - parsed_response(response) + response = agent.post(path, body) do |request| + request.params = params end + parsed_response(response) + end + + def delete(path, params = {}) + parsed_response agent.delete(path, params) + end + + private def parsed_response(response) Response.new(response).handle end + + def parsed_body(body) + Utils.camelize_keys(body) + end def agent @agent ||= Faraday.new(url: @session.api_url, ssl: { verify: true }) do |faraday| # Timeouts faraday.options.timeout = BeyondApi.configuration.timeout.to_i faraday.options.open_timeout = BeyondApi.configuration.open_timeout.to_i - # Authorization - if @oauth.blank? # @session && @session.access_token.present? - faraday.request :authorization, "Bearer", @session.access_token - else + case @authorization + when :basic faraday.request :authorization, :basic, BeyondApi.configuration.client_id, BeyondApi.configuration.client_secret + when :bearer + faraday.request :authorization, "Bearer", @session.access_token end - # Headers faraday.headers["Accept"] = "application/json" # Set default accept header faraday.headers["Content-Type"] = "application/json" # Set default content type - # Request options faraday.request :json # Encode request bodies as JSON faraday.request :retry, BeyondApi.configuration.retry_options - # Response options faraday.response :json, content_type: "application/json" faraday.response :logger, *logger_config { |logger| apply_filters(logger) } diff --git a/lib/beyond_api/response.rb b/lib/beyond_api/response.rb index 9a86015..746f494 100644 --- a/lib/beyond_api/response.rb +++ b/lib/beyond_api/response.rb @@ -16,6 +16,7 @@ def handle private + # TODO: benchmark this ! def parsed_response return {} if body.blank? diff --git a/lib/beyond_api/services/authentication/token.rb b/lib/beyond_api/services/authentication/token.rb index eb0643e..f5487ff 100644 --- a/lib/beyond_api/services/authentication/token.rb +++ b/lib/beyond_api/services/authentication/token.rb @@ -1,17 +1,23 @@ module BeyondApi module Authentication - class Token < BaseService - def refresh(refresh_token) - handle_token_call("refresh_token", refresh_token:) + class Token + include Connection # @session, @authorization + + def initialize(session = nil) + @session = session + @authorization = :basic end - def handle_token_call(grant_type, params = {}) - path = "oauth/token" - @oauth = true + def refresh(refresh_token) + post("oauth/token", {}, { grant_type: "refresh_token", refresh_token: }) + end - params.merge!(grant_type:) + def get(code) + post("oauth/token", {}, { grant_type: "authorization_code" , code: }) + end - post(path, {}, params) + def client_credentials + post("oauth/token", {}, { grant_type: "client_credentials" }) end end end diff --git a/lib/beyond_api/services/base_service.rb b/lib/beyond_api/services/base_service.rb index 0a4ac98..af16fc5 100644 --- a/lib/beyond_api/services/base_service.rb +++ b/lib/beyond_api/services/base_service.rb @@ -1,11 +1,12 @@ module BeyondApi class BaseService - attr_reader :session + include Connection # @session, @authorization - include Connection # @session + ServiceSession = Struct.new(:api_url, :access_token, :refresh_token) - def initialize(session = nil) - @session = session + def initialize(**params) + @session = initialize_session(params) + @authorization = :bearer end def fetch_all_pages(url, resource, params = {}) @@ -20,6 +21,14 @@ def fetch_all_pages(url, resource, params = {}) private + def initialize_session(session) + ServiceSession.new( + session[:api_url], + session[:access_token], + session[:refresh_token] + ) + end + def adjust_response(result) result[:page][:size] = result[:page][:total_elements] result[:page][:total_pages] = 1 diff --git a/lib/beyond_api/services/storefront/script_tag.rb b/lib/beyond_api/services/storefront/script_tag.rb index e5fd958..6c5eaed 100644 --- a/lib/beyond_api/services/storefront/script_tag.rb +++ b/lib/beyond_api/services/storefront/script_tag.rb @@ -11,7 +11,7 @@ def create(script_url) post("script-tags", script_url:) end - def delete(id) + def destroy(id) delete("script-tags/#{id}") end end diff --git a/lib/beyond_api/session.rb b/lib/beyond_api/session.rb deleted file mode 100644 index 90cb1cf..0000000 --- a/lib/beyond_api/session.rb +++ /dev/null @@ -1,48 +0,0 @@ -# frozen_string_literal: true - -module BeyondApi - class Session - include Connection - - attr_reader :api_url - attr_accessor :access_token, :refresh_token - - def initialize(api_url:, access_token: nil, refresh_token: nil) - uri = URI.parse(api_url) - - @api_url = "#{uri.scheme}://#{uri.host}/api" - @access_token = access_token - @refresh_token = refresh_token - end - - def authorization_code(code) - handle_token_call("authorization_code", code: code) - end - - def refresh_token - handle_token_call("refresh_token", refresh_token: refresh_token) - end - - def get_client_credentials - handle_token_call("client_credentials") - end - - alias refresh refresh_token - alias create authorization_code - - private - - def handle_token_call(grant_type, params = {}) - path = "oauth/token" - @oauth = true - - params.merge!(grant_type: grant_type) - - response = post(path, {}, params) - - @session.access_token = response[:access_token] - @session.refresh_token = response[:refresh_token] - @session - end - end -end