Skip to content

Commit 816fe0d

Browse files
Add raw_request (#1431)
* Add raw request * Copy readme from beta * fix readme
1 parent 9cbc463 commit 816fe0d

File tree

6 files changed

+124
-0
lines changed

6 files changed

+124
-0
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ Metrics/MethodLength:
3838
Metrics/ModuleLength:
3939
Enabled: false
4040

41+
Metrics/ParameterLists:
42+
# There's 2 methods in `StripeClient` that have long parameter lists.
43+
Max: 8
44+
4145
Style/AccessModifierDeclarations:
4246
EnforcedStyle: inline
4347

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,23 @@ If your beta feature requires a `Stripe-Version` header to be sent, set the `Str
338338
Stripe.add_beta_version("feature_beta", "v3")
339339
```
340340

341+
### Custom requests
342+
343+
If you:
344+
345+
- would like to send a request to an undocumented API (for example you are in a private beta)
346+
- prefer to bypass the method definitions in the library and specify your request details directly,
347+
- used the method `Stripe::APIResource.request(...)` to specify your own requests, which will soon be broken
348+
349+
you can now use the `raw_request` method on `Stripe`.
350+
351+
```ruby
352+
resp = Stripe.raw_request(:post, "/v1/beta_endpoint", {param: 123}, {stripe_version: "2022-11-15; feature_beta=v3"})
353+
354+
# (Optional) resp is a StripeResponse. You can use `Stripe.deserialize` to get a StripeObject.
355+
deserialized_resp = Stripe.deserialize(resp.http_body)
356+
```
357+
341358
## Support
342359

343360
New features and bug fixes are released on the latest major version of the Stripe Ruby library. If you are on an older major version, we recommend that you upgrade to the latest in order to use the new features and bug fixes including those for security vulnerabilities. Older major versions of the package will continue to be available for use, but will not be receiving any updates.

lib/stripe.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,31 @@ def self.set_app_info(name, partner_id: nil, url: nil, version: nil)
117117
version: version,
118118
}
119119
end
120+
121+
class RawRequest
122+
include Stripe::APIOperations::Request
123+
124+
def initialize
125+
@opts = {}
126+
end
127+
128+
def execute(method, url, params = {}, opts = {})
129+
resp, = execute_resource_request(method, url, params, opts)
130+
131+
resp
132+
end
133+
end
134+
135+
# Sends a request to Stripe REST API
136+
def self.raw_request(method, url, params = {}, opts = {})
137+
req = RawRequest.new
138+
req.execute(method, url, params, opts)
139+
end
140+
141+
def self.deserialize(data)
142+
data = JSON.parse(data) if data.is_a?(String)
143+
Util.convert_to_stripe_object(data, {})
144+
end
120145
end
121146

122147
Stripe.log_level = ENV["STRIPE_LOG"] unless ENV["STRIPE_LOG"].nil?

lib/stripe/stripe_client.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ def self.maybe_gc_connection_managers
462462

463463
headers = request_headers(api_key, method)
464464
.update(Util.normalize_headers(headers))
465+
465466
url = api_url(path, api_base)
466467

467468
# Merge given query parameters with any already encoded in the path.
@@ -879,6 +880,7 @@ def self.maybe_gc_connection_managers
879880
end
880881

881882
headers["Stripe-Version"] = config.api_version if config.api_version
883+
882884
headers["Stripe-Account"] = config.stripe_account if config.stripe_account
883885

884886
user_agent = @system_profiler.user_agent

test/stripe/raw_request_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require File.expand_path("../test_helper", __dir__)
4+
5+
class RawRequestTest < Test::Unit::TestCase
6+
context "raw_request" do
7+
should "send get request and return a response" do
8+
expected_body = "{\"id\": \"acc_123\"}"
9+
req = nil
10+
11+
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acc_123")
12+
.with { |request| req = request }
13+
.to_return(body: expected_body)
14+
15+
resp = Stripe.raw_request(:get, "/v1/accounts/acc_123")
16+
17+
assert_equal expected_body, resp.http_body
18+
assert_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
19+
assert_equal Stripe::ApiVersion::CURRENT, req.headers["Stripe-Version"]
20+
end
21+
22+
should "send post request with body and return a response" do
23+
expected_body = "{\"id\": \"acc_123\"}"
24+
req = nil
25+
26+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acc_123")
27+
.with(body: "p1=1&p2=string")
28+
.with { |request| req = request }
29+
.to_return(body: expected_body)
30+
31+
resp = Stripe.raw_request(:post, "/v1/accounts/acc_123", { p1: 1, p2: "string" })
32+
33+
assert_equal expected_body, resp.http_body
34+
assert_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
35+
assert_equal Stripe::ApiVersion::CURRENT, req.headers["Stripe-Version"]
36+
end
37+
end
38+
end

test/stripe_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,42 @@ class StripeTest < Test::Unit::TestCase
134134
assert_equal "client", Stripe.client_id
135135
end
136136
end
137+
138+
context "deserialize" do
139+
should "deserializes string into known object" do
140+
expected_body = "{\"id\": \"acc_123\", \"object\": \"account\"}"
141+
142+
obj = Stripe.deserialize(expected_body)
143+
144+
assert_equal obj.class, Stripe::Account
145+
assert_equal obj.id, "acc_123"
146+
end
147+
148+
should "deserializes string into unknown object" do
149+
expected_body = "{\"id\": \"acc_123\", \"object\": \"unknown\"}"
150+
151+
obj = Stripe.deserialize(expected_body)
152+
153+
assert_equal obj.class, Stripe::StripeObject
154+
assert_equal obj.id, "acc_123"
155+
end
156+
157+
should "deserializes hash into known object" do
158+
expected_body = { "id" => "acc_123", "object" => "account" }
159+
160+
obj = Stripe.deserialize(expected_body)
161+
162+
assert_equal obj.class, Stripe::Account
163+
assert_equal obj.id, "acc_123"
164+
end
165+
166+
should "deserializes hash into unknown object" do
167+
expected_body = { "id" => "acc_123", "object" => "unknown" }
168+
169+
obj = Stripe.deserialize(expected_body)
170+
171+
assert_equal obj.class, Stripe::StripeObject
172+
assert_equal obj.id, "acc_123"
173+
end
174+
end
137175
end

0 commit comments

Comments
 (0)