Skip to content

Commit 115a1a4

Browse files
committed
feat(repository): add method in BaseMonoManager
1 parent 518b5ba commit 115a1a4

File tree

5 files changed

+96
-75
lines changed

5 files changed

+96
-75
lines changed

monobank_api_client/async_mono/manager.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ async def async_request(
2424
code = response.status
2525
response.raise_for_status()
2626
detail = await response.json()
27-
payload = {"code": code, "detail": detail}
27+
payload = self.mono_response(code, detail)
2828
return payload
2929
except aiohttp.ClientResponseError as exc:
30-
error_response = {"code": code, "detail": str(exc.message)}
30+
error_response = self.mono_response(code, str(exc.message))
3131
return error_response
3232
except Exception as exc:
3333
exception = {"detail": str(exc)}
@@ -42,15 +42,14 @@ async def get_currencies(self) -> Dict:
4242
exception = {"datail": str(exc)}
4343
return exception
4444

45-
async def get_currency(self, ccy_pair: str) -> Dict:
45+
async def get_currency(self, ccy: str) -> Dict:
4646
try:
47-
pair = self.mono_currencies.get(ccy_pair)
47+
pair = self.mono_currencies.get(ccy)
4848
if pair is not None:
4949
currencies = await self.get_currencies()
50-
response = self.currency(ccy_pair, pair, currencies)
50+
response = self.currency(ccy, pair, currencies)
5151
else:
52-
list_ccy = [key for key in self.mono_currencies.keys()]
53-
response = self.currency_exception(list_ccy)
52+
response = self.currency_exception()
5453
return response
5554
except Exception as exc:
5655
exception = {"detail": str(exc)}
@@ -69,14 +68,14 @@ async def get_client_info(self) -> Dict:
6968

7069
async def get_balance(self) -> Dict:
7170
try:
72-
info = await self.get_client_info()
73-
code = info.get("code")
74-
data = info.get("detail")
71+
payload = await self.get_client_info()
72+
code = payload.get("code")
73+
data = payload.get("detail")
7574
balance = {"balance": data["accounts"][0]["balance"] / 100}
76-
response = {"code": code, "detail": balance}
75+
response = self.mono_response(code, balance)
7776
return response
7877
except Exception:
79-
return info
78+
return payload
8079

8180
async def get_statement(self, period: int) -> Dict:
8281
try:

monobank_api_client/fastapi_mono/router.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ async def add_monobank(
1515
schema: MonoSchema, session: AsyncSession = Depends(async_session)
1616
) -> Dict:
1717
try:
18-
return await crud.create_mono(schema, session)
18+
response = await crud.create_mono(schema, session)
19+
return response
1920
except Exception as exc:
2021
exception = {"detail": str(exc)}
2122
return exception
@@ -28,7 +29,8 @@ async def change_monobank(
2829
session: AsyncSession = Depends(async_session),
2930
) -> Dict:
3031
try:
31-
return await crud.update_mono(user, schema, session)
32+
response = await crud.update_mono(user, schema, session)
33+
return response
3234
except Exception as exc:
3335
exception = {"detail": str(exc)}
3436
return exception
@@ -39,7 +41,8 @@ async def delete_monobank(
3941
user: str, session: AsyncSession = Depends(async_session)
4042
) -> Dict:
4143
try:
42-
return await crud.delete_mono(user, session)
44+
response = await crud.delete_mono(user, session)
45+
return response
4346
except Exception as exc:
4447
exception = {"detail": str(exc)}
4548
return exception
@@ -49,7 +52,8 @@ async def delete_monobank(
4952
async def currencies() -> Dict:
5053
try:
5154
mng = AsyncMonoManager()
52-
return await mng.get_currencies()
55+
response = await mng.get_currencies()
56+
return response
5357
except Exception as exc:
5458
exception = {"detail": str(exc)}
5559
return exception
@@ -59,7 +63,8 @@ async def currencies() -> Dict:
5963
async def currency(ccy_pair: str) -> Dict:
6064
try:
6165
mng = AsyncMonoManager()
62-
return await mng.get_currency(ccy_pair)
66+
response = await mng.get_currency(ccy_pair)
67+
return response
6368
except Exception as exc:
6469
exception = {"detail": str(exc)}
6570
return exception
@@ -74,8 +79,10 @@ async def client_info(
7479
payload = await crud.read_mono(user_id, session)
7580
if payload is not None:
7681
mng.token = payload[0].mono_token
77-
return await mng.get_client_info()
78-
return mng.does_not_exsists_exception()
82+
response = await mng.get_client_info()
83+
else:
84+
response = mng.does_not_exsists_exception()
85+
return response
7986
except Exception as exc:
8087
exception = {"detail": str(exc)}
8188
return exception
@@ -88,8 +95,10 @@ async def balance(user_id: str, session: AsyncSession = Depends(async_session))
8895
payload = await crud.read_mono(user_id, session)
8996
if payload is not None:
9097
mng.token = payload[0].mono_token
91-
return await mng.get_balance()
92-
return mng.does_not_exsists_exception()
98+
response = await mng.get_balance()
99+
else:
100+
response = mng.does_not_exsists_exception()
101+
return response
93102
except Exception as exc:
94103
exception = {"detail": str(exc)}
95104
return exception
@@ -104,8 +113,10 @@ async def statement(
104113
payload = await crud.read_mono(user_id, session)
105114
if payload is not None:
106115
mng.token = payload[0].mono_token
107-
return await mng.get_statement(period)
108-
return mng.does_not_exsists_exception()
116+
response = await mng.get_statement(period)
117+
else:
118+
response = mng.does_not_exsists_exception()
119+
return response
109120
except Exception as exc:
110121
exception = {"detail": str(exc)}
111122
return exception
@@ -120,8 +131,10 @@ async def webhook(
120131
payload = await crud.read_mono(user_id, session)
121132
if payload is not None:
122133
mng.token = payload[0].mono_token
123-
return await mng.create_webhook(webhook)
124-
return mng.does_not_exsists_exception()
134+
response = await mng.create_webhook(webhook)
135+
else:
136+
response = mng.does_not_exsists_exception()
137+
return response
125138
except Exception as exc:
126139
exception = {"detail": str(exc)}
127140
return exception

monobank_api_client/mono_config/manager.py

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, List
1+
from typing import Dict, Any
22
from datetime import datetime
33
from .config import (
44
MONOBANK_CURRENCIES_URI,
@@ -218,94 +218,103 @@ def date(period: int) -> Dict:
218218
exception = {"detail": str(exc)}
219219
return exception
220220

221-
def currency(self, ccy_pair: str, pair: Dict, currencies: Dict) -> Dict:
221+
def mono_response(self, code: int, detail: Any, info=None) -> Dict:
222+
try:
223+
if info is not None:
224+
response = {"code": code, "detail": detail, "info": info}
225+
else:
226+
response = {"code": code, "detail": detail}
227+
return response
228+
except Exception as exc:
229+
exception = {"detail": str(exc)}
230+
return exception
231+
232+
def currency(self, ccy: str, pair: Dict, currencies: Dict) -> Dict:
222233
try:
223234
code_a = self.mono_currency_code_a
224235
code_b = self.mono_currency_code_b
225236
code = currencies.get("code")
226237
payload = currencies.get("detail")
227-
for ccy in payload:
228-
if ccy.get(code_a) == pair.get(code_a) and ccy.get(code_b) == pair.get(
238+
for _ in payload:
239+
if _.get(code_a) == pair.get(code_a) and _.get(code_b) == pair.get(
229240
code_b
230241
):
231-
cross = ccy.get("rateCross")
242+
cross = _.get("rateCross")
232243
if cross is not None:
233-
currency = {ccy_pair: {"Cross": cross}}
244+
currency = {ccy: {"Cross": cross}}
234245
else:
235-
buy = ccy.get("rateBuy")
236-
sale = ccy.get("rateSell")
237-
currency = {ccy_pair: {"Buy": buy, "Sale": sale}}
238-
response = {"code": code, "detail": currency}
246+
buy = _.get("rateBuy")
247+
sale = _.get("rateSell")
248+
currency = {ccy: {"Buy": buy, "Sale": sale}}
249+
response = self.mono_response(code, currency)
239250
return response
240251
except AttributeError:
241-
error_response = {"code": code, "detail": payload}
252+
error_response = self.mono_response(code, payload)
242253
return error_response
243254
except Exception as exc:
244255
exception = {"detail": str(exc)}
245256
return exception
246257

247258
def create_success(self) -> Dict:
248259
try:
249-
response = {
250-
"code": self.mono_create_success_code,
251-
"detail": self.mono_create_success_detail,
252-
}
260+
response = self.mono_response(
261+
self.mono_create_success_code, self.mono_create_success_detail
262+
)
253263
return response
254264
except Exception as exc:
255265
exception = {"detail": str(exc)}
256266
return exception
257267

258268
def update_success(self) -> Dict:
259269
try:
260-
response = {
261-
"code": self.mono_update_success_code,
262-
"detail": self.mono_update_success_detail,
263-
}
270+
response = self.mono_response(
271+
self.mono_update_success_code, self.mono_update_success_detail
272+
)
264273
return response
265274
except Exception as exc:
266275
exception = {"detail": str(exc)}
267276
return exception
268277

269278
def delete_success(self) -> Dict:
270279
try:
271-
response = {
272-
"code": self.mono_delete_success_code,
273-
"detail": self.mono_delete_success_detail,
274-
}
280+
response = self.mono_response(
281+
self.mono_delete_success_code, self.mono_delete_success_detail
282+
)
275283
return response
276284
except Exception as exc:
277285
exception = {"detail": str(exc)}
278286
return exception
279287

280-
def currency_exception(self, list_ccy: List) -> Dict:
288+
def currency_exception(self) -> Dict:
281289
try:
282-
response = {
283-
"code": self.mono_currency_exception_code,
284-
"detail": self.mono_currency_exception_detail,
285-
"list of acceptable currency pairs": list_ccy,
286-
}
290+
list_ccy = [key for key in self.mono_currencies.keys()]
291+
currencies_pairs = {"currencies pairs": list_ccy}
292+
response = self.mono_response(
293+
self.mono_currency_exception_code,
294+
self.mono_currency_exception_detail,
295+
currencies_pairs,
296+
)
287297
return response
288298
except Exception as exc:
289299
exception = {"detail": str(exc)}
290300
return exception
291301

292302
def exists_exception(self) -> Dict:
293303
try:
294-
response = {
295-
"code": self.mono_exsists_exception_code,
296-
"detail": self.mono_exsists_exception_detail,
297-
}
304+
response = self.mono_response(
305+
self.mono_exsists_exception_code, self.mono_exsists_exception_detail
306+
)
298307
return response
299308
except Exception as exc:
300309
exception = {"detail": str(exc)}
301310
return exception
302311

303312
def does_not_exsists_exception(self) -> Dict:
304313
try:
305-
response = {
306-
"code": self.mono_does_not_exsists_exception_code,
307-
"detail": self.mono_does_not_exsists_exception_detail,
308-
}
314+
response = self.mono_response(
315+
self.mono_does_not_exsists_exception_code,
316+
self.mono_does_not_exsists_exception_detail,
317+
)
309318
return response
310319
except Exception as exc:
311320
exception = {"detail": str(exc)}

monobank_api_client/sync_mono/manager.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ def sync_request(
2323
try:
2424
code = response.status_code
2525
response.raise_for_status()
26-
payload = {"code": code, "detail": response.json()}
26+
detail = response.json()
27+
payload = self.mono_response(code, detail)
2728
return payload
2829
except requests.exceptions.HTTPError as exc:
29-
error_response = {"code": code, "detail": str(exc)}
30+
error_response = self.mono_response(code, str(exc))
3031
return error_response
3132
except Exception as exc:
3233
exception = {"detail": str(exc)}
@@ -41,15 +42,14 @@ def get_currencies(self) -> Dict:
4142
exception = {"detail": str(exc)}
4243
return exception
4344

44-
def get_currency(self, ccy_pair: str) -> Dict:
45+
def get_currency(self, ccy: str) -> Dict:
4546
try:
46-
pair = self.mono_currencies.get(ccy_pair)
47+
pair = self.mono_currencies.get(ccy)
4748
if pair is not None:
4849
currencies = self.get_currencies()
49-
response = self.currency(ccy_pair, pair, currencies)
50+
response = self.currency(ccy, pair, currencies)
5051
else:
51-
list_ccy = [key for key in self.mono_currencies.keys()]
52-
response = self.currency_exception(list_ccy)
52+
response = self.currency_exception()
5353
return response
5454
except Exception as exc:
5555
exception = {"detail": str(exc)}
@@ -68,14 +68,14 @@ def get_client_info(self) -> Dict:
6868

6969
def get_balance(self) -> Dict:
7070
try:
71-
client_info = self.get_client_info()
72-
code = client_info.get("code")
73-
data = client_info.get("detail")
71+
payload = self.get_client_info()
72+
code = payload.get("code")
73+
data = payload.get("detail")
7474
balance = {"balance": data["accounts"][0]["balance"] / 100}
75-
response = {"code": code, "detail": balance}
75+
response = self.mono_response(code, balance)
7676
return response
7777
except Exception:
78-
return client_info
78+
return payload
7979

8080
def get_statement(self, period: int) -> Dict:
8181
try:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def readme():
88

99
setup(
1010
name="monobank_api_client",
11-
version="1.2.0",
11+
version="1.2.2",
1212
author="ihor.sotnyk",
1313
author_email="ihor.sotnyk@onix-systems.com",
1414
description="This module is designed for quick interaction with the monobank API.",

0 commit comments

Comments
 (0)