1- from typing import Tuple , Dict
1+ from typing import Tuple , Dict , List
22import requests
33from datetime import datetime
44
1212
1313class MonoManager :
1414
15- def __init__ (self , request , token = None ):
16- self .request = request
15+ def __init__ (self , token = None ):
1716 self ._token = token
1817
19- _session = requests .Session ()
20-
21- _day_unix = 86400 # 1 day (UNIX)
22-
2318 _mono_currency_uri = MONOBANK_CURRENCY_URI
2419 _mono_client_info_uri = MONOBANK_CLIENT_INFO_URI
2520 _mono_statement_uri = MONOBANK_STATEMENT_URI
@@ -66,48 +61,62 @@ def mono_webhook_uri(self, new_uri):
6661 self ._mono_webhook_uri = new_uri
6762
6863 @classmethod
69- @property
70- def get_currency (cls ) -> Tuple [int , Dict ]:
64+ def session (cls ) -> requests .Session :
65+ return requests .Session ()
66+
67+ @staticmethod
68+ def __date (period : int ) -> Tuple [int ]:
69+ _day = 86400 # 1 day (UNIX)
7170 try :
72- session = cls ._session
73- uri = cls ._mono_currency_uri
71+ time_delta = int (datetime .now ().timestamp ()) - (period * _day )
72+ return time_delta
73+ except Exception as exc :
74+ exception = {
75+ 'detail' : str (exc )
76+ }
77+ return exception
78+
79+ def get_currency (self ) -> Tuple [int , Dict | List [Dict ]]:
80+ try :
81+ session = self .session ()
82+ uri = self .mono_currency_uri
7483 response = session .get (uri )
84+ code = response .status_code
7585 response .raise_for_status ()
76- return response . status_code , response .json ()
86+ return code , response .json ()
7787 except requests .exceptions .HTTPError as exc :
7888 error_response = {
79- "code" : response .status_code ,
80- "detail" : str (exc ),
89+ "detail" : str (exc )
8190 }
82- return error_response
91+ return code , error_response
8392 except Exception as exc :
8493 exception = {
8594 "detail" : str (exc )
8695 }
8796 return exception
8897
89- def get_client_info (self ) -> Tuple [int , Dict ]:
98+ def get_client_info (self ) -> Tuple [int , Dict | List [ Dict ] ]:
9099 try :
91- session = self ._session
92- token = self ._token
93- uri = self ._mono_client_info_uri
100+ session = self .session ()
101+ token = self .token
102+ uri = self .mono_client_info_uri
94103 headers = {"X-Token" : token }
95104 response = session .get (uri , headers = headers )
105+ code = response .status_code
96106 response .raise_for_status ()
97- return response . status_code , response .json ()
107+ return code , response .json ()
98108 except requests .exceptions .HTTPError as exc :
99109 error_response = {
100- "code" : response .status_code ,
101- "detail" : str (exc ),
110+ "detail" : str (exc )
102111 }
103- return error_response
112+ return code , error_response
104113 except Exception as exc :
105114 exception = {
106115 "detail" : str (exc )
107116 }
108117 return exception
109118
110- def get_balance (self ) -> Tuple [int , Dict ]:
119+ def get_balance (self ) -> Tuple [int , Dict | List [ Dict ] ]:
111120 try :
112121 response = self .get_client_info ()
113122 code = response [0 ]
@@ -119,43 +128,43 @@ def get_balance(self) -> Tuple[int, Dict]:
119128 except Exception :
120129 return response
121130
122- def get_statement (self , period : int ) -> Tuple [int , Dict ]:
131+ def get_statement (self , period : int ) -> Tuple [int , Dict | List [ Dict ] ]:
123132 try :
124- session = self ._session
125- token = self ._token
126- uri = self ._mono_statement_uri
133+ session = self .session ()
134+ token = self .token
135+ uri = self .mono_statement_uri
127136 headers = {"X-Token" : token }
128- time_delta = int ( datetime . now (). timestamp ()) - ( period * self . _day_unix )
137+ time_delta = self . __date ( period )
129138 response = session .get (f"{ uri } { time_delta } /" , headers = headers )
139+ code = response .status_code
130140 response .raise_for_status ()
131- return response . status_code , response .json ()
141+ return code , response .json ()
132142 except requests .exceptions .HTTPError as exc :
133143 error_response = {
134- "code" : response .status_code ,
135- "detail" : str (exc ),
144+ "detail" : str (exc )
136145 }
137- return error_response
146+ return code , error_response
138147 except Exception as exc :
139148 exception = {
140149 "detail" : str (exc )
141150 }
142151 return exception
143152
144- def create_webhook (self , url : str ) -> Tuple [int , Dict ]:
153+ def create_webhook (self , webhook : str ) -> Tuple [int , Dict | List [ Dict ] ]:
145154 try :
146- session = self ._session
147- token = self ._token
148- uri = self ._mono_webhook_uri
155+ session = self .session ()
156+ token = self .token
157+ uri = self .mono_webhook_uri
149158 headers = {"X-Token" : token }
150- response = session .post (uri , headers = headers , data = url )
159+ response = session .post (uri , headers = headers , data = webhook )
160+ code = response .status_code
151161 response .raise_for_status ()
152- return response . status_code , response .json ()
162+ return code , response .json ()
153163 except requests .exceptions .HTTPError as exc :
154164 error_response = {
155- "code" : response .status_code ,
156165 "detail" : str (exc )
157166 }
158- return error_response
167+ return code , error_response
159168 except Exception as exc :
160169 exception = {
161170 "detail" : str (exc )
0 commit comments