Skip to content
Open
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 @@ -2,6 +2,7 @@

Note: For changes to the API, see https://shopify.dev/changelog?filter=api
## Unreleased
- [#1375](https://github.com/Shopify/shopify-api-ruby/pull/1375) add optional `ShopifyAPI::Context::httparty_params` to configure HTTP request params such as timeout, debug_output, or proxy settings.
- [#1443](https://github.com/Shopify/shopify-api-ruby/pull/1443) Add `ShopifyAPI::Utils::ShopValidator` (module) with `sanitize_shop_domain` and `sanitize!`.
- [#1443](https://github.com/Shopify/shopify-api-ruby/pull/1443) `ShopifyAPI::Auth::TokenExchange.exchange_token` always uses the session token's `dest` claim, instead of the `shop` parameter, that is now deprecated. It will show a deprecation warning and the argument will be removed in the next major version.

Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ ShopifyAPI::Context.setup(
)
```

Optionally, you can override HTTP request params such as timeout, debug_output, or proxy settings.
```ruby
ShopifyAPI::Context.setup(
# other params...
httparty_params: {
timeout: 60, # Set the open/read/write timeout for HTTP requests
open_timeout: 60, # Set the open timeout for HTTP requests
read_timeout: 60, # Set the read timeout for HTTP requests
write_timeout: 60, # Set the write timeout for HTTP requests
debug_output: false, # Set to true to enable debug output for HTTP requests
http_proxyaddr: "proxy.example.com", # Set the HTTP proxy address (hostname only, no scheme or port)
http_proxyport: 8080, # Set the HTTP proxy port
http_proxyuser: "username", # Set the HTTP proxy username
http_proxypass: "password", # Set the HTTP proxy password
}
)
```

### Performing OAuth

You need to go through OAuth as described [here](https://shopify.dev/docs/apps/auth/oauth) to create sessions for shops using your app.
Expand Down
1 change: 1 addition & 0 deletions lib/shopify_api/clients/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def request(request, response_as_struct: false)
res = T.cast(HTTParty.send(
request.http_method,
parsed_uri.to_s,
**(Context.httparty_params || {}),
headers: headers,
query: request.query,
body: request.body.class == Hash ? T.unsafe(request.body).to_json : request.body,
Expand Down
10 changes: 9 additions & 1 deletion lib/shopify_api/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Context
@rest_resource_loader = T.let(nil, T.nilable(Zeitwerk::Loader))
@expiring_offline_access_tokens = T.let(false, T::Boolean)

@httparty_params = T.let({}, T::Hash[Symbol, T.untyped])

class << self
extend T::Sig

Expand All @@ -49,6 +51,7 @@ class << self
response_as_struct: T.nilable(T::Boolean),
rest_disabled: T.nilable(T::Boolean),
expiring_offline_access_tokens: T.nilable(T::Boolean),
httparty_params: T.nilable(T::Hash[Symbol, T.untyped])
).void
end
def setup(
Expand All @@ -68,7 +71,8 @@ def setup(
api_host: nil,
response_as_struct: false,
rest_disabled: false,
expiring_offline_access_tokens: false
expiring_offline_access_tokens: false,
httparty_params: nil
)
unless ShopifyAPI::AdminVersions::SUPPORTED_ADMIN_VERSIONS.include?(api_version)
raise Errors::UnsupportedVersionError,
Expand Down Expand Up @@ -97,6 +101,7 @@ def setup(
end

load_rest_resources(api_version: api_version)
@httparty_params = httparty_params || {}
end

sig { params(api_version: String).void }
Expand Down Expand Up @@ -155,6 +160,9 @@ def private?
sig { returns(T::Boolean) }
attr_reader :expiring_offline_access_tokens

sig { returns(T::Hash[Symbol, T.untyped]) }
attr_reader :httparty_params

sig { returns(T::Boolean) }
def embedded?
@is_embedded
Expand Down
64 changes: 64 additions & 0 deletions test/context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_setup
assert_equal("http", ShopifyAPI::Context.host_scheme)
assert_equal("localhost", ShopifyAPI::Context.host_name)
assert_equal("example.com", ShopifyAPI::Context.api_host)
assert_equal({}, ShopifyAPI::Context.httparty_params)

ShopifyAPI::Context.setup(
api_key: "key",
Expand Down Expand Up @@ -146,6 +147,69 @@ def test_with_host_name_and_no_host_env
ENV["HOST"] = old_host
end

def test_with_httparty_params
clear_context

ShopifyAPI::Context.setup(
api_key: "key",
api_secret_key: "secret",
api_version: "2023-10",
scope: ["scope1", "scope2"],
is_private: true,
is_embedded: true,
log_level: :off,
private_shop: "privateshop.myshopify.com",
user_agent_prefix: "user_agent_prefix1",
old_api_secret_key: "old_secret",
api_host: "example.com",
httparty_params: {
timeout: 10,
open_timeout: 20,
read_timeout: 30,
write_timeout: 40,
debug_output: true,
http_proxyaddr: "my.proxy.server",
http_proxyport: 8000,
http_proxyuser: "user",
http_proxypass: "pass",
},
)
assert_equal(
{
timeout: 10,
open_timeout: 20,
read_timeout: 30,
write_timeout: 40,
debug_output: true,
http_proxyaddr: "my.proxy.server",
http_proxyport: 8000,
http_proxyuser: "user",
http_proxypass: "pass",
},
ShopifyAPI::Context.httparty_params,
)
end

def test_with_nil_httparty_params_defaults_to_empty_hash
clear_context

ShopifyAPI::Context.setup(
api_key: "key",
api_secret_key: "secret",
api_version: "2023-10",
scope: ["scope1", "scope2"],
is_private: true,
is_embedded: true,
log_level: :off,
private_shop: "privateshop.myshopify.com",
user_agent_prefix: "user_agent_prefix1",
old_api_secret_key: "old_secret",
api_host: "example.com",
httparty_params: nil,
)
assert_equal({}, ShopifyAPI::Context.httparty_params)
end

def test_send_a_warning_if_log_level_is_invalid
ShopifyAPI::Context.stubs(:log_level).returns(:warn)
ShopifyAPI::Logger.expects(:warn).with("not_a_level is not a valid log_level. "\
Expand Down
Loading