diff --git a/omise/__init__.py b/omise/__init__.py index c318116..d11bc59 100644 --- a/omise/__init__.py +++ b/omise/__init__.py @@ -29,6 +29,7 @@ def iteritems(d, **kw): 'Account', 'Balance', 'BankAccount', + 'Capability', 'Card', 'Charge', 'Collection', @@ -64,6 +65,7 @@ def _get_class_for(type): 'account': Account, 'balance': Balance, 'bank_account': BankAccount, + 'capability': Capability, 'card': Card, 'charge': Charge, 'customer': Customer, @@ -216,6 +218,13 @@ def _request(cls, *args, **kwargs): return Request(api_public, api_vault, api_version).send(*args, **kwargs) +class _PublicResource(Base): + + @classmethod + def _request(cls, *args, **kwargs): + return Request(api_public, api_main, api_version).send(*args, **kwargs) + + class Account(_MainResource, Base): """API class representing accounts details. @@ -343,6 +352,42 @@ def __repr__(self): hex(id(self))) +class Capability(_PublicResource, Base): + """API class representing capability details. + + This API class is used for retrieving the account capabilities. It requires + the public key to be set in ``omise.api_public``. + + Basic usage:: + + >>> import omise + >>> omise.api_public = 'pkey_test_4xs8breq32civvobx15' + >>> capability = omise.Capability.retrieve() + + >>> capability.zero_interest_installments + True + """ + + @classmethod + def _instance_path(cls, *args): + return 'capability' + + @classmethod + def retrieve(cls): + """Retrieve the account capabilities. + + :rtype: Capability + """ + return _as_object(cls._request('get', cls._instance_path())) + + def reload(self): + """Reload the capability details. + + :rtype: Capability + """ + return self._reload_data(self._request('get', self._instance_path())) + + class Token(_VaultResource, Base): """API class for creating and retrieving credit card token with the API. diff --git a/omise/test/test_capability.py b/omise/test/test_capability.py new file mode 100644 index 0000000..8781b19 --- /dev/null +++ b/omise/test/test_capability.py @@ -0,0 +1,119 @@ +import mock +import unittest + +from .helper import _ResourceMixin + + +class CapabilityTest(_ResourceMixin, unittest.TestCase): + + def _getTargetClass(self): + from .. import Capability + return Capability + + def _makeOne(self): + return self._getTargetClass().from_data({ + 'object': 'capability', + 'location': '/capability', + 'payment_methods': [ + { + "object": "payment_method", + "name": "card", + "currencies": [ + "THB", + "JPY", + "USD", + "EUR", + "GBP", + "SGD", + "AUD", + "CHF", + "CNY", + "DKK", + "HKD" + ], + "card_brands": [ + "JCB", + "Visa", + "MasterCard" + ], + "installment_terms": None + }, + { + "object": "payment_method", + "name": "alipay", + "currencies": [ + "THB" + ], + "card_brands": None, + "installment_terms": None + } + ], + 'country': 'TH', + 'zero_interest_installments': True + }) + + @mock.patch('requests.get') + def test_reload(self, api_call): + capability = self._makeOne() + class_ = self._getTargetClass() + + self.assertTrue(isinstance(capability, class_)) + self.assertTrue(capability.zero_interest_installments) + + self.mockResponse(api_call, """{ + "zero_interest_installments": false + }""") + + capability.reload() + self.assertFalse(capability.zero_interest_installments) + self.assertRequest(api_call, 'https://api.omise.co/capability') + + @mock.patch('requests.get') + def test_retrieve(self, api_call): + class_ = self._getTargetClass() + self.mockResponse(api_call, """{ + "object": "capability", + "location": "/capability", + "payment_methods": [ + { + "object": "payment_method", + "name": "card", + "currencies": [ + "THB", + "JPY", + "USD", + "EUR", + "GBP", + "SGD", + "AUD", + "CHF", + "CNY", + "DKK", + "HKD" + ], + "card_brands": [ + "JCB", + "Visa", + "MasterCard" + ], + "installment_terms": null + }, + { + "object": "payment_method", + "name": "alipay", + "currencies": [ + "THB" + ], + "card_brands": null, + "installment_terms": null + } + ], + "country": "TH", + "zero_interest_installments": true + }""") + + capability = class_.retrieve() + self.assertTrue(isinstance(capability, class_)) + self.assertEqual(capability.country, 'TH') + self.assertTrue(capability.zero_interest_installments) + self.assertRequest(api_call, 'https://api.omise.co/capability')