From 4beaf0dfd4d105562d857f983291fc92ba11cc1d Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Thu, 19 Jul 2018 15:55:20 +0200 Subject: [PATCH] Better `class_url` method --- stripe/api_resources/abstract/api_resource.py | 12 +++++------- .../api_resources/abstract/singleton_api_resource.py | 10 ++++++++-- stripe/api_resources/application_fee.py | 4 ---- stripe/api_resources/country_spec.py | 4 ---- stripe/api_resources/ephemeral_key.py | 4 ---- stripe/api_resources/exchange_rate.py | 4 ---- stripe/api_resources/file_upload.py | 4 ++-- stripe/api_resources/issuer_fraud_record.py | 4 ---- stripe/api_resources/order_return.py | 4 ---- stripe/api_resources/payment_intent.py | 4 ---- stripe/api_resources/subscription_item.py | 4 ---- tests/api_resources/abstract/test_api_resource.py | 2 +- .../abstract/test_createable_api_resource.py | 2 +- .../abstract/test_deletable_api_resource.py | 2 +- .../abstract/test_listable_api_resource.py | 2 +- .../abstract/test_nested_resource_class_methods.py | 2 +- .../abstract/test_singleton_api_resource.py | 2 +- .../abstract/test_updateable_api_resource.py | 2 +- 18 files changed, 22 insertions(+), 50 deletions(-) diff --git a/stripe/api_resources/abstract/api_resource.py b/stripe/api_resources/abstract/api_resource.py index 04d49fa14..b263e5c28 100644 --- a/stripe/api_resources/abstract/api_resource.py +++ b/stripe/api_resources/abstract/api_resource.py @@ -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') diff --git a/stripe/api_resources/abstract/singleton_api_resource.py b/stripe/api_resources/abstract/singleton_api_resource.py index c02cf5455..01407d95b 100644 --- a/stripe/api_resources/abstract/singleton_api_resource.py +++ b/stripe/api_resources/abstract/singleton_api_resource.py @@ -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() diff --git a/stripe/api_resources/application_fee.py b/stripe/api_resources/application_fee.py index 8b7923e24..e269d3427 100644 --- a/stripe/api_resources/application_fee.py +++ b/stripe/api_resources/application_fee.py @@ -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' diff --git a/stripe/api_resources/country_spec.py b/stripe/api_resources/country_spec.py index ddc68f722..db06129c7 100644 --- a/stripe/api_resources/country_spec.py +++ b/stripe/api_resources/country_spec.py @@ -5,7 +5,3 @@ class CountrySpec(abstract.ListableAPIResource): OBJECT_NAME = 'country_spec' - - @classmethod - def class_name(cls): - return 'country_spec' diff --git a/stripe/api_resources/ephemeral_key.py b/stripe/api_resources/ephemeral_key.py index 9ee0b29d1..d76725d6f 100644 --- a/stripe/api_resources/ephemeral_key.py +++ b/stripe/api_resources/ephemeral_key.py @@ -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, diff --git a/stripe/api_resources/exchange_rate.py b/stripe/api_resources/exchange_rate.py index 56935c25c..63c824591 100644 --- a/stripe/api_resources/exchange_rate.py +++ b/stripe/api_resources/exchange_rate.py @@ -5,7 +5,3 @@ class ExchangeRate(ListableAPIResource): OBJECT_NAME = 'exchange_rate' - - @classmethod - def class_name(cls): - return 'exchange_rate' diff --git a/stripe/api_resources/file_upload.py b/stripe/api_resources/file_upload.py index 22fa0e780..c596873c8 100644 --- a/stripe/api_resources/file_upload.py +++ b/stripe/api_resources/file_upload.py @@ -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, diff --git a/stripe/api_resources/issuer_fraud_record.py b/stripe/api_resources/issuer_fraud_record.py index 32f6ec9d5..27bf4e6c6 100644 --- a/stripe/api_resources/issuer_fraud_record.py +++ b/stripe/api_resources/issuer_fraud_record.py @@ -3,7 +3,3 @@ class IssuerFraudRecord(ListableAPIResource): OBJECT_NAME = 'issuer_fraud_record' - - @classmethod - def class_name(cls): - return 'issuer_fraud_record' diff --git a/stripe/api_resources/order_return.py b/stripe/api_resources/order_return.py index be578e338..c8cebf5a1 100644 --- a/stripe/api_resources/order_return.py +++ b/stripe/api_resources/order_return.py @@ -5,7 +5,3 @@ class OrderReturn(ListableAPIResource): OBJECT_NAME = 'order_return' - - @classmethod - def class_url(cls): - return '/v1/order_returns' diff --git a/stripe/api_resources/payment_intent.py b/stripe/api_resources/payment_intent.py index 0aaa7160a..3b9df9f0f 100644 --- a/stripe/api_resources/payment_intent.py +++ b/stripe/api_resources/payment_intent.py @@ -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) diff --git a/stripe/api_resources/subscription_item.py b/stripe/api_resources/subscription_item.py index fbfc4f574..8b3e3761a 100644 --- a/stripe/api_resources/subscription_item.py +++ b/stripe/api_resources/subscription_item.py @@ -9,7 +9,3 @@ class SubscriptionItem(CreateableAPIResource, DeletableAPIResource, UpdateableAPIResource, ListableAPIResource): OBJECT_NAME = 'subscription_item' - - @classmethod - def class_name(cls): - return 'subscription_item' diff --git a/tests/api_resources/abstract/test_api_resource.py b/tests/api_resources/abstract/test_api_resource.py index 779edf2f3..aaed8b2f0 100644 --- a/tests/api_resources/abstract/test_api_resource.py +++ b/tests/api_resources/abstract/test_api_resource.py @@ -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' diff --git a/tests/api_resources/abstract/test_createable_api_resource.py b/tests/api_resources/abstract/test_createable_api_resource.py index 5b2c7c676..1e892d271 100644 --- a/tests/api_resources/abstract/test_createable_api_resource.py +++ b/tests/api_resources/abstract/test_createable_api_resource.py @@ -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( diff --git a/tests/api_resources/abstract/test_deletable_api_resource.py b/tests/api_resources/abstract/test_deletable_api_resource.py index 155845d3c..b4c8219bc 100644 --- a/tests/api_resources/abstract/test_deletable_api_resource.py +++ b/tests/api_resources/abstract/test_deletable_api_resource.py @@ -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( diff --git a/tests/api_resources/abstract/test_listable_api_resource.py b/tests/api_resources/abstract/test_listable_api_resource.py index da618b097..8cd00d7da 100644 --- a/tests/api_resources/abstract/test_listable_api_resource.py +++ b/tests/api_resources/abstract/test_listable_api_resource.py @@ -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( diff --git a/tests/api_resources/abstract/test_nested_resource_class_methods.py b/tests/api_resources/abstract/test_nested_resource_class_methods.py index 24f93691b..461d983ce 100644 --- a/tests/api_resources/abstract/test_nested_resource_class_methods.py +++ b/tests/api_resources/abstract/test_nested_resource_class_methods.py @@ -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( diff --git a/tests/api_resources/abstract/test_singleton_api_resource.py b/tests/api_resources/abstract/test_singleton_api_resource.py index a447843d5..ddfdf3ac6 100644 --- a/tests/api_resources/abstract/test_singleton_api_resource.py +++ b/tests/api_resources/abstract/test_singleton_api_resource.py @@ -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( diff --git a/tests/api_resources/abstract/test_updateable_api_resource.py b/tests/api_resources/abstract/test_updateable_api_resource.py index 9924c29e6..b8f271f17 100644 --- a/tests/api_resources/abstract/test_updateable_api_resource.py +++ b/tests/api_resources/abstract/test_updateable_api_resource.py @@ -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):