| 1 |
import io |
| 2 |
from stripe._api_resource import APIResource |
| 3 |
from stripe._custom_method import custom_method |
| 4 |
from stripe._util import class_method_variant, sanitize_id |
| 5 |
|
| 6 |
|
| 7 |
class TestCustomMethod(object): |
| 8 |
@custom_method("do_stuff", http_verb="post", http_path="do_the_thing") |
| 9 |
@custom_method( |
| 10 |
"do_list_stuff", http_verb="get", http_path="do_the_list_thing" |
| 11 |
) |
| 12 |
@custom_method( |
| 13 |
"do_stream_stuff", |
| 14 |
http_verb="post", |
| 15 |
http_path="do_the_stream_thing", |
| 16 |
is_streaming=True, |
| 17 |
) |
| 18 |
class MyResource(APIResource): |
| 19 |
OBJECT_NAME = "myresource" |
| 20 |
|
| 21 |
def do_stuff(self, idempotency_key=None, **params): |
| 22 |
url = self.instance_url() + "/do_the_thing" |
| 23 |
self._request_and_refresh( |
| 24 |
"post", url, {**params, "idempotency_key": idempotency_key} |
| 25 |
) |
| 26 |
return self |
| 27 |
|
| 28 |
def do_stream_stuff(self, idempotency_key=None, **params): |
| 29 |
url = self.instance_url() + "/do_the_stream_thing" |
| 30 |
return self._request_stream( |
| 31 |
"post", url, {**params, "idempotency_key": idempotency_key} |
| 32 |
) |
| 33 |
|
| 34 |
@classmethod |
| 35 |
def _cls_do_stuff_new_codegen(cls, id, **params): |
| 36 |
return cls._static_request( |
| 37 |
"post", |
| 38 |
"/v1/myresources/{id}/do_the_thing".format(id=sanitize_id(id)), |
| 39 |
params=params, |
| 40 |
) |
| 41 |
|
| 42 |
@class_method_variant("_cls_do_stuff_new_codegen") |
| 43 |
def do_stuff_new_codegen(self, **params): |
| 44 |
return self._request( |
| 45 |
"post", |
| 46 |
"/v1/myresources/{id}/do_the_thing".format( |
| 47 |
id=sanitize_id(self.get("id")) |
| 48 |
), |
| 49 |
params=params, |
| 50 |
) |
| 51 |
|
| 52 |
def test_call_custom_method_class(self, http_client_mock): |
| 53 |
http_client_mock.stub_request( |
| 54 |
"post", |
| 55 |
path="/v1/myresources/mid/do_the_thing", |
| 56 |
rbody='{"id": "mid", "thing_done": true}', |
| 57 |
rheaders={"request-id": "req_id"}, |
| 58 |
) |
| 59 |
|
| 60 |
obj = self.MyResource.do_stuff("mid", foo="bar") |
| 61 |
|
| 62 |
http_client_mock.assert_requested( |
| 63 |
"post", |
| 64 |
path="/v1/myresources/mid/do_the_thing", |
| 65 |
post_data="foo=bar", |
| 66 |
) |
| 67 |
assert obj.thing_done is True |
| 68 |
|
| 69 |
def test_call_custom_list_method_class_paginates(self, http_client_mock): |
| 70 |
http_client_mock.stub_request( |
| 71 |
"get", |
| 72 |
path="/v1/myresources/mid/do_the_list_thing", |
| 73 |
query_string="param1=abc¶m2=def", |
| 74 |
rbody='{"object": "list", "url": "/v1/myresources/mid/do_the_list_thing", "has_more": true, "data": [{"id": "cus_1", "object": "customer"}, {"id": "cus_2", "object": "customer"}]}', |
| 75 |
rheaders={"request-id": "req_123"}, |
| 76 |
) |
| 77 |
|
| 78 |
resp = self.MyResource.do_list_stuff("mid", param1="abc", param2="def") |
| 79 |
|
| 80 |
http_client_mock.assert_requested( |
| 81 |
"get", |
| 82 |
path="/v1/myresources/mid/do_the_list_thing", |
| 83 |
query_string="param1=abc¶m2=def", |
| 84 |
) |
| 85 |
|
| 86 |
# Stub out the second request which will happen automatically. |
| 87 |
http_client_mock.stub_request( |
| 88 |
"get", |
| 89 |
path="/v1/myresources/mid/do_the_list_thing", |
| 90 |
query_string="param1=abc¶m2=def&starting_after=cus_2", |
| 91 |
rbody='{"object": "list", "url": "/v1/myresources/mid/do_the_list_thing", "has_more": false, "data": [{"id": "cus_3", "object": "customer"}]}', |
| 92 |
rheaders={"request-id": "req_123"}, |
| 93 |
) |
| 94 |
|
| 95 |
# Iterate through entire content |
| 96 |
ids = [] |
| 97 |
for i in resp.auto_paging_iter(): |
| 98 |
ids.append(i.id) |
| 99 |
|
| 100 |
# Explicitly assert that the pagination parameter were kept for the |
| 101 |
# second request along with the starting_after param. |
| 102 |
http_client_mock.assert_requested( |
| 103 |
"get", |
| 104 |
path="/v1/myresources/mid/do_the_list_thing", |
| 105 |
query_string="param1=abc¶m2=def&starting_after=cus_2", |
| 106 |
) |
| 107 |
|
| 108 |
assert ids == ["cus_1", "cus_2", "cus_3"] |
| 109 |
|
| 110 |
def test_call_custom_stream_method_class(self, http_client_mock): |
| 111 |
http_client_mock.stub_request( |
| 112 |
"post", |
| 113 |
path="/v1/myresources/mid/do_the_stream_thing", |
| 114 |
rbody=io.BytesIO(str.encode("response body")), |
| 115 |
rheaders={"request-id": "req_id"}, |
| 116 |
) |
| 117 |
|
| 118 |
resp = self.MyResource.do_stream_stuff("mid", foo="bar") |
| 119 |
|
| 120 |
http_client_mock.assert_requested( |
| 121 |
"post", |
| 122 |
path="/v1/myresources/mid/do_the_stream_thing", |
| 123 |
post_data="foo=bar", |
| 124 |
) |
| 125 |
|
| 126 |
body_content = resp.io.read() |
| 127 |
if hasattr(body_content, "decode"): |
| 128 |
body_content = body_content.decode("utf-8") |
| 129 |
|
| 130 |
assert body_content == "response body" |
| 131 |
|
| 132 |
def test_call_custom_method_class_with_object(self, http_client_mock): |
| 133 |
http_client_mock.stub_request( |
| 134 |
"post", |
| 135 |
path="/v1/myresources/mid/do_the_thing", |
| 136 |
rbody='{"id": "mid", "thing_done": true}', |
| 137 |
rheaders={"request-id": "req_id"}, |
| 138 |
) |
| 139 |
|
| 140 |
obj = self.MyResource.construct_from({"id": "mid"}, "mykey") |
| 141 |
self.MyResource.do_stuff(obj, foo="bar") |
| 142 |
|
| 143 |
http_client_mock.assert_requested( |
| 144 |
"post", |
| 145 |
path="/v1/myresources/mid/do_the_thing", |
| 146 |
post_data="foo=bar", |
| 147 |
) |
| 148 |
assert obj.thing_done is True |
| 149 |
|
| 150 |
def test_call_custom_stream_method_class_with_object( |
| 151 |
self, http_client_mock |
| 152 |
): |
| 153 |
http_client_mock.stub_request( |
| 154 |
"post", |
| 155 |
path="/v1/myresources/mid/do_the_stream_thing", |
| 156 |
rbody=io.BytesIO(str.encode("response body")), |
| 157 |
rheaders={"request-id": "req_id"}, |
| 158 |
) |
| 159 |
|
| 160 |
obj = self.MyResource.construct_from({"id": "mid"}, "mykey") |
| 161 |
resp = self.MyResource.do_stream_stuff(obj, foo="bar") |
| 162 |
|
| 163 |
http_client_mock.assert_requested( |
| 164 |
"post", |
| 165 |
path="/v1/myresources/mid/do_the_stream_thing", |
| 166 |
post_data="foo=bar", |
| 167 |
) |
| 168 |
|
| 169 |
body_content = resp.io.read() |
| 170 |
if hasattr(body_content, "decode"): |
| 171 |
body_content = body_content.decode("utf-8") |
| 172 |
|
| 173 |
assert body_content == "response body" |
| 174 |
|
| 175 |
def test_call_custom_method_instance(self, http_client_mock): |
| 176 |
http_client_mock.stub_request( |
| 177 |
"post", |
| 178 |
path="/v1/myresources/mid/do_the_thing", |
| 179 |
rbody='{"id": "mid", "thing_done": true}', |
| 180 |
rheaders={"request-id": "req_id"}, |
| 181 |
) |
| 182 |
|
| 183 |
obj = self.MyResource.construct_from({"id": "mid"}, "mykey") |
| 184 |
obj.do_stuff(foo="bar") |
| 185 |
|
| 186 |
http_client_mock.assert_requested( |
| 187 |
"post", |
| 188 |
path="/v1/myresources/mid/do_the_thing", |
| 189 |
post_data="foo=bar", |
| 190 |
) |
| 191 |
assert obj.thing_done is True |
| 192 |
|
| 193 |
def test_call_custom_stream_method_instance(self, http_client_mock): |
| 194 |
http_client_mock.stub_request( |
| 195 |
"post", |
| 196 |
path="/v1/myresources/mid/do_the_stream_thing", |
| 197 |
rbody=io.BytesIO(str.encode("response body")), |
| 198 |
rheaders={"request-id": "req_id"}, |
| 199 |
) |
| 200 |
|
| 201 |
obj = self.MyResource.construct_from({"id": "mid"}, "mykey") |
| 202 |
resp = obj.do_stream_stuff(foo="bar") |
| 203 |
|
| 204 |
http_client_mock.assert_requested( |
| 205 |
"post", |
| 206 |
path="/v1/myresources/mid/do_the_stream_thing", |
| 207 |
post_data="foo=bar", |
| 208 |
) |
| 209 |
|
| 210 |
body_content = resp.io.read() |
| 211 |
if hasattr(body_content, "decode"): |
| 212 |
body_content = body_content.decode("utf-8") |
| 213 |
|
| 214 |
assert body_content == "response body" |
| 215 |
|
| 216 |
def test_call_custom_method_class_special_fields(self, http_client_mock): |
| 217 |
http_client_mock.stub_request( |
| 218 |
"post", |
| 219 |
path="/v1/myresources/mid/do_the_thing", |
| 220 |
rbody='{"id": "mid", "thing_done": true}', |
| 221 |
rheaders={"request-id": "req_id"}, |
| 222 |
) |
| 223 |
|
| 224 |
self.MyResource.do_stuff( |
| 225 |
"mid", |
| 226 |
foo="bar", |
| 227 |
stripe_version="2017-08-15", |
| 228 |
api_key="APIKEY", |
| 229 |
idempotency_key="IdempotencyKey", |
| 230 |
stripe_account="Acc", |
| 231 |
) |
| 232 |
|
| 233 |
http_client_mock.assert_requested( |
| 234 |
"post", |
| 235 |
path="/v1/myresources/mid/do_the_thing", |
| 236 |
post_data="foo=bar", |
| 237 |
api_key="APIKEY", |
| 238 |
stripe_version="2017-08-15", |
| 239 |
stripe_account="Acc", |
| 240 |
idempotency_key="IdempotencyKey", |
| 241 |
) |
| 242 |
|
| 243 |
def test_call_custom_method_class_newcodegen_special_fields( |
| 244 |
self, http_client_mock |
| 245 |
): |
| 246 |
http_client_mock.stub_request( |
| 247 |
"post", |
| 248 |
path="/v1/myresources/mid/do_the_thing", |
| 249 |
rbody='{"id": "mid", "thing_done": true}', |
| 250 |
rheaders={"request-id": "req_id"}, |
| 251 |
) |
| 252 |
|
| 253 |
self.MyResource.do_stuff_new_codegen( |
| 254 |
"mid", |
| 255 |
foo="bar", |
| 256 |
stripe_version="2017-08-15", |
| 257 |
api_key="APIKEY", |
| 258 |
idempotency_key="IdempotencyKey", |
| 259 |
stripe_account="Acc", |
| 260 |
) |
| 261 |
|
| 262 |
http_client_mock.assert_requested( |
| 263 |
"post", |
| 264 |
path="/v1/myresources/mid/do_the_thing", |
| 265 |
post_data="foo=bar", |
| 266 |
api_key="APIKEY", |
| 267 |
stripe_version="2017-08-15", |
| 268 |
stripe_account="Acc", |
| 269 |
idempotency_key="IdempotencyKey", |
| 270 |
) |
| 271 |
|
| 272 |
def test_call_custom_method_instance_newcodegen_special_fields( |
| 273 |
self, http_client_mock |
| 274 |
): |
| 275 |
http_client_mock.stub_request( |
| 276 |
"post", |
| 277 |
path="/v1/myresources/mid/do_the_thing", |
| 278 |
rbody='{"id": "mid", "thing_done": true}', |
| 279 |
rheaders={"request-id": "req_id"}, |
| 280 |
) |
| 281 |
|
| 282 |
obj = self.MyResource.construct_from({"id": "mid"}, "mykey") |
| 283 |
obj.do_stuff_new_codegen( |
| 284 |
foo="bar", |
| 285 |
stripe_version="2017-08-15", |
| 286 |
api_key="APIKEY", |
| 287 |
idempotency_key="IdempotencyKey", |
| 288 |
stripe_account="Acc", |
| 289 |
headers={"extra_header": "val"}, |
| 290 |
) |
| 291 |
|
| 292 |
http_client_mock.assert_requested( |
| 293 |
"post", |
| 294 |
path="/v1/myresources/mid/do_the_thing", |
| 295 |
post_data="foo=bar", |
| 296 |
api_key="APIKEY", |
| 297 |
stripe_version="2017-08-15", |
| 298 |
stripe_account="Acc", |
| 299 |
idempotency_key="IdempotencyKey", |
| 300 |
extra_headers={"extra_header": "val"}, |
| 301 |
) |