Skip to content

Commit c6f95f0

Browse files
authored
Merge pull request #1348 from Shopify/liz/config-to-disable-rest
Add config option to disable the REST client
2 parents 240be28 + c8a8c09 commit c6f95f0

File tree

8 files changed

+110
-1
lines changed

8 files changed

+110
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api
88
- [#1347](https://github.com/Shopify/shopify-api-ruby/pull/1347) Extend webhook registration to support filters
99
- [#1344](https://github.com/Shopify/shopify-api-ruby/pull/1344) Allow ShopifyAPI::Webhooks::Registry to update a webhook when fields or metafield_namespaces are changed.
1010
- [#1343](https://github.com/Shopify/shopify-api-ruby/pull/1343) Make ShopifyAPI::Context::scope parameter optional. `scope` defaults to empty list `[]`.
11+
- [#1348](https://github.com/Shopify/shopify-api-ruby/pull/1348) Add config option that will disable the REST API client and REST resources. New apps should use the GraphQL Admin API
1112

1213
## 14.6.0
1314

docs/usage/rest.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Make a REST API call
2+
> [!WARNING]
3+
> The Admin REST API has been deprecated. New apps should use the GraphQL Admin API. For more information see [All in on GraphQL](https://www.shopify.com/ca/partners/blog/all-in-on-graphql). New apps will be created with the config option `rest_disabled: true`. This will raise a `ShopifyAPI::Errors::DisabledResourceError` if you try to use the REST API.
24
35
Once OAuth is complete, we can use `ShopifyAPI`'s REST library to make authenticated API calls to the Shopify Admin API.
46
#### Required Session

lib/shopify_api/clients/rest/admin.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ class Admin < HttpClient
99

1010
sig { params(session: T.nilable(Auth::Session), api_version: T.nilable(String)).void }
1111
def initialize(session: nil, api_version: nil)
12+
if Context.rest_disabled
13+
raise Errors::DisabledResourceError,
14+
"The Admin REST API has been deprecated. Please use the GraphQL Admin API. For more information see https://www.shopify.com/ca/partners/blog/all-in-on-graphql"
15+
end
16+
1217
@api_version = T.let(api_version || Context.api_version, String)
1318
if api_version
1419
if api_version == Context.api_version

lib/shopify_api/context.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Context
2222
@user_agent_prefix = T.let(nil, T.nilable(String))
2323
@old_api_secret_key = T.let(nil, T.nilable(String))
2424
@response_as_struct = T.let(false, T.nilable(T::Boolean))
25+
@rest_disabled = T.let(false, T.nilable(T::Boolean))
2526

2627
@rest_resource_loader = T.let(nil, T.nilable(Zeitwerk::Loader))
2728

@@ -45,6 +46,7 @@ class << self
4546
old_api_secret_key: T.nilable(String),
4647
api_host: T.nilable(String),
4748
response_as_struct: T.nilable(T::Boolean),
49+
rest_disabled: T.nilable(T::Boolean),
4850
).void
4951
end
5052
def setup(
@@ -62,7 +64,8 @@ def setup(
6264
user_agent_prefix: nil,
6365
old_api_secret_key: nil,
6466
api_host: nil,
65-
response_as_struct: false
67+
response_as_struct: false,
68+
rest_disabled: false
6669
)
6770
unless ShopifyAPI::AdminVersions::SUPPORTED_ADMIN_VERSIONS.include?(api_version)
6871
raise Errors::UnsupportedVersionError,
@@ -82,6 +85,7 @@ def setup(
8285
@user_agent_prefix = user_agent_prefix
8386
@old_api_secret_key = old_api_secret_key
8487
@response_as_struct = response_as_struct
88+
@rest_disabled = rest_disabled
8589
@log_level = if valid_log_level?(log_level)
8690
log_level.to_sym
8791
else
@@ -178,6 +182,11 @@ def host_name
178182
T.must(URI(T.must(host)).host)
179183
end
180184

185+
sig { returns(T::Boolean) }
186+
def rest_disabled
187+
T.must(@rest_disabled)
188+
end
189+
181190
private
182191

183192
sig { params(log_level: T.any(Symbol, String)).returns(T::Boolean) }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
module ShopifyAPI
5+
module Errors
6+
class DisabledResourceError < StandardError; end
7+
end
8+
end

test/clients/base_rest_resource_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@ def setup
1414
ShopifyAPI::Context.load_rest_resources(api_version: ShopifyAPI::Context.api_version)
1515
end
1616

17+
def test_rest_disabled
18+
ShopifyAPI::Context.setup(
19+
api_key: "test-key",
20+
api_secret_key: "test-secret-key",
21+
api_version: "2023-01",
22+
is_private: true,
23+
is_embedded: false,
24+
rest_disabled: true,
25+
)
26+
assert_raises(ShopifyAPI::Errors::DisabledResourceError) do
27+
TestHelpers::FakeResource.find(id: 1, session: @session)
28+
end
29+
30+
ShopifyAPI::Context.setup(
31+
api_key: "test-key",
32+
api_secret_key: "test-secret-key",
33+
api_version: "2023-01",
34+
is_private: true,
35+
is_embedded: false,
36+
rest_disabled: false,
37+
)
38+
end
39+
1740
def test_finds_resource_by_id
1841
body = { fake_resource: { id: 1, attribute: "attribute" } }.to_json
1942

test/clients/rest/admin_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,44 @@ def test_api_version_can_be_overrriden
5050
run_test(:post)
5151
end
5252

53+
def test_raises_error_when_rest_is_disabled
54+
ShopifyAPI::Context.setup(
55+
api_key: "test-key",
56+
api_secret_key: "test-secret-key",
57+
api_version: "2023-01",
58+
is_private: true,
59+
is_embedded: false,
60+
rest_disabled: true,
61+
)
62+
session = ShopifyAPI::Auth::Session.new(
63+
shop: "test-shop.myshopify.com",
64+
access_token: SecureRandom.alphanumeric(10),
65+
)
66+
67+
assert_raises(ShopifyAPI::Errors::DisabledResourceError,
68+
"REST API access has been disabled via Context.rest_disabled") do
69+
ShopifyAPI::Clients::Rest::Admin.new(session: session)
70+
end
71+
end
72+
73+
def test_client_works_normally_when_rest_is_not_disabled
74+
ShopifyAPI::Context.setup(
75+
api_key: "test-key",
76+
api_secret_key: "test-secret-key",
77+
api_version: "2023-01",
78+
is_private: true,
79+
is_embedded: false,
80+
rest_disabled: false,
81+
)
82+
session = ShopifyAPI::Auth::Session.new(
83+
shop: "test-shop.myshopify.com",
84+
access_token: SecureRandom.alphanumeric(10),
85+
)
86+
87+
client = ShopifyAPI::Clients::Rest::Admin.new(session: session)
88+
refute_nil(client)
89+
end
90+
5391
private
5492

5593
def run_test(http_method, path = "/some-path", expected_path = "some-path.json")

test/context_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,29 @@ def test_scope_config_can_be_optional_and_defaults_to_empty
178178
assert_equal(ShopifyAPI::Auth::AuthScopes.new, ShopifyAPI::Context.scope)
179179
end
180180

181+
def test_rest_disabled_defaults_to_false
182+
ShopifyAPI::Context.setup(
183+
api_key: "test-key",
184+
api_secret_key: "test-secret-key",
185+
api_version: "2023-01",
186+
is_private: true,
187+
is_embedded: false,
188+
)
189+
refute(ShopifyAPI::Context.rest_disabled)
190+
end
191+
192+
def test_rest_disabled_can_be_set_in_setup
193+
ShopifyAPI::Context.setup(
194+
api_key: "test-key",
195+
api_secret_key: "test-secret-key",
196+
api_version: "2023-01",
197+
is_private: true,
198+
is_embedded: false,
199+
rest_disabled: true,
200+
)
201+
assert(ShopifyAPI::Context.rest_disabled)
202+
end
203+
181204
def teardown
182205
ShopifyAPI::Context.deactivate_session
183206
end

0 commit comments

Comments
 (0)