Skip to content

Commit 4cf4226

Browse files
committed
Merge branch 'develop_1' into 'main'
feat(repository): Integration of supporting packages See merge request onix-systems/python-internal-mono!4
2 parents 97f5148 + e6b7401 commit 4cf4226

File tree

26 files changed

+1140
-233
lines changed

26 files changed

+1140
-233
lines changed

README.md

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,65 @@
11
# monobank-api-client
2-
This module is designed for quick interaction with the monobank API
2+
This module provides quick integration of the Monobank API for developing applications based on synchronous and asynchronous frameworks.
33

44
## Name
55
monobank_api_client
66

77
## Installation
88
This framework is published at the PyPI, install it with pip:
99

10-
pip install monobank-api-client
10+
1.This package makes it possible to use module methods in synchronous frameworks:
11+
12+
pip install monobank-api-client[http]
13+
14+
2.This package makes it possible to use module methods in asynchronous frameworks:
15+
16+
pip install monobank-api-client[aio]
17+
18+
3.This package makes it possible to use ready-made views with a synchronous script based on the Django Rest framework:
19+
20+
pip install monobank-api-client[drf]
21+
22+
To get started, add the following packages to INSTALLED_APPS:
23+
24+
INSTALLED_APPS = [
25+
...
26+
'rest_framework',
27+
'drf_mono',
28+
]
29+
30+
Include drf_mono urls to your urls.py:
31+
32+
urlpatterns = [
33+
...
34+
path('mono/', include('drf_mono.urls', namespace='drf_mono')),
35+
]
36+
37+
4.This package makes it possible to use ready-made routers with an asynchronous script based on the FastAPI framework:
38+
39+
pip install monobank-api-client[fastapi]
40+
41+
5.To install all packages at once:
42+
43+
pip install monobank-api-client[all]
1144

1245
## Usage
1346

1447
1. Request your token at https://api.monobank.ua/
15-
2. Use that token to initialize client:
48+
2. For a synchronous request use that token to initialize client:
49+
50+
from sync_mono.manager import SyncMonoManager
51+
52+
token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
53+
54+
mng = SyncMonoManager(token)
55+
56+
3. For an asynchronous request, use this token to initialize the client:
1657

17-
from monobank_api_client.managers import MonoManager
58+
from async_mono.manager import AsyncMonoManager
1859

1960
token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
2061

21-
mng = MonoManager(token)
62+
mng = AsyncMonoManager(token)
2263

2364
### Methods
2465

monobank_api_client/async_mono/__init__.py

Whitespace-only changes.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import aiohttp
2+
from typing import Dict
3+
from mono_config.manager import BaseMonoManager
4+
5+
6+
class AsyncMonoManager(BaseMonoManager):
7+
@classmethod
8+
async def session(cls) -> aiohttp.client.ClientSession:
9+
return aiohttp.ClientSession()
10+
11+
async def async_request(
12+
self,
13+
method: str,
14+
uri: str,
15+
headers=None,
16+
data=None,
17+
) -> Dict:
18+
session = await self.session()
19+
if method == "GET":
20+
response = await session.get(uri, headers=headers)
21+
if method == "POST":
22+
response = await session.post(uri, headers=headers, data=data)
23+
try:
24+
code = response.status
25+
response.raise_for_status()
26+
detail = await response.json()
27+
payload = self.mono_response(code, detail)
28+
return payload
29+
except aiohttp.ClientResponseError as exc:
30+
error_response = self.mono_response(code, str(exc.message))
31+
return error_response
32+
except Exception as exc:
33+
exception = {"detail": str(exc)}
34+
return exception
35+
36+
async def get_currencies(self) -> Dict:
37+
try:
38+
uri = self.mono_currencies_uri
39+
response = await self.async_request(method="GET", uri=uri)
40+
return response
41+
except Exception as exc:
42+
exception = {"datail": str(exc)}
43+
return exception
44+
45+
async def get_currency(self, ccy: str) -> Dict:
46+
try:
47+
pair = self.mono_currencies.get(ccy)
48+
if pair is not None:
49+
currencies = await self.get_currencies()
50+
response = self.currency(ccy, pair, currencies)
51+
else:
52+
response = self.currency_exception()
53+
return response
54+
except Exception as exc:
55+
exception = {"detail": str(exc)}
56+
return exception
57+
58+
async def get_client_info(self) -> Dict:
59+
try:
60+
uri = self.mono_client_info_uri
61+
token = self.token
62+
headers = {"X-Token": token}
63+
response = await self.async_request(method="GET", uri=uri, headers=headers)
64+
return response
65+
except Exception as exc:
66+
exception = {"detail": str(exc)}
67+
return exception
68+
69+
async def get_balance(self) -> Dict:
70+
try:
71+
payload = await self.get_client_info()
72+
code = payload.get("code")
73+
data = payload.get("detail")
74+
balance = {"balance": data["accounts"][0]["balance"] / 100}
75+
response = self.mono_response(code, balance)
76+
return response
77+
except Exception:
78+
return payload
79+
80+
async def get_statement(self, period: int) -> Dict:
81+
try:
82+
uri = self.mono_statement_uri
83+
token = self.token
84+
headers = {"X-Token": token}
85+
time_delta = self.date(period).get("time_delta")
86+
response = await self.async_request(
87+
method="GET", uri=f"{uri}{time_delta}/", headers=headers
88+
)
89+
return response
90+
except Exception as exc:
91+
exception = {"detail": str(exc)}
92+
return exception
93+
94+
async def create_webhook(self, webhook: str) -> Dict:
95+
try:
96+
uri = self.mono_webhook_uri
97+
token = self.token
98+
headers = {"X-Token": token}
99+
response = await self.async_request(
100+
method="POST",
101+
uri=uri,
102+
headers=headers,
103+
data=webhook,
104+
)
105+
return response
106+
except Exception as exc:
107+
exception = {"detail": str(exc)}
108+
return exception

