Skip to content

Commit

Permalink
Add support for static deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Apr 2, 2019
1 parent bcec86e commit f04ac92
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 26 deletions.
22 changes: 21 additions & 1 deletion stripe/api_resources/abstract/api_resource.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import, division, print_function

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

Expand Down Expand Up @@ -43,3 +43,23 @@ def instance_url(self):
base = self.class_url()
extn = quote_plus(id)
return "%s/%s" % (base, extn)

@classmethod
def _static_request(
cls,
method,
url,
api_key=None,
idempotency_key=None,
stripe_version=None,
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
)
8 changes: 8 additions & 0 deletions stripe/api_resources/abstract/deletable_api_resource.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
from __future__ import absolute_import, division, print_function

from stripe import util
from stripe.api_resources.abstract.api_resource import APIResource
from stripe.six.moves.urllib.parse import quote_plus


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)

@util.class_method_variant("_cls_delete")
def delete(self, **params):
self.refresh_from(self.request("delete", self.instance_url(), params))
return self
23 changes: 2 additions & 21 deletions stripe/api_resources/abstract/updateable_api_resource.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
from __future__ import absolute_import, division, print_function

from stripe import api_requestor, util
from stripe import util
from stripe.api_resources.abstract.api_resource import APIResource
from stripe.six.moves.urllib.parse import quote_plus


class UpdateableAPIResource(APIResource):
@classmethod
def _modify(
cls,
url,
api_key=None,
idempotency_key=None,
stripe_version=None,
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("post", url, params, headers)
return util.convert_to_stripe_object(
response, api_key, stripe_version, stripe_account
)

@classmethod
def modify(cls, sid, **params):
url = "%s/%s" % (cls.class_url(), quote_plus(util.utf8(sid)))
return cls._modify(url, **params)
return cls._static_request("post", url, **params)

def save(self, idempotency_key=None):
updated_params = self.serialize(None)
Expand Down
3 changes: 2 additions & 1 deletion stripe/api_resources/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def retrieve(cls, id=None, api_key=None, **params):

@classmethod
def modify(cls, id=None, **params):
return cls._modify(cls._build_instance_url(id), **params)
url = cls._build_instance_url(id)
return cls._static_request("post", url, **params)

@classmethod
def _build_instance_url(cls, sid):
Expand Down
2 changes: 1 addition & 1 deletion stripe/api_resources/alipay_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def instance_url(self):
@classmethod
def modify(cls, customer, id, **params):
url = cls._build_instance_url(customer, id)
return cls._modify(url, **params)
return cls._static_request("post", url, **params)

@classmethod
def retrieve(
Expand Down
2 changes: 1 addition & 1 deletion stripe/api_resources/application_fee_refund.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _build_instance_url(cls, fee, sid):
@classmethod
def modify(cls, fee, sid, **params):
url = cls._build_instance_url(fee, sid)
return cls._modify(url, **params)
return cls._static_request("post", url, **params)

def instance_url(self):
return self._build_instance_url(self.fee, self.id)
Expand Down
21 changes: 21 additions & 0 deletions stripe/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, division, print_function

import functools
import hmac
import io
import logging
Expand Down Expand Up @@ -284,3 +285,23 @@ def populate_headers(idempotency_key):
if idempotency_key is not None:
return {"Idempotency-Key": idempotency_key}
return None


class class_method_variant(object):
def __init__(self, class_method_name):
self.class_method_name = class_method_name

def __call__(self, method):
self.method = method
return self

def __get__(self, obj=None, objtype=None):
@functools.wraps(self.method)
def _wrapper(*args, **kwargs):
if obj is not None:
return self.method(obj, *args, **kwargs)
else:
class_method = getattr(objtype, self.class_method_name)
return class_method(*args, **kwargs)

return _wrapper
19 changes: 18 additions & 1 deletion tests/api_resources/abstract/test_deletable_api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@ class TestDeletableAPIResource(object):
class MyDeletable(stripe.api_resources.abstract.DeletableAPIResource):
OBJECT_NAME = "mydeletable"

def test_delete(self, request_mock):
def test_delete_class(self, request_mock):
request_mock.stub_request(
"delete",
"/v1/mydeletables/mid",
{"id": "mid", "deleted": True},
rheaders={"request-id": "req_id"},
)

obj = self.MyDeletable.delete("mid")

request_mock.assert_requested("delete", "/v1/mydeletables/mid", {})
assert obj.deleted is True
assert obj.id == "mid"

assert obj.last_response is not None
assert obj.last_response.request_id == "req_id"

def test_delete_instance(self, request_mock):
request_mock.stub_request(
"delete",
"/v1/mydeletables/mid",
Expand Down

0 comments on commit f04ac92

Please sign in to comment.