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

Next major release changes #845

Merged
merged 7 commits into from
Aug 1, 2022
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
7 changes: 0 additions & 7 deletions stripe/api_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@

from stripe.api_resources.account import Account
from stripe.api_resources.account_link import AccountLink
from stripe.api_resources.alipay_account import AlipayAccount
from stripe.api_resources.apple_pay_domain import ApplePayDomain
from stripe.api_resources.application_fee import ApplicationFee
from stripe.api_resources.application_fee_refund import ApplicationFeeRefund
from stripe.api_resources.balance import Balance
from stripe.api_resources.balance_transaction import BalanceTransaction
from stripe.api_resources.bank_account import BankAccount
from stripe.api_resources.bitcoin_receiver import BitcoinReceiver
from stripe.api_resources.bitcoin_transaction import BitcoinTransaction
from stripe.api_resources.capability import Capability
from stripe.api_resources.card import Card
from stripe.api_resources.cash_balance import CashBalance
Expand All @@ -54,7 +51,6 @@
from stripe.api_resources.invoice import Invoice
from stripe.api_resources.invoice_item import InvoiceItem
from stripe.api_resources.invoice_line_item import InvoiceLineItem
from stripe.api_resources.issuer_fraud_record import IssuerFraudRecord
from stripe.api_resources.line_item import LineItem
from stripe.api_resources.login_link import LoginLink
from stripe.api_resources.mandate import Mandate
Expand All @@ -69,8 +65,6 @@
from stripe.api_resources.product import Product
from stripe.api_resources.promotion_code import PromotionCode
from stripe.api_resources.quote import Quote
from stripe.api_resources.recipient import Recipient
from stripe.api_resources.recipient_transfer import RecipientTransfer
from stripe.api_resources.refund import Refund
from stripe.api_resources.reversal import Reversal
from stripe.api_resources.review import Review
Expand All @@ -86,7 +80,6 @@
from stripe.api_resources.tax_code import TaxCode
from stripe.api_resources.tax_id import TaxId
from stripe.api_resources.tax_rate import TaxRate
from stripe.api_resources.three_d_secure import ThreeDSecure
from stripe.api_resources.token import Token
from stripe.api_resources.topup import Topup
from stripe.api_resources.transfer import Transfer
Expand Down
68 changes: 63 additions & 5 deletions stripe/api_resources/abstract/api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def retrieve(cls, id, api_key=None, **params):
return instance

def refresh(self):
self.refresh_from(self.request("get", self.instance_url()))
return self
return self._request_and_refresh("get", self.instance_url())

@classmethod
def class_url(cls):
Expand Down Expand Up @@ -44,6 +43,65 @@ def instance_url(self):
extn = quote_plus(id)
return "%s/%s" % (base, extn)

# The `method_` and `url_` arguments are suffixed with an underscore to
# avoid conflicting with actual request parameters in `params`.
def _request(
self,
method_,
url_,
api_key=None,
idempotency_key=None,
stripe_version=None,
stripe_account=None,
headers=None,
params=None,
):
obj = StripeObject._request(
self,
method_,
url_,
api_key,
idempotency_key,
stripe_version,
stripe_account,
headers,
params,
)

if type(self) is type(obj):
self.refresh_from(obj)
return self
else:
return obj

# The `method_` and `url_` arguments are suffixed with an underscore to
# avoid conflicting with actual request parameters in `params`.
def _request_and_refresh(
self,
method_,
url_,
api_key=None,
idempotency_key=None,
stripe_version=None,
stripe_account=None,
headers=None,
params=None,
):
obj = StripeObject._request(
self,
method_,
url_,
api_key,
idempotency_key,
stripe_version,
stripe_account,
headers,
params,
)

self.refresh_from(obj)
return self

# The `method_` and `url_` arguments are suffixed with an underscore to
# avoid conflicting with actual request parameters in `params`.
@classmethod
Expand All @@ -55,15 +113,15 @@ def _static_request(
idempotency_key=None,
stripe_version=None,
stripe_account=None,
**params
params=None,
):
requestor = api_requestor.APIRequestor(
api_key, api_version=stripe_version, account=stripe_account
)
headers = util.populate_headers(idempotency_key)
response, api_key = requestor.request(method_, url_, params, headers)
return util.convert_to_stripe_object(
response, api_key, stripe_version, stripe_account
response, api_key, stripe_version, stripe_account, params
)

# The `method_` and `url_` arguments are suffixed with an underscore to
Expand All @@ -77,7 +135,7 @@ def _static_request_stream(
idempotency_key=None,
stripe_version=None,
stripe_account=None,
**params
params=None,
):
requestor = api_requestor.APIRequestor(
api_key, api_version=stripe_version, account=stripe_account
Expand Down
18 changes: 8 additions & 10 deletions stripe/api_resources/abstract/createable_api_resource.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import absolute_import, division, print_function

from stripe.api_resources.abstract.api_resource import APIResource
from stripe import api_requestor, util


class CreateableAPIResource(APIResource):
Expand All @@ -14,13 +13,12 @@ def create(
stripe_account=None,
**params
):
requestor = api_requestor.APIRequestor(
api_key, api_version=stripe_version, account=stripe_account
)
url = cls.class_url()
headers = util.populate_headers(idempotency_key)
response, api_key = requestor.request("post", url, params, headers)

return util.convert_to_stripe_object(
response, api_key, stripe_version, stripe_account
return cls._static_request(
"post",
cls.class_url(),
api_key,
idempotency_key,
stripe_version,
stripe_account,
params,
)
4 changes: 2 additions & 2 deletions stripe/api_resources/abstract/custom_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def custom_method_request(cls, sid, **params):
quote_plus(util.utf8(sid)),
http_path,
)
obj = cls._static_request(http_verb, url, **params)
obj = cls._static_request(http_verb, url, params=params)

# For list objects, we have to attach the parameters so that they
# can be referenced in auto-pagination and ensure consistency.
Expand All @@ -35,7 +35,7 @@ def custom_method_request_stream(cls, sid, **params):
quote_plus(util.utf8(sid)),
http_path,
)
return cls._static_request_stream(http_verb, url, **params)
return cls._static_request_stream(http_verb, url, params=params)

