1
|
|
|
"""Base script used across defined.""" |
2
|
|
|
|
3
|
|
|
import requests |
4
|
|
|
import paystackapi as api |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class Borg: |
8
|
|
|
"""Borg class making class attributes global""" |
9
|
|
|
_shared_state = {} |
10
|
|
|
|
11
|
|
|
def __init__(self): |
12
|
|
|
self.__dict__ = self._shared_state |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class PayStackBase(Borg): |
16
|
|
|
"""Base Class used across defined.""" |
17
|
|
|
|
18
|
|
|
def __init__(self, **kwargs): |
19
|
|
|
"""Initialize Paystack with secret key.""" |
20
|
|
|
Borg.__init__(self) |
21
|
|
|
secret_key = kwargs.get('secret_key', api.SECRET_KEY) |
22
|
|
|
authorization = kwargs.get( |
23
|
|
|
'authorization', |
24
|
|
|
api.HEADERS['Authorization'].format(secret_key)) |
25
|
|
|
headers = dict(Authorization=authorization) |
26
|
|
|
arguments = dict(api_url=api.API_URL, headers=headers) |
27
|
|
|
if not hasattr(self, 'requests'): |
28
|
|
|
req = PayStackRequests(**arguments) |
29
|
|
|
self._shared_state.update(requests=req) |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class PayStackRequests(object): |
33
|
|
|
|
34
|
|
|
def __init__(self, api_url='https://api.paystack.co/', |
35
|
|
|
headers=None): |
36
|
|
|
"""Initialize Paystack Request object for browsing resource. |
37
|
|
|
|
38
|
|
|
Args: |
39
|
|
|
api_url: str |
40
|
|
|
headers: dict |
41
|
|
|
""" |
42
|
|
|
self.API_BASE_URL = '{api_url}'.format(**locals()) |
43
|
|
|
self.headers = headers |
44
|
|
|
|
45
|
|
|
def _request(self, method, resource_uri, **kwargs): |
46
|
|
|
"""Perform a method on a resource. |
47
|
|
|
|
48
|
|
|
Args: |
49
|
|
|
method: requests.`method` |
50
|
|
|
resource_uri: resource endpoint |
51
|
|
|
Raises: |
52
|
|
|
HTTPError |
53
|
|
|
Returns: |
54
|
|
|
JSON Response |
55
|
|
|
""" |
56
|
|
|
data = kwargs.get('data') |
57
|
|
|
response = method(self.API_BASE_URL + resource_uri, |
58
|
|
|
json=data, headers=self.headers) |
59
|
|
|
response.raise_for_status() |
60
|
|
|
return response.json() |
61
|
|
|
|
62
|
|
|
def get(self, endpoint, **kwargs): |
63
|
|
|
"""Get a resource. |
64
|
|
|
|
65
|
|
|
Args: |
66
|
|
|
endpoint: resource endpoint. |
67
|
|
|
""" |
68
|
|
|
return self._request(requests.get, endpoint, **kwargs) |
69
|
|
|
|
70
|
|
|
def post(self, endpoint, **kwargs): |
71
|
|
|
"""Create a resource. |
72
|
|
|
|
73
|
|
|
Args: |
74
|
|
|
endpoint: resource endpoint. |
75
|
|
|
""" |
76
|
|
|
return self._request(requests.post, endpoint, **kwargs) |
77
|
|
|
|
78
|
|
|
def put(self, endpoint, **kwargs): |
79
|
|
|
"""Update a resource. |
80
|
|
|
|
81
|
|
|
Args: |
82
|
|
|
endpoint: resource endpoint. |
83
|
|
|
""" |
84
|
|
|
return self._request(requests.put, endpoint, **kwargs) |
85
|
|
|
|