diff --git a/lib/beyond_api/connection.rb b/lib/beyond_api/connection.rb index a51a975..65a38b6 100644 --- a/lib/beyond_api/connection.rb +++ b/lib/beyond_api/connection.rb @@ -5,9 +5,20 @@ module Connection LOGGER = BeyondApi.logger LOGGER.level = Kernel.const_get("::Logger::#{BeyondApi.configuration.log_level.to_s.upcase}") - # def get(path, params = {}) - # @agent.get(path, params) - # end + 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) + end + + def parsed_response(response) + Response.new(response).handle + end def agent @agent ||= Faraday.new(url: @session.api_url, ssl: { verify: true }) do |faraday| @@ -16,7 +27,7 @@ def agent faraday.options.open_timeout = BeyondApi.configuration.open_timeout.to_i # Authorization - if @session && @session.access_token.present? + if @oauth.blank? # @session && @session.access_token.present? faraday.request :authorization, "Bearer", @session.access_token else faraday.request :authorization, :basic, BeyondApi.configuration.client_id, BeyondApi.configuration.client_secret diff --git a/lib/beyond_api/services/checkout/shipping_zone.rb b/lib/beyond_api/services/checkout/shipping_zone.rb index f29a68a..d2836c1 100644 --- a/lib/beyond_api/services/checkout/shipping_zone.rb +++ b/lib/beyond_api/services/checkout/shipping_zone.rb @@ -2,8 +2,7 @@ module BeyondApi module Checkout class ShippingZone < BaseService def all - # TODO: iterate over all pages - Request.new(@session).get("/shipping-zones") + get("shipping-zones") end end end diff --git a/lib/beyond_api/services/product_management/category.rb b/lib/beyond_api/services/product_management/category.rb index 75cdbe3..a2c4058 100644 --- a/lib/beyond_api/services/product_management/category.rb +++ b/lib/beyond_api/services/product_management/category.rb @@ -2,7 +2,7 @@ module BeyondApi module ProductManagement class Category < BaseService def find(id) - Request.new(@session).get("/categories/#{id}") + get("categories/#{id}") end end end diff --git a/lib/beyond_api/services/product_management/image.rb b/lib/beyond_api/services/product_management/image.rb index a1e175a..50cb0da 100644 --- a/lib/beyond_api/services/product_management/image.rb +++ b/lib/beyond_api/services/product_management/image.rb @@ -2,7 +2,7 @@ module BeyondApi module ProductManagement class Image < BaseService def all(id) - Request.new(@session).get("/products/#{id}/images") + get("products/#{id}/images") end end end diff --git a/lib/beyond_api/services/product_management/product.rb b/lib/beyond_api/services/product_management/product.rb index 83cfc1d..56f9c8d 100644 --- a/lib/beyond_api/services/product_management/product.rb +++ b/lib/beyond_api/services/product_management/product.rb @@ -2,12 +2,13 @@ module BeyondApi module ProductManagement class Product < BaseService def all(params = {}) - # Request.new(@session).get("/products", params) - fetch_all_pages("/products", :products, params) + # Request.new(@session).get("products", params) + # fetch_all_pages("/products", :products, params) + get("products", params) end def find(id) - Request.new(@session).get("/products/#{id}") + get("products/#{id}") end end end diff --git a/lib/beyond_api/services/product_management/variation.rb b/lib/beyond_api/services/product_management/variation.rb index 8e34c24..12d00a1 100644 --- a/lib/beyond_api/services/product_management/variation.rb +++ b/lib/beyond_api/services/product_management/variation.rb @@ -2,7 +2,7 @@ module BeyondApi module ProductManagement class Variation < BaseService def all(id) - Request.new(@session).get("/products/#{id}/variations") + get("products/#{id}/variations") end end end diff --git a/lib/beyond_api/services/product_management/variation_image.rb b/lib/beyond_api/services/product_management/variation_image.rb index 0eb3eb7..22194a9 100644 --- a/lib/beyond_api/services/product_management/variation_image.rb +++ b/lib/beyond_api/services/product_management/variation_image.rb @@ -2,7 +2,7 @@ module BeyondApi module ProductManagement class VariationImage < BaseService def all(product_id, variation_id) - Request.new(@session).get("/products/#{product_id}/variations/#{variation_id}/images") + get("products/#{product_id}/variations/#{variation_id}/images") end end end diff --git a/lib/beyond_api/services/product_view/category.rb b/lib/beyond_api/services/product_view/category.rb index abeeb34..005042c 100644 --- a/lib/beyond_api/services/product_view/category.rb +++ b/lib/beyond_api/services/product_view/category.rb @@ -2,18 +2,16 @@ module BeyondApi module ProductView class Category < BaseService def all(params = {}) - fetch_all_pages("/product-view/categories", :categories, params) + # fetch_all_pages("product-view/categories", :categories, params) + get("product-view/categories", params) end def find(id) - get("/product-view/categories/#{id}") + get("product-view/categories/#{id}") end def preview(body, params = {}) - post( - "/product-view/categories/preview", - body, - params) + post("product-view/categories/preview", body, params) end end end diff --git a/lib/beyond_api/services/shop/address.rb b/lib/beyond_api/services/shop/address.rb index c01922d..cca88aa 100644 --- a/lib/beyond_api/services/shop/address.rb +++ b/lib/beyond_api/services/shop/address.rb @@ -1,8 +1,8 @@ module BeyondApi module Shop class Address < BaseService - def get - Request.new(@session).get("/shop/address") + def show + get("shop/address") end end end diff --git a/lib/beyond_api/services/shop/shop.rb b/lib/beyond_api/services/shop/shop.rb index b758623..d360de1 100644 --- a/lib/beyond_api/services/shop/shop.rb +++ b/lib/beyond_api/services/shop/shop.rb @@ -1,8 +1,8 @@ module BeyondApi module Shop class Shop < BaseService - def details - Request.new(@session).get("/shop") + def show + get("shop") end end end diff --git a/lib/beyond_api/services/storefront/script_tag.rb b/lib/beyond_api/services/storefront/script_tag.rb index 2dcbcf4..e5fd958 100644 --- a/lib/beyond_api/services/storefront/script_tag.rb +++ b/lib/beyond_api/services/storefront/script_tag.rb @@ -4,15 +4,15 @@ class ScriptTag < BaseService def all(params = {}) params.merge!(client_id: BeyondApi.configuration.client_id) if params[:only_own] - Request.new(@session).get("/script-tags", params) + get("script-tags", params) end def create(script_url) - Request.new(@session).post("/script-tags", script_url:) + post("script-tags", script_url:) end def delete(id) - Request.new(@session).delete("/script-tags/#{id}") + delete("script-tags/#{id}") end end end diff --git a/lib/beyond_api/token.rb b/lib/beyond_api/token.rb index 3daa1ea..aed0787 100644 --- a/lib/beyond_api/token.rb +++ b/lib/beyond_api/token.rb @@ -36,14 +36,14 @@ def client_credentials private def handle_token_call(grant_type, params = {}) - path = "/oauth/token?" + path = "oauth/token?" + @oauth = true params.merge!(grant_type: grant_type) - response = agent.post(path, {}) do |request| + response = agent.post(path) do |request| request.params = params end - binding.b handle_response(response) end