-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.py
176 lines (159 loc) · 6.16 KB
/
history.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from datetime import datetime
from typing import Optional
import aiohttp
import json
from yoomoney_async.operation.operation import Operation
from .exceptions import (
IllegalParamType,
IllegalParamStartRecord,
IllegalParamRecords,
IllegalParamLabel,
IllegalParamFromDate,
IllegalParamTillDate,
TechnicalError
)
class History:
def __init__(self,
base_url: str = None,
token: str = None,
method: str = None,
type: str = None,
label: str = None,
from_date: Optional[datetime] = None,
till_date: Optional[datetime] = None,
start_record: str = None,
records: int = None,
details: bool = None,
):
self.__private_method = method
self.__private_base_url = base_url
self.__private_token = token
self.type = type
self.label = label
try:
if from_date is not None:
from_date = "{Y}-{m}-{d}T{H}:{M}:{S}+03:00".format(
Y=str(from_date.year),
m=str(from_date.month),
d=str(from_date.day),
H=str(from_date.hour),
M=str(from_date.minute),
S=str(from_date.second)
)
except Exception:
raise IllegalParamFromDate()
try:
if till_date is not None:
till_date = "{Y}-{m}-{d}T{H}:{M}:{S}+03:00".format(
Y=str(till_date.year),
m=str(till_date.month),
d=str(till_date.day),
H=str(till_date.hour),
M=str(till_date.minute),
S=str(till_date.second)
)
except Exception:
IllegalParamTillDate()
self.from_date = from_date
self.till_date = till_date
self.start_record = start_record
self.records = records
self.details = details
self.operations = list()
async def start(self):
data = await self._request()
if "error" in data:
if data["error"] == "illegal_param_type":
raise IllegalParamType()
elif data["error"] == "illegal_param_start_record":
raise IllegalParamStartRecord()
elif data["error"] == "illegal_param_records":
raise IllegalParamRecords()
elif data["error"] == "illegal_param_label":
raise IllegalParamLabel()
elif data["error"] == "illegal_param_from":
raise IllegalParamFromDate()
elif data["error"] == "illegal_param_till":
raise IllegalParamTillDate()
else:
raise TechnicalError()
for operation_data in data["operations"]:
param = {}
if "operation_id" in operation_data:
param["operation_id"] = operation_data["operation_id"]
else:
param["operation_id"] = None
if "status" in operation_data:
param["status"] = operation_data["status"]
else:
param["status"] = None
if "datetime" in operation_data:
param["datetime"] = datetime.strptime(
str(operation_data["datetime"]).replace("T", " ").replace("Z", ""), '%Y-%m-%d %H:%M:%S')
else:
param["datetime"] = None
if "title" in operation_data:
param["title"] = operation_data["title"]
else:
param["title"] = None
if "pattern_id" in operation_data:
param["pattern_id"] = operation_data["pattern_id"]
else:
param["pattern_id"] = None
if "direction" in operation_data:
param["direction"] = operation_data["direction"]
else:
param["direction"] = None
if "amount" in operation_data:
param["amount"] = operation_data["amount"]
else:
param["amount"] = None
if "label" in operation_data:
param["label"] = operation_data["label"]
else:
param["label"] = None
if "type" in operation_data:
param["type"] = operation_data["type"]
else:
param["type"] = None
operation = Operation(
operation_id=param["operation_id"],
status=param["status"],
datetime=datetime.strptime(str(param["datetime"]).replace("T", " ").replace("Z", ""),
'%Y-%m-%d %H:%M:%S'),
title=param["title"],
pattern_id=param["pattern_id"],
direction=param["direction"],
amount=param["amount"],
label=param["label"],
type=param["type"],
json=operation_data,
)
self.operations.append(operation)
return self
async def _request(self):
access_token = str(self.__private_token)
url = self.__private_base_url + self.__private_method
headers = {
'Authorization': 'Bearer ' + str(access_token),
'Content-Type': 'application/x-www-form-urlencoded'
}
payload = {}
if self.type is not None:
payload["type"] = self.type
if self.label is not None:
payload["label"] = self.label
if self.from_date is not None:
payload["from"] = self.from_date
if self.till_date is not None:
payload["till"] = self.till_date
if self.start_record is not None:
payload["start_record"] = self.start_record
if self.records is not None:
payload["records"] = self.records
if self.details is not None:
payload["details"] = self.details
async with aiohttp.ClientSession() as client:
async with client.post(url, headers=headers, data=payload) as response:
result = await response.text()
return json.loads(result)