From abe638cdc1ed335926113b379ab1083f16389588 Mon Sep 17 00:00:00 2001 From: k4th Date: Wed, 7 Aug 2024 13:02:37 +0200 Subject: [PATCH 1/2] Fix connection --- lib/beyond_api/connection.rb | 6 +++--- lib/beyond_api/services/checkout/shipping_zone.rb | 3 +-- lib/beyond_api/services/product_management/category.rb | 2 +- lib/beyond_api/services/product_management/image.rb | 2 +- lib/beyond_api/services/product_management/product.rb | 4 ++-- lib/beyond_api/services/product_management/variation.rb | 2 +- .../services/product_management/variation_image.rb | 2 +- lib/beyond_api/services/product_view/category.rb | 9 +++------ lib/beyond_api/services/shop/address.rb | 2 +- lib/beyond_api/services/shop/shop.rb | 2 +- lib/beyond_api/services/storefront/script_tag.rb | 6 +++--- lib/beyond_api/token.rb | 5 ++--- 12 files changed, 20 insertions(+), 25 deletions(-) diff --git a/lib/beyond_api/connection.rb b/lib/beyond_api/connection.rb index a51a975..3ac2002 100644 --- a/lib/beyond_api/connection.rb +++ b/lib/beyond_api/connection.rb @@ -5,9 +5,9 @@ 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 = {}) + agent.get(path, params) + end def agent @agent ||= Faraday.new(url: @session.api_url, ssl: { verify: true }) do |faraday| diff --git a/lib/beyond_api/services/checkout/shipping_zone.rb b/lib/beyond_api/services/checkout/shipping_zone.rb index f29a68a..968f2aa 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") + Request.new(@session).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..5ee62a4 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}") + Request.new(@session).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..d63eae4 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") + Request.new(@session).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..14e24e4 100644 --- a/lib/beyond_api/services/product_management/product.rb +++ b/lib/beyond_api/services/product_management/product.rb @@ -2,12 +2,12 @@ module BeyondApi module ProductManagement class Product < BaseService def all(params = {}) - # Request.new(@session).get("/products", params) + # Request.new(@session).get("products", params) fetch_all_pages("/products", :products, params) end def find(id) - Request.new(@session).get("/products/#{id}") + Request.new(@session).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..881daf6 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") + Request.new(@session).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..b11faaa 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") + Request.new(@session).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..5c0f251 100644 --- a/lib/beyond_api/services/product_view/category.rb +++ b/lib/beyond_api/services/product_view/category.rb @@ -2,18 +2,15 @@ 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) 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..1710c9c 100644 --- a/lib/beyond_api/services/shop/address.rb +++ b/lib/beyond_api/services/shop/address.rb @@ -2,7 +2,7 @@ module BeyondApi module Shop class Address < BaseService def get - Request.new(@session).get("/shop/address") + Request.new(@session).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..3f807ac 100644 --- a/lib/beyond_api/services/shop/shop.rb +++ b/lib/beyond_api/services/shop/shop.rb @@ -2,7 +2,7 @@ module BeyondApi module Shop class Shop < BaseService def details - Request.new(@session).get("/shop") + Request.new(@session).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..a28c72b 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) + Request.new(@session).get("script-tags", params) end def create(script_url) - Request.new(@session).post("/script-tags", script_url:) + Request.new(@session).post("script-tags", script_url:) end def delete(id) - Request.new(@session).delete("/script-tags/#{id}") + Request.new(@session).delete("script-tags/#{id}") end end end diff --git a/lib/beyond_api/token.rb b/lib/beyond_api/token.rb index 3daa1ea..28bbe20 100644 --- a/lib/beyond_api/token.rb +++ b/lib/beyond_api/token.rb @@ -36,14 +36,13 @@ def client_credentials private def handle_token_call(grant_type, params = {}) - path = "/oauth/token?" + path = "oauth/token?" 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 From d59237e35be10189dd3d0c8201729b83f33a6afa Mon Sep 17 00:00:00 2001 From: k4th Date: Wed, 7 Aug 2024 13:34:26 +0200 Subject: [PATCH 2/2] Update requests --- lib/beyond_api/connection.rb | 15 +++++++++++++-- lib/beyond_api/services/checkout/shipping_zone.rb | 2 +- .../services/product_management/category.rb | 2 +- .../services/product_management/image.rb | 2 +- .../services/product_management/product.rb | 5 +++-- .../services/product_management/variation.rb | 2 +- .../product_management/variation_image.rb | 2 +- lib/beyond_api/services/product_view/category.rb | 3 ++- lib/beyond_api/services/shop/address.rb | 4 ++-- lib/beyond_api/services/shop/shop.rb | 4 ++-- lib/beyond_api/services/storefront/script_tag.rb | 6 +++--- lib/beyond_api/token.rb | 1 + 12 files changed, 31 insertions(+), 17 deletions(-) diff --git a/lib/beyond_api/connection.rb b/lib/beyond_api/connection.rb index 3ac2002..65a38b6 100644 --- a/lib/beyond_api/connection.rb +++ b/lib/beyond_api/connection.rb @@ -6,7 +6,18 @@ module Connection LOGGER.level = Kernel.const_get("::Logger::#{BeyondApi.configuration.log_level.to_s.upcase}") def get(path, params = {}) - agent.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 @@ -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 968f2aa..d2836c1 100644 --- a/lib/beyond_api/services/checkout/shipping_zone.rb +++ b/lib/beyond_api/services/checkout/shipping_zone.rb @@ -2,7 +2,7 @@ module BeyondApi module Checkout class ShippingZone < BaseService def all - 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 5ee62a4..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 d63eae4..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 14e24e4..56f9c8d 100644 --- a/lib/beyond_api/services/product_management/product.rb +++ b/lib/beyond_api/services/product_management/product.rb @@ -3,11 +3,12 @@ module ProductManagement class Product < BaseService def all(params = {}) # Request.new(@session).get("products", params) - fetch_all_pages("/products", :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 881daf6..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 b11faaa..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 5c0f251..005042c 100644 --- a/lib/beyond_api/services/product_view/category.rb +++ b/lib/beyond_api/services/product_view/category.rb @@ -2,7 +2,8 @@ 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) diff --git a/lib/beyond_api/services/shop/address.rb b/lib/beyond_api/services/shop/address.rb index 1710c9c..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 3f807ac..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 a28c72b..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 28bbe20..aed0787 100644 --- a/lib/beyond_api/token.rb +++ b/lib/beyond_api/token.rb @@ -37,6 +37,7 @@ def client_credentials def handle_token_call(grant_type, params = {}) path = "oauth/token?" + @oauth = true params.merge!(grant_type: grant_type)