monobank_api_client/drf_mono/__init__.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.contrib import admin
2+
from .models import Mono
3+
4+
5+
@admin.register(Mono)
6+
class MonoAdmin(admin.ModelAdmin):
7+
list_display = (
8+
"user",
9+
"mono_token",
10+
"date_joined",
11+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class DrfMonoConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "drf_mono"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Generated by Django 4.2.7 on 2023-11-29 12:51
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
import django.utils.timezone
7+
8+
9+
class Migration(migrations.Migration):
10+
initial = True
11+
12+
dependencies = [
13+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name="Mono",
19+
fields=[
20+
(
21+
"id",
22+
models.BigAutoField(
23+
auto_created=True,
24+
primary_key=True,
25+
serialize=False,
26+
verbose_name="ID",
27+
),
28+
),
29+
("mono_token", models.CharField(max_length=44, unique=True)),
30+
(
31+
"date_joined",
32+
models.DateTimeField(default=django.utils.timezone.now),
33+
),
34+
(
35+
"user",
36+
models.OneToOneField(
37+
on_delete=django.db.models.deletion.CASCADE,
38+
to=settings.AUTH_USER_MODEL,
39+
),
40+
),
41+
],
42+
),
43+
]

monobank_api_client/drf_mono/migrations/__init__.py

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from django.db import models
2+
from django.utils import timezone
3+
from django.contrib.auth import get_user_model
4+
5+
User = get_user_model()
6+
7+
8+
class Mono(models.Model):
9+
mono_token = models.CharField(
10+
max_length=44,
11+
blank=False,
12+
unique=True,
13+
)
14+
user = models.OneToOneField(User, on_delete=models.CASCADE, unique=True)
15+
date_joined = models.DateTimeField(default=timezone.now)
16+
17+
def __str__(self) -> str:
18+
return self.user.email
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from rest_framework import serializers
2+
from .models import Mono
3+
4+
5+
class MonoTokenSerializer(serializers.ModelSerializer):
6+
class Meta:
7+
model = Mono
8+
fields = ["mono_token"]
9+
extra_kwargs = {"mono_token": {"write_only": True}}
10+
11+
12+
class MonoPeriodSerializer(serializers.Serializer):
13+
period = serializers.IntegerField(min_value=0, max_value=31)
14+
15+
16+
class WebhookSerializer(serializers.Serializer):
17+
webHookUrl = serializers.URLField()
18+
19+
20+
class MonoCurrencySerializer(serializers.Serializer):
21+
currency = serializers.CharField()

0 commit comments

Comments
 (0)