This repository was archived by the owner on Nov 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathget_async_api.py
More file actions
149 lines (121 loc) · 4.87 KB
/
Copy pathget_async_api.py
File metadata and controls
149 lines (121 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from datetime import datetime
from typing import List, Dict, Any, Optional
from urllib.parse import urlencode, quote
from .get_api import DEFAULT_URL, DEFAULT_API_LINK
from utils.base import AIODonationAlertsAPIBase
from utils.models import (
Scope,
AccessToken,
DonationsData,
Donations,
User,
CustomAlert
)
class AIODonationAlertsAPIAuthorize(AIODonationAlertsAPIBase):
def __init__(
self,
client_id: str,
client_secret: str,
redirect_uri: str,
scopes: str | List[Scope]
) -> None:
super().__init__(
client_id,
client_secret,
redirect_uri,
scopes
)
async def login(self) -> str:
payload: Dict[str, Any] = {
"client_id": self.client_id,
"redirect_uri": self.redirect_uri,
"response_type": "code",
"scope": self.scopes
}
return f"{DEFAULT_URL}authorize?{urlencode(payload, quote_via=quote)}"
async def get_access_token(self, code: str) -> AccessToken:
payload: Dict[str, Any] = {
"client_id": self.client_id,
"client_secret": self.client_secret,
"grant_type": "authorization_code",
"code": code,
"redirect_uri": self.redirect_uri,
"scope": self.scopes,
}
async with self.session.post(f"{DEFAULT_URL}token", data=payload) as response:
response_obj: Dict[str, Any] = await response.json()
return AccessToken(**response_obj)
async def refresh_access_token(self, refresh_token: str) -> AccessToken:
headers: Dict[str, str] = {
"Content-Type": "application/x-www-form-urlencoded"
}
payload: Dict[str, Any] = {
"grant_type": "refresh_token",
"refresh_token": refresh_token,
"client_id": self.client_id,
"client_secret": self.client_secret,
"scope": self.scopes
}
async with self.session.post(f"{DEFAULT_URL}token", headers=headers, data=payload) as response:
response_obj: Dict[str, Any] = await response.json()
return AccessToken(**response_obj)
class AIODonationAlertsAPIUser(AIODonationAlertsAPIBase):
def __init__(self) -> None:
pass
async def get(self, access_token: str) -> User:
headers: Dict[str, str] = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/x-www-form-urlencoded",
}
async with self.session.get(f"{DEFAULT_API_LINK}user/oauth", headers=headers) as response:
response_obj: Dict[str, Any] = await response.json()
return User(**response_obj)
class AIODonationAlertsAPIDonations(AIODonationAlertsAPIBase):
def __init__(self) -> None:
pass
async def get(self, access_token: str, page: int=1) -> Donations:
headers: Dict[str, str] = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/x-www-form-urlencoded",
}
async with self.session.get(f"{DEFAULT_API_LINK}alerts/donations?page={page}", headers=headers) as response:
response_obj: Dict[str, Any] = await response.json()
donations: DonationsData = [
DonationsData(
created_at=datetime.strptime(item["created_at"], "%Y-%m-%d %H:%M:%S"),
**{key: value for key, value in item.items() if key != "created_at"}
)
if "created_at" in item and isinstance(item["created_at"], str)
else DonationsData(**item)
for item in response_obj["data"]
]
return Donations(items=donations, links=response_obj["links"], meta=response_obj["meta"])
class AIODonationAlertsAPICustomAlert(AIODonationAlertsAPIBase):
def __init__(self) -> None:
pass
async def send(
self,
access_token: str,
external_id: str,
header: str,
message: str,
*,
is_shown: int=0,
image_url: Optional[str] | None=None,
sound_url: Optional[str] | None=None
) -> CustomAlert:
headers: Dict[str, str] = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/x-www-form-urlencoded",
}
payload: Dict[str, Any] = {
"external_id": external_id,
"header": header,
"message": message,
"is_shown": is_shown,
"image_url": image_url,
"sound_url": sound_url
}
async with self.session.post(f"{DEFAULT_API_LINK}custom_alert", headers=headers, data=payload) as response:
response_obj: Dict[str, Any] = await response.json()
return CustomAlert(**response_obj["data"])