Skip to content

Commit

Permalink
Add guard for sandbox-only
Browse files Browse the repository at this point in the history
  • Loading branch information
hakanensari committed Sep 20, 2024
1 parent 0aa413a commit 2288d7c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion bin/templates/api.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ class <%= api_data[:class_name] %> < API
<%= generate_method_docs(operation) %>
# @return [Hash] The API response
def <%= snakecase(operation["operationId"]) %><%= generate_parameters(operation["parameters"], snakecase(operation["operationId"]), 6) %>
<% if !operation["static_sandbox"] && !operation["dynamic_sandbox"] -%>
<% if !operation["static_sandbox"] && !operation["dynamic_sandbox"] -%>
cannot_sandbox!
<% elsif operation["sandbox_only"] %>
must_sandbox!
<% end %>
path = "<%= operation["path"] %>"
<% if operation["body_param"] && operation["body_param"]["name"] != "body" -%>
Expand Down
10 changes: 8 additions & 2 deletions lib/peddler/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Peddler
# Wraps an Amazon Selling Partner API (SP-API)
class API
class CannotSandbox < StandardError; end
class MustSandbox < StandardError; end

# @return [Peddler::Region]
attr_reader :region
Expand Down Expand Up @@ -41,9 +42,14 @@ def sandbox?
@sandbox
end

# @raise [CannotSandbox] if in sandbox environment
# @raise [CannotSandbox] if in a sandbox environment
def cannot_sandbox!
raise CannotSandbox, "cannot run in sandbox" if sandbox?
raise CannotSandbox, "cannot run in a sandbox" if sandbox?
end

# @raise [MustSandbox] unless in a sandbox environment
def must_sandbox!
raise MustSandbox, "must run in a sandbox" unless sandbox?
end

# @see https://developer-docs.amazon.com/sp-api/docs/include-a-user-agent-header-in-all-requests
Expand Down
6 changes: 6 additions & 0 deletions lib/peddler/api/fba_inventory_v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def get_inventory_summaries(granularity_type, granularity_id, marketplace_ids, d
# @param [Float] rate_limit Requests per second
# @return [Hash] The API response
def create_inventory_item(create_inventory_item_request_body, rate_limit: nil)
must_sandbox!

path = "/fba/inventory/v1/items"
body = create_inventory_item_request_body

Expand All @@ -88,6 +90,8 @@ def create_inventory_item(create_inventory_item_request_body, rate_limit: nil)
# @param [Float] rate_limit Requests per second
# @return [Hash] The API response
def delete_inventory_item(seller_sku, marketplace_id, rate_limit: nil)
must_sandbox!

path = "/fba/inventory/v1/items/#{seller_sku}"
params = {
"marketplaceId" => marketplace_id,
Expand All @@ -108,6 +112,8 @@ def delete_inventory_item(seller_sku, marketplace_id, rate_limit: nil)
# @param [Float] rate_limit Requests per second
# @return [Hash] The API response
def add_inventory(x_amzn_idempotency_token, add_inventory_request_body, rate_limit: nil)
must_sandbox!

path = "/fba/inventory/v1/items/inventory"
body = add_inventory_request_body

Expand Down
2 changes: 2 additions & 0 deletions lib/peddler/api/fulfillment_outbound_2020_07_01.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ def cancel_fulfillment_order(seller_fulfillment_order_id, rate_limit: 2.0)
# @param [Float] rate_limit Requests per second
# @return [Hash] The API response
def submit_fulfillment_order_status_update(seller_fulfillment_order_id, body, rate_limit: nil)
must_sandbox!

path = "/fba/outbound/2020-07-01/fulfillmentOrders/#{seller_fulfillment_order_id}/status"

put(path, body:)
Expand Down
6 changes: 6 additions & 0 deletions test/peddler/api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def test_cannot_sandbox
end
end

def test_must_sandbox
assert_raises(API::MustSandbox) do
@api.must_sandbox!
end
end

def test_host_header
assert(@api.http.default_options.headers["Host"])
end
Expand Down

0 comments on commit 2288d7c

Please sign in to comment.