if is_streaming:
class_method_impl = classmethod(custom_method_request_stream)
Expand Down
7 changes: 4 additions & 3 deletions stripe/api_resources/abstract/deletable_api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ class DeletableAPIResource(APIResource):
@classmethod
def _cls_delete(cls, sid, **params):
url = "%s/%s" % (cls.class_url(), quote_plus(util.utf8(sid)))
return cls._static_request("delete", url, **params)
return cls._static_request("delete", url, params=params)

@util.class_method_variant("_cls_delete")
def delete(self, **params):
self.refresh_from(self.request("delete", self.instance_url(), params))
return self
return self._request_and_refresh(
"delete", self.instance_url(), params=params
)
20 changes: 7 additions & 13 deletions stripe/api_resources/abstract/listable_api_resource.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import absolute_import, division, print_function

from stripe import api_requestor, util
from stripe.api_resources.abstract.api_resource import APIResource


Expand All @@ -13,16 +12,11 @@ def auto_paging_iter(cls, *args, **params):
def list(
cls, api_key=None, stripe_version=None, stripe_account=None, **params
):
requestor = api_requestor.APIRequestor(
api_key,
api_base=cls.api_base(),
api_version=stripe_version,
account=stripe_account,
return cls._static_request(
"get",
cls.class_url(),
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
params=params,
)
url = cls.class_url()
response, api_key = requestor.request("get", url, params)
stripe_object = util.convert_to_stripe_object(
response, api_key, stripe_version, stripe_account
)
stripe_object._retrieve_params = params
return stripe_object
18 changes: 10 additions & 8 deletions stripe/api_resources/abstract/nested_resource_class_methods.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import absolute_import, division, print_function

from stripe import api_requestor, util
from stripe.six.moves.urllib.parse import quote_plus

from stripe.api_resources.abstract import APIResource


def nested_resource_class_methods(
resource, path=None, operations=None, resource_plural=None
Expand Down Expand Up @@ -38,13 +39,14 @@ def nested_resource_request(
stripe_account=None,
**params
):
requestor = api_requestor.APIRequestor(
api_key, api_version=stripe_version, account=stripe_account
)
headers = util.populate_headers(idempotency_key)
response, api_key = requestor.request(method, url, params, headers)
return util.convert_to_stripe_object(
response, api_key, stripe_version, stripe_account
return APIResource._static_request(
method,
url,
api_key=api_key,
idempotency_key=idempotency_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
params=params,
)

resource_request_method = "%ss_request" % resource
Expand Down
19 changes: 7 additions & 12 deletions stripe/api_resources/abstract/searchable_api_resource.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import absolute_import, division, print_function

from stripe import api_requestor, util
from stripe.api_resources.abstract.api_resource import APIResource


Expand All @@ -14,15 +13,11 @@ def _search(
stripe_account=None,
**params
):
requestor = api_requestor.APIRequestor(
api_key,
api_base=cls.api_base(),
api_version=stripe_version,
account=stripe_account,
return cls._static_request(
"get",
search_url,
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
params=params,
)
response, api_key = requestor.request("get", search_url, params)
stripe_object = util.convert_to_stripe_object(
response, api_key, stripe_version, stripe_account
)
stripe_object._retrieve_params = params
return stripe_object
13 changes: 6 additions & 7 deletions stripe/api_resources/abstract/updateable_api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ class UpdateableAPIResource(APIResource):
@classmethod
def modify(cls, sid, **params):
url = "%s/%s" % (cls.class_url(), quote_plus(util.utf8(sid)))
return cls._static_request("post", url, **params)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, what is the purpose of removing api_key support from the modify method?
How could I call modify without passing api_key explicitly?

return cls._static_request("post", url, params=params)

def save(self, idempotency_key=None):
updated_params = self.serialize(None)
headers = util.populate_headers(idempotency_key)

if updated_params:
self.refresh_from(
self.request(
"post", self.instance_url(), updated_params, headers
)
self._request_and_refresh(
"post",
self.instance_url(),
idempotency_key=idempotency_key,
params=updated_params,
)
else:
util.logger.debug("Trying to save already saved object %r", self)
Expand Down
8 changes: 3 additions & 5 deletions stripe/api_resources/abstract/verify_mixin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from __future__ import absolute_import, division, print_function

from stripe import util


class VerifyMixin(object):
def verify(self, idempotency_key=None, **params):
url = self.instance_url() + "/verify"
headers = util.populate_headers(idempotency_key)
self.refresh_from(self.request("post", url, params, headers))
return self
return self._request(
"post", url, idempotency_key=idempotency_key, params=params
)
Loading