From 829b8ec9251d52b1a9b004070387506469dc4774 Mon Sep 17 00:00:00 2001 From: Dominic Charley-Roy Date: Tue, 21 Dec 2021 09:32:45 -0500 Subject: [PATCH] Update class custom methods to save list object parameters. --- .../api_resources/abstract/custom_method.py | 9 ++- .../abstract/test_custom_method.py | 57 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/stripe/api_resources/abstract/custom_method.py b/stripe/api_resources/abstract/custom_method.py index 114f2fefa..ca0cf93d0 100644 --- a/stripe/api_resources/abstract/custom_method.py +++ b/stripe/api_resources/abstract/custom_method.py @@ -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" % ( diff --git a/tests/api_resources/abstract/test_custom_method.py b/tests/api_resources/abstract/test_custom_method.py index c3e2c38df..f02a87e9e 100644 --- a/tests/api_resources/abstract/test_custom_method.py +++ b/tests/api_resources/abstract/test_custom_method.py @@ -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", @@ -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",