Skip to content

Commit

Permalink
Add support for stripe 2.64.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bhch committed Dec 31, 2021
1 parent a4993a6 commit 492d507
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 13 deletions.
9 changes: 8 additions & 1 deletion async_stripe/api_resources/abstract/custom_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ async def custom_method_request(cls, sid, **params):
quote_plus(util.utf8(sid)),
http_path,
)
return await cls._static_request(http_verb, url, **params)
obj = await cls._static_request(http_verb, url, **params)

# For list objects, we have to attach the parameters so that they
# can be referenced in auto-pagination and ensure consistency.
if "object" in obj and obj.object == "list":
obj._retrieve_params = params

return obj

async def custom_method_request_stream(cls, sid, **params):
url = "%s/%s/%s" % (
Expand Down
7 changes: 4 additions & 3 deletions async_stripe/api_resources/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ async def delete_discount_patch(self, **params):
async def list_payment_methods_patch(self, idempotency_key=None, **params):
url = self.instance_url() + "/payment_methods"
headers = util.populate_headers(idempotency_key)
self.refresh_from(await self.request("get", url, params, headers))
return self

resp = await self.request("get", url, params, headers)
stripe_object = util.convert_to_stripe_object(resp)
stripe_object._retrieve_params = params
return stripe_object

stripe.Customer.delete_discount = delete_discount_patch
stripe.Customer.list_payment_methods = list_payment_methods_patch
Expand Down
12 changes: 8 additions & 4 deletions async_stripe/api_resources/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ async def finalize_quote_patch(self, idempotency_key=None, **params):
async def list_computed_upfront_line_items_patch(self, idempotency_key=None, **params):
url = self.instance_url() + "/computed_upfront_line_items"
headers = util.populate_headers(idempotency_key)
self.refresh_from(await self.request("get", url, params, headers))
return self
resp = await self.request("get", url, params, headers)
stripe_object = util.convert_to_stripe_object(resp)
stripe_object._retrieve_params = params
return stripe_object

async def list_line_items_patch(self, idempotency_key=None, **params):
url = self.instance_url() + "/line_items"
headers = util.populate_headers(idempotency_key)
self.refresh_from(await self.request("get", url, params, headers))
return self
resp = await self.request("get", url, params, headers)
stripe_object = util.convert_to_stripe_object(resp)
stripe_object._retrieve_params = params
return stripe_object

async def _cls_pdf_patch(
cls,
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ keywords = ["stripe", "async", "asynchronous", "wrapper"]

[tool.poetry.dependencies]
python = "^3.6"
stripe = "2.63.0"
stripe = "2.64.0"
tornado = ">=5.1"

[tool.poetry.dev-dependencies]
Expand Down
66 changes: 66 additions & 0 deletions tests/api_resources/abstract/test_custom_method.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import, division, print_function

import stripe
from async_stripe.api_resources.abstract import patch_custom_methods

import pytest

Expand All @@ -14,6 +15,9 @@ class TestCustomMethod(object):
@stripe.api_resources.abstract.custom_method(
"do_stuff", http_verb="post", http_path="do_the_thing"
)
@stripe.api_resources.abstract.custom_method(
"do_list_stuff", http_verb="get", http_path="do_the_list_thing"
)
@stripe.api_resources.abstract.custom_method(
"do_stream_stuff",
http_verb="post",
Expand All @@ -34,6 +38,14 @@ async def do_stream_stuff(self, idempotency_key=None, **params):
headers = util.populate_headers(idempotency_key)
return await self.request_stream("post", url, params, headers)

# For testing, we need to patch the custom methods to make them async
custom_resources = [
{"name": "do_stuff", "http_verb": "post", "http_path": "do_the_thing"},
{"name": "do_list_stuff", "http_verb": "get", "http_path": "do_the_list_thing"},
{"name": "do_stream_stuff", "http_verb": "post", "http_path": "do_the_stream_thing", "is_streaming": True},
]
patch_custom_methods(MyResource, custom_resources)

async def test_call_custom_method_class(self, request_mock):
request_mock.stub_request(
"post",
Expand All @@ -49,6 +61,60 @@ async def test_call_custom_method_class(self, request_mock):
)
assert obj.thing_done is True

async def test_call_custom_list_method_class_paginates(self, request_mock):
request_mock.stub_request(
"get",
"/v1/myresources/mid/do_the_list_thing",
{
"object": "list",
"url": "/v1/myresources/mid/do_the_list_thing",
"has_more": True,
"data": [
{"id": "cus_1", "object": "customer"},
{"id": "cus_2", "object": "customer"},
],
},
rheaders={"request-id": "req_123"},
)

resp = await self.MyResource.do_list_stuff("mid", param1="abc", param2="def")

request_mock.assert_requested(
"get",
"/v1/myresources/mid/do_the_list_thing",
{"param1": "abc", "param2": "def"},
)

# Stub out the second request which will happen automatically.
request_mock.stub_request(
"get",
"/v1/myresources/mid/do_the_list_thing",
{
"object": "list",
"url": "/v1/myresources/mid/do_the_list_thing",
"has_more": False,
"data": [
{"id": "cus_3", "object": "customer"},
],
},
rheaders={"request-id": "req_123"},
)

# Iterate through entire content
ids = []
async for i in resp.auto_paging_iter():
ids.append(i.id)

# Explicitly assert that the pagination parameter were kept for the
# second request along with the starting_after param.
request_mock.assert_requested(
"get",
"/v1/myresources/mid/do_the_list_thing",
{"starting_after": "cus_2", "param1": "abc", "param2": "def"},
)

assert ids == ["cus_1", "cus_2", "cus_3"]

async def test_call_custom_stream_method_class(self, request_mock):
request_mock.stub_request_stream(
"post",
Expand Down
10 changes: 10 additions & 0 deletions tests/api_resources/test_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,13 @@ async def test_is_listable(self, request_mock):
request_mock.assert_requested(
"get", "/v1/customers/%s/payment_methods" % TEST_RESOURCE_ID
)

async def test_is_listable_on_object(self, request_mock):
customer = await stripe.Customer.retrieve(
TEST_RESOURCE_ID
)
resource = await customer.list_payment_methods(TEST_RESOURCE_ID, type="card")
request_mock.assert_requested(
"get", "/v1/customers/%s/payment_methods" % TEST_RESOURCE_ID
)
assert isinstance(resource, stripe.ListObject)

0 comments on commit 492d507

Please sign in to comment.