Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/beyond_api/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/beyond_api/services/checkout/shipping_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/beyond_api/services/product_management/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/beyond_api/services/product_management/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lib/beyond_api/services/product_management/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/beyond_api/services/product_management/variation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions lib/beyond_api/services/product_view/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/beyond_api/services/shop/address.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/beyond_api/services/shop/shop.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/beyond_api/services/storefront/script_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/beyond_api/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down