|
1 | | -from typing import Any, Dict, Tuple |
2 | 1 | import requests |
| 2 | +from typing import Dict |
3 | 3 | from datetime import datetime |
4 | 4 |
|
| 5 | +from .config import ( |
| 6 | + MONOBANK_CLIENT_INFO_URI, |
| 7 | + MONOBANK_CURRENCY_URI, |
| 8 | + MONOBANK_STATEMENT_URI, |
| 9 | + MONOBANK_WEBHOOK_URI, |
| 10 | +) |
| 11 | + |
5 | 12 |
|
6 | 13 | class MonoManager: |
| 14 | + def __init__(self, token=None): |
| 15 | + self._token = token |
| 16 | + |
| 17 | + _mono_currency_uri = MONOBANK_CURRENCY_URI |
| 18 | + _mono_client_info_uri = MONOBANK_CLIENT_INFO_URI |
| 19 | + _mono_statement_uri = MONOBANK_STATEMENT_URI |
| 20 | + _mono_webhook_uri = MONOBANK_WEBHOOK_URI |
| 21 | + |
| 22 | + @property |
| 23 | + def token(self) -> str: |
| 24 | + return self._token |
| 25 | + |
| 26 | + @token.setter |
| 27 | + def token(self, new_token): |
| 28 | + self._token = new_token |
| 29 | + |
| 30 | + @property |
| 31 | + def mono_currency_uri(self) -> str: |
| 32 | + return self._mono_currency_uri |
| 33 | + |
| 34 | + @mono_currency_uri.setter |
| 35 | + def mono_currency_uri(self, new_uri): |
| 36 | + self._mono_currency_uri = new_uri |
| 37 | + |
| 38 | + @property |
| 39 | + def mono_client_info_uri(self) -> str: |
| 40 | + return self._mono_client_info_uri |
| 41 | + |
| 42 | + @mono_client_info_uri.setter |
| 43 | + def mono_client_info_uri(self, new_uri): |
| 44 | + self._mono_client_info_uri = new_uri |
| 45 | + |
| 46 | + @property |
| 47 | + def mono_statement_uri(self) -> str: |
| 48 | + return self._mono_statement_uri |
7 | 49 |
|
8 | | - def __init__(self, request): |
9 | | - self.request = request |
10 | | - |
11 | | - session = requests.Session() |
12 | | - |
13 | | - _currency_uri = 'https://api.monobank.ua/bank/currency' |
14 | | - _client_info_uri = 'https://api.monobank.ua/personal/client-info' |
15 | | - _statement_uri = 'https://api.monobank.ua/personal/statement/0/' |
16 | | - _webhook_uri = 'https://api.monobank.ua/personal/webhook' |
17 | | - _day_utc = 86400 # 1 day (UNIX) |
| 50 | + @mono_statement_uri.setter |
| 51 | + def mono_statement_uri(self, new_uri): |
| 52 | + self._mono_statement_uri = new_uri |
| 53 | + |
| 54 | + @property |
| 55 | + def mono_webhook_uri(self) -> str: |
| 56 | + return self._mono_webhook_uri |
| 57 | + |
| 58 | + @mono_webhook_uri.setter |
| 59 | + def mono_webhook_uri(self, new_uri): |
| 60 | + self._mono_webhook_uri = new_uri |
18 | 61 |
|
19 | 62 | @classmethod |
20 | | - def get_currency(cls) -> Tuple[int, Dict[str, Any]]: |
| 63 | + def session(cls) -> requests.sessions.Session: |
| 64 | + return requests.Session() |
| 65 | + |
| 66 | + @staticmethod |
| 67 | + def __date(period: int) -> Dict: |
| 68 | + _day = 86400 # 1 day (UNIX) |
21 | 69 | try: |
22 | | - response = cls.session.get(cls._currency_uri) |
23 | | - response.raise_for_status() |
24 | | - return response.status_code, response.json() |
25 | | - except requests.exceptions.HTTPError as exc: |
26 | | - error_response = { |
27 | | - "detail": str(exc), |
28 | | - "code": response.status_code, |
29 | | - } |
30 | | - return error_response |
| 70 | + delta = int(datetime.now().timestamp()) - (period * _day) |
| 71 | + time_delta = {"time_delta": delta} |
| 72 | + return time_delta |
31 | 73 | except Exception as exc: |
32 | | - return { |
33 | | - "detail": str(exc) |
34 | | - } |
| 74 | + exception = {"detail": str(exc)} |
| 75 | + return exception |
35 | 76 |
|
36 | | - def get_client_info(self, token: str) -> Tuple[int, Dict[str, Any]]: |
| 77 | + def get_currency(self) -> Dict: |
37 | 78 | try: |
38 | | - headers = {"X-Token": token} |
39 | | - response = self.session.get( |
40 | | - self._client_info_uri, |
41 | | - headers=headers |
42 | | - ) |
| 79 | + session = self.session() |
| 80 | + uri = self.mono_currency_uri |
| 81 | + response = session.get(uri) |
| 82 | + code = response.status_code |
43 | 83 | response.raise_for_status() |
44 | | - return response.status_code, response.json() |
| 84 | + payload = {"code": code, "detail": response.json()} |
| 85 | + return payload |
45 | 86 | except requests.exceptions.HTTPError as exc: |
46 | | - error_response = { |
47 | | - "detail": str(exc), |
48 | | - "code": response.status_code, |
49 | | - } |
| 87 | + error_response = {"code": code, "detail": str(exc)} |
50 | 88 | return error_response |
51 | 89 | except Exception as exc: |
52 | | - return { |
53 | | - "detail": str(exc) |
54 | | - } |
| 90 | + exception = {"detail": str(exc)} |
| 91 | + return exception |
55 | 92 |
|
56 | | - def get_balance(self, token: str) -> Tuple[int, Dict[str, Any]]: |
| 93 | + def get_client_info(self) -> Dict: |
57 | 94 | try: |
| 95 | + session = self.session() |
| 96 | + token = self.token |
| 97 | + uri = self.mono_client_info_uri |
58 | 98 | headers = {"X-Token": token} |
59 | | - response = self.session.get( |
60 | | - self._client_info_uri, |
61 | | - headers=headers |
62 | | - ) |
| 99 | + response = session.get(uri, headers=headers) |
| 100 | + code = response.status_code |
63 | 101 | response.raise_for_status() |
64 | | - balance = { |
65 | | - 'balance': response.json()["accounts"][0]["balance"] / 100 |
66 | | - } |
67 | | - return response.status_code, balance |
| 102 | + payload = {"code": code, "detail": response.json()} |
| 103 | + return payload |
68 | 104 | except requests.exceptions.HTTPError as exc: |
69 | | - error_response = { |
70 | | - "detail": str(exc), |
71 | | - "code": response.status_code, |
72 | | - } |
| 105 | + error_response = {"code": code, "detail": str(exc)} |
73 | 106 | return error_response |
74 | 107 | except Exception as exc: |
75 | | - return { |
76 | | - "detail": str(exc) |
77 | | - } |
| 108 | + exception = {"detail": str(exc)} |
| 109 | + return exception |
| 110 | + |
| 111 | + def get_balance(self) -> Dict: |
| 112 | + try: |
| 113 | + client_info = self.get_client_info() |
| 114 | + code = client_info.get("code") |
| 115 | + data = client_info.get("detail") |
| 116 | + balance = {"balance": data["accounts"][0]["balance"] / 100} |
| 117 | + payload = {"code": code, "detail": balance} |
| 118 | + return payload |
| 119 | + except Exception: |
| 120 | + return client_info |
78 | 121 |
|
79 | | - def get_statement(self, token: str, period: int) -> Tuple[int, Dict[str, Any]]: |
| 122 | + def get_statement(self, period: int) -> Dict: |
80 | 123 | try: |
81 | | - time_delta = int(datetime.now().timestamp()) - (period * self._day_utc) |
| 124 | + session = self.session() |
| 125 | + token = self.token |
| 126 | + uri = self.mono_statement_uri |
82 | 127 | headers = {"X-Token": token} |
83 | | - response = self.session.get( |
84 | | - f"{self._statement_uri}{time_delta}/", |
85 | | - headers=headers |
86 | | - ) |
| 128 | + time_delta = self.__date(period).get("time_delta") |
| 129 | + response = session.get(f"{uri}{time_delta}/", headers=headers) |
| 130 | + code = response.status_code |
87 | 131 | response.raise_for_status() |
88 | | - return response.status_code, response.json() |
| 132 | + payload = {"code": code, "detail": response.json()} |
| 133 | + return payload |
89 | 134 | except requests.exceptions.HTTPError as exc: |
90 | | - error_response = { |
91 | | - "detail": str(exc), |
92 | | - "code": response.status_code, |
93 | | - } |
| 135 | + error_response = {"code": code, "detail": str(exc)} |
94 | 136 | return error_response |
95 | 137 | except Exception as exc: |
96 | | - return { |
97 | | - "detail": str(exc) |
98 | | - } |
| 138 | + exception = {"detail": str(exc)} |
| 139 | + return exception |
99 | 140 |
|
100 | | - def create_webhook(self, token: str, webHookUrl: str) -> Tuple[int, Dict[str, Any]]: |
| 141 | + def create_webhook(self, webhook: str) -> Dict: |
101 | 142 | try: |
| 143 | + session = self.session() |
| 144 | + token = self.token |
| 145 | + uri = self.mono_webhook_uri |
102 | 146 | headers = {"X-Token": token} |
103 | | - response = self.session.post( |
104 | | - self._webhook_uri, |
105 | | - data=webHookUrl, |
106 | | - headers=headers |
107 | | - ) |
| 147 | + response = session.post(uri, headers=headers, data=webhook) |
| 148 | + code = response.status_code |
108 | 149 | response.raise_for_status() |
109 | | - return response.status_code, response.json() |
| 150 | + payload = {"code": code, "detail": response.json()} |
| 151 | + return payload |
110 | 152 | except requests.exceptions.HTTPError as exc: |
111 | | - error_response = { |
112 | | - "detail": str(exc), |
113 | | - "code": response.status_code, |
114 | | - } |
| 153 | + error_response = {"code": code, "detail": str(exc)} |
115 | 154 | return error_response |
116 | 155 | except Exception as exc: |
117 | | - return { |
118 | | - "detail": str(exc) |
119 | | - } |
| 156 | + exception = {"detail": str(exc)} |
| 157 | + return exception |
0 commit comments