Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 072600b

Browse files
authored
code clean up (#16)
* code clean up * also add python 3.10 support * update README * bump version
1 parent 26c8753 commit 072600b

File tree

7 files changed

+101
-55
lines changed

7 files changed

+101
-55
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Client library for Mopé by Hakrinbank
1010

1111

1212
### Requirements
13-
- Python >=3.6,<=3.9
13+
- Python >= 3.6,<=3.10
1414
- requests (currently not sure what the min required version is)
1515
- Mopé API Tokens
1616

mope/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__author__ = '[email protected]'
2-
__version__ = '0.0.5'
2+
__version__ = '0.0.6'

mope/client.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
1+
from urllib.parse import urljoin
2+
13
import requests
24

35
from mope.__version__ import __version__
46
from mope.resources.shop import ShopResource
57

8+
_user_agent = 'Python Mope %s' % __version__
69

7-
class Mope:
8-
def __init__(self, token=None):
10+
11+
class Client:
12+
def __init__(self, token):
913
self._token = token
10-
self._headers = {}
11-
self.url_base = 'https://api.mope.sr/api'
14+
self._headers = {
15+
'User-Agent': _user_agent,
16+
'Content-Type': 'application/json',
17+
'Authorization': 'Bearer %s' % token,
18+
}
19+
self._base_url = 'https://api.mope.sr/api'
1220

13-
self._make_headers(token, user_agent='Python Mope %s' % __version__)
21+
def call_api(self, method, endpoint, params=None, data=None, json=None):
22+
url = urljoin(self._base_url, endpoint)
23+
return requests.request(
24+
method=method,
25+
url=url,
26+
params=params,
27+
data=data,
28+
json=json,
29+
headers=self._headers,
30+
)
1431

15-
self.shop = ShopResource(self)
1632

17-
def _make_headers(self, token, user_agent):
33+
class Mope:
34+
def __init__(self, token=None):
35+
self._token = token
1836
self._headers = {
19-
'User-Agent': user_agent,
37+
'User-Agent': _user_agent,
2038
'Content-Type': 'application/json',
2139
'Authorization': 'Bearer %s' % token,
2240
}
41+
self.base_url = 'https://api.mope.sr/api'
2342

24-
def call_api(self, method, endpoint, params=None, data=None, json=None):
25-
return requests.request(method=method, url=endpoint, params=params, data=data, json=json, headers=self._headers)
43+
self._client = Client(token)
44+
45+
self.shop = ShopResource(self._client)
2646

2747
def __str__(self):
28-
return 'Python Mope %s' % __version__
48+
return _user_agent

mope/models/payments.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CreatePaymentResponse(BaseModel):
2121

2222
@classmethod
2323
def from_json(cls, data: Dict[str, Any]):
24-
return cls(data['id'], data['url'])
24+
return cls(id=data['id'], url=data['url'])
2525

2626

2727
@dataclass
@@ -32,7 +32,11 @@ class Currency(BaseModel):
3232

3333
@classmethod
3434
def from_json(cls, data: Dict[str, str]):
35-
return cls(data['name'], data['code'], data['symbol'])
35+
return cls(
36+
name=data['name'],
37+
code=data['code'],
38+
symbol=data['symbol'],
39+
)
3640

3741

3842
@dataclass
@@ -48,6 +52,13 @@ class PaymentRequest(BaseModel):
4852

4953
@classmethod
5054
def from_json(cls, data: Dict[str, Any]):
51-
return cls(data['id'], data['amount'], data['created_at'],
52-
data['description'], data['expires_at'], Currency.from_json(data['currency']),
53-
data['status'], data['meta_data'])
55+
return cls(
56+
id=data['id'],
57+
amount=data['amount'],
58+
created_at=data['created_at'],
59+
description=data['description'],
60+
expires_at=data['expires_at'],
61+
currency=Currency.from_json(data['currency']),
62+
status=data['status'],
63+
meta_data=data['meta_data'],
64+
)

mope/resources/shop.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@ class ShopResource:
55
def __init__(self, client):
66
self._client = client
77

8-
def create_payment_request(self, amount: int, description: str,
9-
order_id: str, currency: str, redirect_url: str) -> CreatePaymentResponse:
10-
endpoint = '%s/shop/payment_request' % self._client.url_base
8+
def create_payment_request(
9+
self, amount: int,
10+
description: str,
11+
order_id: str,
12+
currency: str,
13+
redirect_url: str,
14+
) -> CreatePaymentResponse:
15+
"""
16+
makes a payment request
17+
"""
18+
19+
endpoint = 'shop/payment_request'
1120
response = self._client.call_api('POST', endpoint=endpoint, json={
1221
'amount': amount,
1322
'order_id': order_id,
@@ -19,7 +28,11 @@ def create_payment_request(self, amount: int, description: str,
1928
return CreatePaymentResponse.from_json(response.json())
2029

2130
def get_payment_request(self, payment_id: str) -> PaymentRequest:
22-
endpoint = '%s/shop/payment_request/%s' % (self._client.url_base, payment_id)
31+
"""
32+
fetches a payment request given a payment ID
33+
"""
34+
35+
endpoint = 'shop/payment_request/%s' % payment_id
2336
response = self._client.call_api('GET', endpoint=endpoint)
2437
response.raise_for_status()
2538
return PaymentRequest.from_json(response.json())

setup.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,29 @@
1111
if PYTHON36:
1212
install_requires.append('dataclasses')
1313

14-
setup(name='python-mope',
15-
version=__version__,
16-
description='A client library for interacting with Mopé Web Shop APIs',
17-
long_description=long_description,
18-
long_description_content_type='text/markdown',
19-
author='Lemuel Boyce',
20-
author_email='[email protected]',
21-
packages=find_packages(exclude=["tests"]),
22-
url='https://github.com/rhymiz/python-mope',
23-
include_package_data=True,
24-
zip_safe=True,
25-
license='MIT',
26-
python_requires='>=3.6,<=3.9',
27-
install_requires=install_requires,
28-
classifiers=[
29-
"Programming Language :: Python :: 3",
30-
"Programming Language :: Python :: 3.6",
31-
"Programming Language :: Python :: 3.7",
32-
"Programming Language :: Python :: 3.8",
33-
"Programming Language :: Python :: 3.9",
34-
"License :: OSI Approved :: MIT License",
35-
"Operating System :: OS Independent",
36-
])
14+
setup(
15+
name='python-mope',
16+
version=__version__,
17+
description='A client library for interacting with the Mopé Web Shop API',
18+
long_description=long_description,
19+
long_description_content_type='text/markdown',
20+
author='Lemuel Boyce',
21+
author_email='[email protected]',
22+
packages=find_packages(exclude=["tests"]),
23+
url='https://github.com/rhymiz/python-mope',
24+
include_package_data=True,
25+
zip_safe=True,
26+
license='MIT',
27+
python_requires='>=3.6,<=3.10',
28+
install_requires=install_requires,
29+
classifiers=[
30+
"Programming Language :: Python :: 3",
31+
"Programming Language :: Python :: 3.6",
32+
"Programming Language :: Python :: 3.7",
33+
"Programming Language :: Python :: 3.8",
34+
"Programming Language :: Python :: 3.9",
35+
"Programming Language :: Python :: 3.10",
36+
"License :: OSI Approved :: MIT License",
37+
"Operating System :: OS Independent",
38+
],
39+
)

tests/test_mope.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
'created_at': '2019-12-02T14:28:14+00:00',
1313
'description': 'Order for a lot of beer',
1414
'expires_at': '2019-12-03T14:28:14+00:00',
15-
1615
'currency': {
1716
'name': 'Surinamese dollar',
1817
'code': 'SRD',
@@ -27,10 +26,11 @@ class MopeTestCase(unittest.TestCase):
2726

2827
def setUp(self) -> None:
2928
self.mope = Mope()
29+
self.client = getattr(self.mope, '_client')
3030

3131
@mock.patch('requests.request')
3232
def test_call_api(self, mock_request):
33-
self.mope.call_api('POST', 'https://api.mope.sr/api', json={'amount': 1000})
33+
self.client.call_api('POST', 'https://api.mope.sr/api', json={'amount': 1000})
3434

3535
mock_request.assert_called_once_with(
3636
url='https://api.mope.sr/api',
@@ -54,24 +54,23 @@ def test_shop_create_payment_request(self, mock_request):
5454
currency='SRD',
5555
redirect_url='https://www.sup.com/products/1',
5656
)
57-
5857
mock_request.assert_called_once_with(
59-
url='https://api.mope.sr/api/shop/payment_request',
58+
method='POST',
59+
url='https://api.mope.sr/shop/payment_request',
60+
params=None,
6061
data=None,
6162
json={
6263
'amount': 1000,
6364
'order_id': 'test-1',
6465
'currency': 'SRD',
6566
'description': 'test',
66-
'redirect_url': 'https://www.sup.com/products/1'
67+
'redirect_url': 'https://www.sup.com/products/1',
6768
},
68-
method='POST',
69-
params=None,
7069
headers={
71-
'User-Agent': 'Python Mope %s' % __version__,
70+
'User-Agent': 'Python Mope 0.0.5',
7271
'Content-Type': 'application/json',
73-
'Authorization': 'Bearer None'
74-
},
72+
'Authorization': 'Bearer None',
73+
}
7574
)
7675

7776
@mock.patch('requests.request')

0 commit comments

Comments
 (0)