Skip to content

Commit

Permalink
Update code and tests to support stripe-python 6.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bhch committed Aug 27, 2023
1 parent 3943c94 commit 023d6fb
Show file tree
Hide file tree
Showing 22 changed files with 64 additions and 105 deletions.
6 changes: 3 additions & 3 deletions async_stripe/api_requestor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import stripe
from stripe import error, version, util, six
from stripe import error, version, util
from stripe.multipart_data_generator import MultipartDataGenerator
from stripe.six.moves.urllib.parse import urlencode
from urllib.parse import urlencode

from stripe.api_requestor import APIRequestor
from stripe.api_requestor import _build_api_url, _api_encode
Expand Down Expand Up @@ -139,7 +139,7 @@ async def request_raw_patch(self, method, url, params=None, supplied_headers=Non

headers = self.request_headers(my_api_key, method)
if supplied_headers is not None:
for key, value in six.iteritems(supplied_headers):
for key, value in supplied_headers.items():
headers[key] = value

util.log_info("Request to Stripe api", method=method, path=abs_url)
Expand Down
6 changes: 3 additions & 3 deletions async_stripe/api_resources/abstract/custom_method.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from stripe import util
from stripe.six.moves.urllib.parse import quote_plus
from urllib.parse import quote_plus



Expand Down Expand Up @@ -27,7 +27,7 @@ def _patch(cls, name, http_verb, http_path, is_streaming):
async def custom_method_request(cls, sid, **params):
url = "%s/%s/%s" % (
cls.class_url(),
quote_plus(util.utf8(sid)),
quote_plus(sid),
http_path,
)
obj = await cls._static_request(http_verb, url, params=params)
Expand All @@ -42,7 +42,7 @@ async def custom_method_request(cls, sid, **params):
async def custom_method_request_stream(cls, sid, **params):
url = "%s/%s/%s" % (
cls.class_url(),
quote_plus(util.utf8(sid)),
quote_plus(sid),
http_path,
)
return await cls._static_request_stream(http_verb, url, params=params)
Expand Down
3 changes: 0 additions & 3 deletions async_stripe/api_resources/abstract/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from stripe import error, util, six
from stripe.six.moves.urllib.parse import quote_plus
from stripe.api_resources.abstract.test_helpers import APIResourceTestHelpers
from async_stripe.api_resources.abstract.api_resource import APIResource


# Some resource classes have a nested TestHelpers class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from stripe import util
from stripe.six.moves.urllib.parse import quote_plus
from stripe.api_resources.abstract.updateable_api_resource import (
UpdateableAPIResource,
)
Expand Down
3 changes: 0 additions & 3 deletions async_stripe/api_resources/account.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# -*- coding: utf-8 -*-
import stripe
from stripe import oauth, six
from stripe import util
from stripe.six.moves.urllib.parse import quote_plus
from async_stripe.api_resources.abstract import patch_nested_resources


Expand Down
4 changes: 0 additions & 4 deletions async_stripe/api_resources/list_object.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# -*- coding: utf-8 -*-
import stripe
from stripe import six, util
from stripe.stripe_object import StripeObject

from stripe.six.moves.urllib.parse import quote_plus


async def auto_paging_iter_patch(self):
Expand Down
4 changes: 2 additions & 2 deletions async_stripe/api_resources/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import stripe
from stripe import util
from stripe import api_requestor
from stripe.six.moves.urllib.parse import quote_plus
from urllib.parse import quote_plus


async def _cls_pdf_patch(
Expand All @@ -16,7 +16,7 @@ async def _cls_pdf_patch(
):
url = "%s/%s/%s" % (
cls.class_url(),
quote_plus(util.utf8(sid)),
quote_plus(sid),
"pdf",
)
requestor = api_requestor.APIRequestor(
Expand Down
4 changes: 3 additions & 1 deletion async_stripe/api_resources/search_result_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ async def next_search_result_page_patch(
params_with_filters.update({"page": self.next_page})
params_with_filters.update(params)

return await self.search(
result = await self.search(
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
**params_with_filters
)
assert isinstance(result, stripe.SearchResultObject)
return result


stripe.SearchResultObject.auto_paging_iter = auto_paging_iter_patch
Expand Down
6 changes: 3 additions & 3 deletions async_stripe/api_resources/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
from stripe import error
from stripe import util
from stripe.api_resources import Customer
from stripe.six.moves.urllib.parse import quote_plus
from urllib.parse import quote_plus


async def detach_patch(self, idempotency_key=None, **params):
token = util.utf8(self.id)
token = self.id

if hasattr(self, "customer") and self.customer:
extn = quote_plus(token)
customer = util.utf8(self.customer)
customer = self.customer
base = Customer.class_url()
owner_extn = quote_plus(customer)
url = "%s/%s/sources/%s" % (base, owner_extn, extn)
Expand Down
11 changes: 6 additions & 5 deletions tests/api_resources/abstract/test_test_helpers_api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class TestTestHelperAPIResource(object):
@stripe.api_resources.abstract.test_helpers
class MyTestHelpersResource(stripe.api_resources.abstract.APIResource):
OBJECT_NAME = "myresource"

Expand All @@ -34,6 +33,12 @@ async def do_stuff(self, idempotency_key=None, **params):
[{"name": "do_stuff", "http_verb": "post", "http_path": "do_the_thing"}]
)

@property
def test_helpers(self):
return self.TestHelpers(self)

MyTestHelpersResource.TestHelpers._resource_cls = MyTestHelpersResource

async def test_call_custom_method_class(self, request_mock):
request_mock.stub_request(
"post",
Expand Down Expand Up @@ -68,7 +73,3 @@ async def test_call_custom_method_instance_via_property(self, request_mock):
{"foo": "bar"},
)
assert obj.thing_done is True

def test_helper_decorator_raises_for_non_resource(self):
with pytest.raises(ValueError):
stripe.api_resources.abstract.test_helpers(str)
7 changes: 5 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import pytest

import stripe
from stripe.six.moves.urllib.request import urlopen
from stripe.six.moves.urllib.error import HTTPError
from urllib.request import urlopen
from urllib.error import HTTPError

from tests.request_mock import RequestMock
from tests.stripe_mock import StripeMock
Expand Down Expand Up @@ -78,18 +78,21 @@ def pytest_runtest_setup(item):
def setup_stripe():
orig_attrs = {
"api_base": stripe.api_base,
"upload_api_base": stripe.upload_api_base,
"api_key": stripe.api_key,
"client_id": stripe.client_id,
"default_http_client": stripe.default_http_client,
}
http_client = stripe.http_client.new_default_http_client()
stripe.api_base = "http://localhost:%s" % MOCK_PORT
stripe.upload_api_base = "http://localhost:%s" % MOCK_PORT
stripe.api_key = "sk_test_123"
stripe.client_id = "ca_123"
stripe.default_http_client = http_client
yield
http_client.close()
stripe.api_base = orig_attrs["api_base"]
stripe.upload_api_base = orig_attrs["upload_api_base"]
stripe.api_key = orig_attrs["api_key"]
stripe.client_id = orig_attrs["client_id"]
stripe.default_http_client = orig_attrs["default_http_client"]
Expand Down
4 changes: 2 additions & 2 deletions tests/request_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json

import stripe
from stripe import six, util
from stripe import util
from stripe.stripe_response import StripeResponse, StripeStreamResponse


Expand Down Expand Up @@ -187,7 +187,7 @@ def get_response(self, method, url, expect_stream=False):
if expect_stream != is_streaming:
return None

if not isinstance(rbody, six.string_types):
if not isinstance(rbody, str):
rbody = json.dumps(rbody)
if is_streaming:
stripe_response = StripeStreamResponse(
Expand Down
10 changes: 5 additions & 5 deletions tests/test_api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import pytest

import stripe
from stripe import six, util
from stripe import util
from stripe.stripe_response import StripeResponse, StripeStreamResponse

from stripe.six.moves.urllib.parse import urlsplit
from urllib.parse import urlsplit

import urllib3

Expand Down Expand Up @@ -124,7 +124,7 @@ def _x_stripe_ua_handles_failed_platform_function(self, other):
return True

def _extra_match(self, other):
for k, v in six.iteritems(self.extra):
for k, v in self.extra.items():
if other[k] != v:
return False

Expand Down Expand Up @@ -212,7 +212,7 @@ class TestAPIRequestor(object):
],
"list": [("%s[0]", 1), ("%s[1]", "foo"), ("%s[2]", "baz")],
"string": [("%s", "boo")],
"unicode": [("%s", stripe.util.utf8(u"\u1234"))],
"unicode": [("%s", u"\u1234")],
"datetime": [("%s", 1356994801)],
"none": [],
}
Expand Down Expand Up @@ -334,7 +334,7 @@ async def test_param_encoding(self, requestor, mock_response, check_call):
await requestor.request("get", "", self.ENCODE_INPUTS)

expectation = []
for type_, values in six.iteritems(self.ENCODE_EXPECTATIONS):
for type_, values in self.ENCODE_EXPECTATIONS.items():
expectation.extend([(k % (type_,), str(v)) for k, v in values])

check_call("get", QueryMatcher(expectation))
Expand Down
57 changes: 14 additions & 43 deletions tests/test_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,32 @@

from __future__ import absolute_import, division, print_function

from stripe import six, error
from stripe import error


class TestStripeError(object):
def test_formatting(self):
err = error.StripeError(u"öre")
assert six.text_type(err) == u"öre"
if six.PY2:
assert str(err) == "\xc3\xb6re"
else:
assert str(err) == u"öre"
assert str(err) == u"öre"

def test_formatting_with_request_id(self):
err = error.StripeError(u"öre", headers={"request-id": "123"})
assert six.text_type(err) == u"Request 123: öre"
if six.PY2:
assert str(err) == "Request 123: \xc3\xb6re"
else:
assert str(err) == u"Request 123: öre"
assert str(err) == u"Request 123: öre"

def test_formatting_with_none(self):
err = error.StripeError(None, headers={"request-id": "123"})
assert six.text_type(err) == u"Request 123: <empty message>"
if six.PY2:
assert str(err) == "Request 123: <empty message>"
else:
assert str(err) == "Request 123: <empty message>"
assert str(err) == "Request 123: <empty message>"

def test_formatting_with_message_none_and_request_id_none(self):
err = error.StripeError(None)
assert six.text_type(err) == u"<empty message>"
if six.PY2:
assert str(err) == "<empty message>"
else:
assert str(err) == u"<empty message>"
assert str(err) == u"<empty message>"

def test_repr(self):
err = error.StripeError(u"öre", headers={"request-id": "123"})
if six.PY2:
assert (
repr(err)
== "StripeError(message=u'\\xf6re', http_status=None, "
"request_id='123')"
)
else:
assert (
repr(err) == "StripeError(message='öre', http_status=None, "
"request_id='123')"
)
assert (
repr(err) == "StripeError(message='öre', http_status=None, "
"request_id='123')"
)

def test_error_object(self):
err = error.StripeError(
Expand All @@ -74,17 +51,11 @@ def test_repr(self):
http_status=403,
headers={"request-id": "123"},
)
if six.PY2:
assert (
repr(err) == "CardError(message=u'\\xf6re', param='cparam', "
"code='ccode', http_status=403, request_id='123')"
)
else:
assert (
repr(err)
== "CardError(message='öre', param='cparam', code='ccode', "
"http_status=403, request_id='123')"
)
assert (
repr(err)
== "CardError(message='öre', param='cparam', code='ccode', "
"http_status=403, request_id='123')"
)


class TestApiConnectionError(object):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_generated_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2984,4 +2984,11 @@ async def test_creditnote_preview_lines(self, request_mock):
request_mock.assert_requested(
"get",
"/v1/credit_notes/preview/lines",
)

async def test_quote_pdf(self, request_mock):
await stripe.Quote.pdf("qt_xxxxxxxxxxxxx")
request_mock.assert_requested_stream(
"get",
"/v1/quotes/qt_xxxxxxxxxxxxx/pdf",
)
2 changes: 1 addition & 1 deletion tests/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import stripe
import urllib3
from stripe import six, util
from stripe import util

from async_stripe.http_client import TornadoAsyncHTTPClient

Expand Down
Loading

0 comments on commit 023d6fb

Please sign in to comment.