Skip to content

Commit 7f9d28d

Browse files
committed
Debugged the method get_balance in the MonoManager and update config
1 parent 76f8422 commit 7f9d28d

3 files changed

Lines changed: 48 additions & 43 deletions

File tree

monobank_api_client/config.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
# monobank urls
2-
MONO_CURRENCY_URI = 'https://api.monobank.ua/bank/currency'
3-
MONO_CLIENT_INFO_URI = 'https://api.monobank.ua/personal/client-info'
4-
MONO_STATEMENT = 'https://api.monobank.ua/personal/statement/0/'
5-
MONO_WEBHOOK_URI = 'https://api.monobank.ua/personal/webhook'
1+
from dotenv import load_dotenv
2+
import os
3+
4+
load_dotenv()
5+
6+
7+
MONOBANK_CURRENCY_URI = os.getenv(
8+
'MONOBANK_CURRENCY_URI', 'https://api.monobank.ua/bank/currency'
9+
)
10+
MONOBANK_CLIENT_INFO_URI = os.getenv(
11+
'MONOBANK_CLIENT_INFO_URI', 'https://api.monobank.ua/personal/client-info'
12+
)
13+
MONOBANK_STATEMENT_URI = os.getenv(
14+
'MONOBANK_STATEMENT_URI', 'https://api.monobank.ua/personal/statement/0/'
15+
)
16+
MONOBANK_WEBHOOK_URI = os.getenv(
17+
'MONOBANK_WEBHOOK_URI', 'https://api.monobank.ua/personal/webhook'
18+
)

monobank_api_client/managers.py

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
from datetime import datetime
44

55
from .config import (
6-
MONO_CURRENCY_URI,
7-
MONO_CLIENT_INFO_URI,
8-
MONO_STATEMENT,
9-
MONO_WEBHOOK_URI,
6+
MONOBANK_CLIENT_INFO_URI,
7+
MONOBANK_CURRENCY_URI,
8+
MONOBANK_STATEMENT_URI,
9+
MONOBANK_WEBHOOK_URI,
1010
)
1111

1212

@@ -20,10 +20,10 @@ def __init__(self, request, token=None):
2020

2121
_day_unix = 86400 # 1 day (UNIX)
2222

23-
_mono_currency_uri = MONO_CURRENCY_URI
24-
_mono_client_info_uri = MONO_CLIENT_INFO_URI
25-
_mono_statement_uri = MONO_STATEMENT
26-
_mono_webhook_uri = MONO_WEBHOOK_URI
23+
_mono_currency_uri = MONOBANK_CURRENCY_URI
24+
_mono_client_info_uri = MONOBANK_CLIENT_INFO_URI
25+
_mono_statement_uri = MONOBANK_STATEMENT_URI
26+
_mono_webhook_uri = MONOBANK_WEBHOOK_URI
2727

2828
@property
2929
def token(self):
@@ -65,26 +65,26 @@ def mono_webhook_uri(self):
6565
def mono_webhook_uri(self, new_uri):
6666
self._mono_webhook_uri = new_uri
6767

68-
6968
@classmethod
7069
@property
7170
def get_currency(cls) -> Tuple[int, Dict[str, Any]]:
7271
try:
7372
session = cls._session
74-
uri = cls._mono_client_info_uri
73+
uri = cls._mono_currency_uri
7574
response = session.get(uri)
7675
response.raise_for_status()
7776
return response.status_code, response.json()
7877
except requests.exceptions.HTTPError as exc:
7978
error_response = {
80-
"detail": str(exc),
8179
"code": response.status_code,
80+
"detail": str(exc),
8281
}
8382
return error_response
8483
except Exception as exc:
85-
return {
84+
exception = {
8685
"detail": str(exc)
8786
}
87+
return exception
8888

8989
def get_client_info(self) -> Tuple[int, Dict[str, Any]]:
9090
try:
@@ -97,37 +97,27 @@ def get_client_info(self) -> Tuple[int, Dict[str, Any]]:
9797
return response.status_code, response.json()
9898
except requests.exceptions.HTTPError as exc:
9999
error_response = {
100-
"detail": str(exc),
101100
"code": response.status_code,
101+
"detail": str(exc),
102102
}
103103
return error_response
104104
except Exception as exc:
105-
return {
105+
exception = {
106106
"detail": str(exc)
107107
}
108+
return exception
108109

109110
def get_balance(self) -> Tuple[int, Dict[str, Any]]:
110111
try:
111-
session = self._session
112-
token = self._token
113-
uri = self._mono_client_info_uri
114-
headers = {"X-Token": token}
115-
response = session.get(uri, headers=headers)
116-
response.raise_for_status()
112+
response = self.get_client_info()
113+
code = response[0]
114+
payload = response[1]
117115
balance = {
118-
'balance': response.json()["accounts"][0]["balance"] / 100
119-
}
120-
return response.status_code, balance
121-
except requests.exceptions.HTTPError as exc:
122-
error_response = {
123-
"detail": str(exc),
124-
"code": response.status_code,
125-
}
126-
return error_response
127-
except Exception as exc:
128-
return {
129-
"detail": str(exc)
116+
'balance': payload["accounts"][0]["balance"] / 100
130117
}
118+
return code, balance
119+
except Exception:
120+
return response
131121

132122
def get_statement(self, period: int) -> Tuple[int, Dict[str, Any]]:
133123
try:
@@ -141,31 +131,33 @@ def get_statement(self, period: int) -> Tuple[int, Dict[str, Any]]:
141131
return response.status_code, response.json()
142132
except requests.exceptions.HTTPError as exc:
143133
error_response = {
144-
"detail": str(exc),
145134
"code": response.status_code,
135+
"detail": str(exc),
146136
}
147137
return error_response
148138
except Exception as exc:
149-
return {
139+
exception = {
150140
"detail": str(exc)
151141
}
142+
return exception
152143

153144
def create_webhook(self, webHookUrl: str) -> Tuple[int, Dict[str, Any]]:
154145
try:
155146
session = self._session
156147
token = self._token
157148
uri = self._mono_webhook_uri
158149
headers = {"X-Token": token}
159-
response = session.post(uri, data=webHookUrl, headers=headers)
150+
response = session.post(uri, headers=headers, data=webHookUrl)
160151
response.raise_for_status()
161152
return response.status_code, response.json()
162153
except requests.exceptions.HTTPError as exc:
163154
error_response = {
164-
"detail": str(exc),
165155
"code": response.status_code,
156+
"detail": str(exc)
166157
}
167158
return error_response
168159
except Exception as exc:
169-
return {
160+
exception = {
170161
"detail": str(exc)
171162
}
163+
return exception

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='0.1.3',
11+
version='0.1.5',
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)