Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes DiscountCode errors type, adds new BatchResult type #1276

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Note: For changes to the API, see https://shopify.dev/changelog?filter=api

## Unreleased
- [#1279](https://github.com/Shopify/shopify-api-ruby/pull/1276) Fixes DiscountCode errors type, adds new BatchResult type
- [#1254](https://github.com/Shopify/shopify-api-ruby/pull/1254) Introduce token exchange API for fetching access tokens. This feature is currently unstable and cannot be used yet.
- [#1268](https://github.com/Shopify/shopify-api-ruby/pull/1268) Add [new webhook handler interface](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/webhooks.md#create-a-webhook-handler) to provide `webhook_id ` and `api_version` information to webhook handlers.

Expand Down
67 changes: 64 additions & 3 deletions lib/shopify_api/rest/resources/2024_01/discount_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def initialize(session: ShopifyAPI::Context.active_session, from_hash: nil)

@code = T.let(nil, T.nilable(String))
@created_at = T.let(nil, T.nilable(String))
@errors = T.let(nil, T.nilable(T::Hash[T.untyped, T.untyped]))
@id = T.let(nil, T.nilable(Integer))
@price_rule_id = T.let(nil, T.nilable(Integer))
@updated_at = T.let(nil, T.nilable(String))
Expand All @@ -35,6 +34,7 @@ def initialize(session: ShopifyAPI::Context.active_session, from_hash: nil)
{http_method: :delete, operation: :delete, ids: [:price_rule_id, :id], path: "price_rules/<price_rule_id>/discount_codes/<id>.json"},
{http_method: :get, operation: :count, ids: [], path: "discount_codes/count.json"},
{http_method: :get, operation: :get, ids: [:price_rule_id, :batch_id], path: "price_rules/<price_rule_id>/batch/<batch_id>/discount_codes.json"},
{http_method: :get, operation: :get_batch_results, ids: [:price_rule_id, :batch_id], path: "price_rules/<price_rule_id>/batch/<batch_id>/discount_codes.json"},
{http_method: :get, operation: :get, ids: [:price_rule_id], path: "price_rules/<price_rule_id>/discount_codes.json"},
{http_method: :get, operation: :get, ids: [:price_rule_id, :id], path: "price_rules/<price_rule_id>/discount_codes/<id>.json"},
{http_method: :get, operation: :get_all, ids: [:price_rule_id, :batch_id], path: "price_rules/<price_rule_id>/batch/<batch_id>.json"},
Expand All @@ -48,8 +48,6 @@ def initialize(session: ShopifyAPI::Context.active_session, from_hash: nil)
attr_reader :code
sig { returns(T.nilable(String)) }
attr_reader :created_at
sig { returns(T.nilable(T::Hash[T.untyped, T.untyped])) }
attr_reader :errors
sig { returns(T.nilable(Integer)) }
attr_reader :id
sig { returns(T.nilable(Integer)) }
Expand All @@ -59,6 +57,12 @@ def initialize(session: ShopifyAPI::Context.active_session, from_hash: nil)
sig { returns(T.nilable(Integer)) }
attr_reader :usage_count

class BatchResult < T::Struct
const :id, T.nilable(Integer)
const :code, T.nilable(String)
const :errors, T.nilable(T::Hash[T.untyped, T.untyped])
end

class << self
sig do
params(
Expand Down Expand Up @@ -115,6 +119,17 @@ def all(
session: ShopifyAPI::Context.active_session,
**kwargs
)
unless batch_id.nil?
deprecation_msg = <<~MSG
DEPRECATED: Use `.get_batch_results`
instead of `.all` with `batch_id` argument to get the right return type.
MSG
ShopifyAPI::Logger.deprecated(
deprecation_msg,
"14.0.0",
)
end

response = base_find(
session: session,
ids: {price_rule_id: price_rule_id, batch_id: batch_id},
Expand Down Expand Up @@ -176,6 +191,52 @@ def get_all(
)
end

sig do
params(
price_rule_id: T.nilable(T.any(Integer, String)),
batch_id: T.nilable(T.any(Integer, String)),
session: Auth::Session,
kwargs: T.untyped
).returns(T::Array[BatchResult])
end
def get_batch_results(
price_rule_id: nil,
batch_id: nil,
session: ShopifyAPI::Context.active_session,
**kwargs
)
response = request(
http_method: :get,
operation: :get_batch_results,
session: session,
ids: {price_rule_id: price_rule_id, batch_id: batch_id},
params: {}.merge(kwargs).compact,
body: {},
entity: nil,
)

body = T.cast(response.body, T::Hash[String, T.untyped])
results = T.let([], T::Array[BatchResult])

response_names = json_response_body_names
response_names.each do |response_name|
root = response_name.pluralize
batch_results = body[root]

results = batch_results.map do |result|
original_state = result.transform_keys(&:to_sym)

BatchResult.new(
id: original_state[:id],
code: original_state[:code],
errors: original_state[:errors],
)
end
end

results
end

sig do
params(
code: T.untyped,
Expand Down
2 changes: 1 addition & 1 deletion lib/shopify_api/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# frozen_string_literal: true

module ShopifyAPI
VERSION = "13.4.0"
VERSION = "13.4.1"
end
40 changes: 40 additions & 0 deletions test/rest/2024_01/discount_code_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,44 @@ def test_9()
end
end

sig do
void
end
def test_10()
stub_request(:get, "https://test-shop.myshopify.io/admin/api/2024-01/price_rules/507328175/batch/173232803/discount_codes.json")
.with(
headers: {"X-Shopify-Access-Token"=>"this_is_a_test_token", "Accept"=>"application/json"},
body: {}
)
.to_return(
status: 200,
body: JSON.generate(
{
"discount_codes" => [
{"id" => nil, "code" => "foo", "errors" => { "code": ["Must be unique, please try a different code"] }},
]
}
), headers: {})

response = ShopifyAPI::DiscountCode.get_batch_results(
price_rule_id: 507328175,
batch_id: 173232803,
)

assert_requested(:get, "https://test-shop.myshopify.io/admin/api/2024-01/price_rules/507328175/batch/173232803/discount_codes.json")

response = response.first if response.respond_to?(:first)

# Assert attributes are correctly typed preventing Sorbet errors downstream
if response.respond_to?(:original_state)
response&.original_state&.each do |key, value|
begin
response.send(key)
rescue TypeError => error
fail TypeError.new("#{self.class}##{key} is mistyped: #{error.message}")
end
response.send(key)
end
end
end
end
Loading