Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update class custom methods to save list object parameters. #757

Merged
merged 1 commit into from
Dec 21, 2021
Merged
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
9 changes: 8 additions & 1 deletion stripe/api_resources/abstract/custom_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ def custom_method_request(cls, sid, **params):
quote_plus(util.utf8(sid)),
http_path,
)
return cls._static_request(http_verb, url, **params)
obj = 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

def custom_method_request_stream(cls, sid, **params):
url = "%s/%s/%s" % (
Expand Down
57 changes: 57 additions & 0 deletions tests/api_resources/abstract/test_custom_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 Down Expand Up @@ -43,6 +46,60 @@ def test_call_custom_method_class(self, request_mock):
)
assert obj.thing_done is True

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 = 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 = []
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"]

def test_call_custom_stream_method_class(self, request_mock):
request_mock.stub_request_stream(
"post",
Expand Down