Skip to content

Commit

Permalink
Better class_url method
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Jul 19, 2018
1 parent b4a27c7 commit 4beaf0d
Show file tree
Hide file tree
Showing 18 changed files with 22 additions and 50 deletions.
12 changes: 5 additions & 7 deletions stripe/api_resources/abstract/api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ def refresh(self):
return self

@classmethod
def class_name(cls):
def class_url(cls):
if cls == APIResource:
raise NotImplementedError(
'APIResource is an abstract class. You should perform '
'actions on its subclasses (e.g. Charge, Customer)')
return str(quote_plus(cls.__name__.lower()))

@classmethod
def class_url(cls):
cls_name = cls.class_name()
return "/v1/%ss" % (cls_name,)
# Namespaces are separated in object names with periods (.) and in URLs
# with forward slashes (/), so replace the former with the latter.
base = cls.OBJECT_NAME.replace('.', '/')
return "/v1/%ss" % (base,)

def instance_url(self):
id = self.get('id')
Expand Down
10 changes: 8 additions & 2 deletions stripe/api_resources/abstract/singleton_api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ def retrieve(cls, **params):

@classmethod
def class_url(cls):
cls_name = cls.class_name()
return "/v1/%s" % (cls_name,)
if cls == SingletonAPIResource:
raise NotImplementedError(
'SingletonAPIResource is an abstract class. You should '
'perform actions on its subclasses (e.g. Balance)')
# Namespaces are separated in object names with periods (.) and in URLs
# with forward slashes (/), so replace the former with the latter.
base = cls.OBJECT_NAME.replace('.', '/')
return "/v1/%s" % (base,)

def instance_url(self):
return self.class_url()
4 changes: 0 additions & 4 deletions stripe/api_resources/application_fee.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
class ApplicationFee(ListableAPIResource):
OBJECT_NAME = 'application_fee'

@classmethod
def class_name(cls):
return 'application_fee'

def refund(self, idempotency_key=None, **params):
headers = util.populate_headers(idempotency_key)
url = self.instance_url() + '/refund'
Expand Down
4 changes: 0 additions & 4 deletions stripe/api_resources/country_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@

class CountrySpec(abstract.ListableAPIResource):
OBJECT_NAME = 'country_spec'

@classmethod
def class_name(cls):
return 'country_spec'
4 changes: 0 additions & 4 deletions stripe/api_resources/ephemeral_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
class EphemeralKey(DeletableAPIResource):
OBJECT_NAME = 'ephemeral_key'

@classmethod
def class_name(cls):
return 'ephemeral_key'

@classmethod
def create(cls, api_key=None, idempotency_key=None,
stripe_version=None, stripe_account=None,
Expand Down
4 changes: 0 additions & 4 deletions stripe/api_resources/exchange_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@

class ExchangeRate(ListableAPIResource):
OBJECT_NAME = 'exchange_rate'

@classmethod
def class_name(cls):
return 'exchange_rate'
4 changes: 2 additions & 2 deletions stripe/api_resources/file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def api_base(cls):
return stripe.upload_api_base

@classmethod
def class_name(cls):
return 'file'
def class_url(cls):
return '/v1/files'

@classmethod
def create(cls, api_key=None, api_version=None, stripe_account=None,
Expand Down
4 changes: 0 additions & 4 deletions stripe/api_resources/issuer_fraud_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@

class IssuerFraudRecord(ListableAPIResource):
OBJECT_NAME = 'issuer_fraud_record'

@classmethod
def class_name(cls):
return 'issuer_fraud_record'
4 changes: 0 additions & 4 deletions stripe/api_resources/order_return.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@

class OrderReturn(ListableAPIResource):
OBJECT_NAME = 'order_return'

@classmethod
def class_url(cls):
return '/v1/order_returns'
4 changes: 0 additions & 4 deletions stripe/api_resources/payment_intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ class PaymentIntent(CreateableAPIResource, UpdateableAPIResource,
ListableAPIResource):
OBJECT_NAME = 'payment_intent'

@classmethod
def class_name(cls):
return 'payment_intent'

def cancel(self, idempotency_key=None, **params):
url = self.instance_url() + '/cancel'
headers = util.populate_headers(idempotency_key)
Expand Down
4 changes: 0 additions & 4 deletions stripe/api_resources/subscription_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@
class SubscriptionItem(CreateableAPIResource, DeletableAPIResource,
UpdateableAPIResource, ListableAPIResource):
OBJECT_NAME = 'subscription_item'

@classmethod
def class_name(cls):
return 'subscription_item'
2 changes: 1 addition & 1 deletion tests/api_resources/abstract/test_api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class TestAPIResource(object):
class MyResource(stripe.api_resources.abstract.APIResource):
pass
OBJECT_NAME = 'myresource'

def test_retrieve_and_refresh(self, request_mock):
url = '/v1/myresources/foo%2A'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class TestCreateableAPIResource(object):
class MyCreatable(stripe.api_resources.abstract.CreateableAPIResource):
pass
OBJECT_NAME = 'mycreatable'

def test_create(self, request_mock):
request_mock.stub_request(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class TestDeletableAPIResource(object):
class MyDeletable(stripe.api_resources.abstract.DeletableAPIResource):
pass
OBJECT_NAME = 'mydeletable'

def test_delete(self, request_mock):
request_mock.stub_request(
Expand Down
2 changes: 1 addition & 1 deletion tests/api_resources/abstract/test_listable_api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class TestListableAPIResource(object):
class MyListable(stripe.api_resources.abstract.ListableAPIResource):
pass
OBJECT_NAME = 'mylistable'

def test_all(self, request_mock):
request_mock.stub_request(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TestNestedResourceClassMethods(object):
operations=['create', 'retrieve', 'update', 'delete', 'list']
)
class MainResource(stripe.api_resources.abstract.APIResource):
pass
OBJECT_NAME = 'mainresource'

def test_create_nested(self, request_mock):
request_mock.stub_request(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class TestSingletonAPIResource(object):
class MySingleton(stripe.api_resources.abstract.SingletonAPIResource):
pass
OBJECT_NAME = 'mysingleton'

def test_retrieve(self, request_mock):
request_mock.stub_request(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class TestUpdateableAPIResource(object):
class MyUpdateable(stripe.api_resources.abstract.UpdateableAPIResource):
pass
OBJECT_NAME = 'myupdateable'

@pytest.fixture
def obj(self, request_mock):
Expand Down

0 comments on commit 4beaf0d

Please sign in to comment.