Skip to content

Commit 978c053

Browse files
committed
add optional ShopifyAPI::Context::httparty_params
Allows to override HTTP request params such as timeout, debug_output, or proxy settings.
1 parent 013563e commit 978c053

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Note: For changes to the API, see https://shopify.dev/changelog?filter=api
44
## Unreleased
5+
- [#1375](https://github.com/Shopify/shopify-api-ruby/pull/1375) add optional ShopifyAPI::Context::httparty_params
56

67
## 14.9.0
78

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ ShopifyAPI::Context.setup(
6161
)
6262
```
6363

64+
Optionally, you can override HTTP request params such as timeout, debug_output, or proxy settings.
65+
```ruby
66+
ShopifyAPI::Context.setup(
67+
# other params...
68+
httparty_params: {
69+
timeout: 60, # Set the open/read/write timeout for HTTP requests
70+
open_timeout: 60, # Set the open timeout for HTTP requests
71+
read_timeout: 60, # Set the read timeout for HTTP requests
72+
write_timeout: 60, # Set the write timeout for HTTP requests
73+
debug_output: false, # Set to true to enable debug output for HTTP requests
74+
http_proxyaddr: "http://proxy.example.com:8080", # Set the HTTP proxy address
75+
http_proxyport: 8080, # Set the HTTP proxy port
76+
http_proxyuser: "username", # Set the HTTP proxy username
77+
http_proxypass: "password", # Set the HTTP proxy password
78+
}
79+
)
80+
```
81+
6482
### Performing OAuth
6583

6684
You need to go through OAuth as described [here](https://shopify.dev/docs/apps/auth/oauth) to create sessions for shops using your app.

lib/shopify_api/clients/http_client.rb

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def request(request, response_as_struct: false)
5151
res = T.cast(HTTParty.send(
5252
request.http_method,
5353
parsed_uri.to_s,
54+
**Context.httparty_params,
5455
headers: headers,
5556
query: request.query,
5657
body: request.body.class == Hash ? T.unsafe(request.body).to_json : request.body,

lib/shopify_api/context.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Context
2626

2727
@rest_resource_loader = T.let(nil, T.nilable(Zeitwerk::Loader))
2828

29+
@httparty_params = T.let({}, T::Hash[Symbol, T.untyped])
30+
2931
class << self
3032
extend T::Sig
3133

@@ -47,6 +49,7 @@ class << self
4749
api_host: T.nilable(String),
4850
response_as_struct: T.nilable(T::Boolean),
4951
rest_disabled: T.nilable(T::Boolean),
52+
httparty_params: T::Hash[Symbol, T.untyped]
5053
).void
5154
end
5255
def setup(
@@ -65,7 +68,8 @@ def setup(
6568
old_api_secret_key: nil,
6669
api_host: nil,
6770
response_as_struct: false,
68-
rest_disabled: false
71+
rest_disabled: false,
72+
httparty_params: {}
6973
)
7074
unless ShopifyAPI::AdminVersions::SUPPORTED_ADMIN_VERSIONS.include?(api_version)
7175
raise Errors::UnsupportedVersionError,
@@ -93,6 +97,7 @@ def setup(
9397
end
9498

9599
load_rest_resources(api_version: api_version)
100+
@httparty_params = httparty_params
96101
end
97102

98103
sig { params(api_version: String).void }
@@ -148,6 +153,9 @@ def private?
148153
sig { returns(T.nilable(String)) }
149154
attr_reader :private_shop, :user_agent_prefix, :old_api_secret_key, :host, :api_host
150155

156+
sig { returns(T::Hash[Symbol, T.untyped]) }
157+
attr_reader :httparty_params
158+
151159
sig { returns(T::Boolean) }
152160
def embedded?
153161
@is_embedded

test/context_test.rb

+34
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def test_setup
4646
assert_equal("http", ShopifyAPI::Context.host_scheme)
4747
assert_equal("localhost", ShopifyAPI::Context.host_name)
4848
assert_equal("example.com", ShopifyAPI::Context.api_host)
49+
assert_equal({}, ShopifyAPI::Context.httparty_params)
4950

5051
ShopifyAPI::Context.setup(
5152
api_key: "key",
@@ -146,6 +147,39 @@ def test_with_host_name_and_no_host_env
146147
ENV["HOST"] = old_host
147148
end
148149

150+
def test_with_httparty_params
151+
clear_context
152+
153+
ShopifyAPI::Context.setup(
154+
api_key: "key",
155+
api_secret_key: "secret",
156+
api_version: "2023-10",
157+
scope: ["scope1", "scope2"],
158+
is_private: true,
159+
is_embedded: true,
160+
log_level: :off,
161+
private_shop: "privateshop.myshopify.com",
162+
user_agent_prefix: "user_agent_prefix1",
163+
old_api_secret_key: "old_secret",
164+
api_host: "example.com",
165+
httparty_params: {
166+
http_proxyaddr: "my.proxy.server",
167+
http_proxyport: 8000,
168+
http_proxyuser: "user",
169+
http_proxypass: "pass",
170+
},
171+
)
172+
assert_equal(
173+
{
174+
http_proxyaddr: "my.proxy.server",
175+
http_proxyport: 8000,
176+
http_proxyuser: "user",
177+
http_proxypass: "pass",
178+
},
179+
ShopifyAPI::Context.httparty_params,
180+
)
181+
end
182+
149183
def test_send_a_warning_if_log_level_is_invalid
150184
ShopifyAPI::Context.stubs(:log_level).returns(:warn)
151185
ShopifyAPI::Logger.expects(:warn).with("not_a_level is not a valid log_level. "\

0 commit comments

Comments
 (0)