-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
144 lines (104 loc) · 5.02 KB
/
handler.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
# Built-in library
import json
from uuid import uuid4
# Third-party libray
from rx.core import Observer
from aiohttp import web
from aio_pika import Message, DeliveryMode
class OrderHandler(Observer):
def __init__(self):
super().__init__()
def on_next(self, request: web.Request):
loop = request['loop']
async def asyncOrder():
future = request['future']
body = request['body']
channel = request['channel']
queue = request['queue']
clientRef = body['client_ref']
account = body['account']
action = body['action']
stock = body['stock']
price = body['price']
vol = body['vol']
orderId = str(uuid4())[:8]
msg = f"{account}|{action}|{stock}.{price}.{vol}.{orderId}"
msg = Message(msg.encode(), delivery_mode=DeliveryMode.PERSISTENT)
await channel.default_exchange.publish(msg, routing_key=queue)
future.set_result(web.json_response({'status':'OK', 'order_id': orderId, 'client_ref':clientRef}))
loop.create_task(asyncOrder())
def orderVerificator(self, request: web.Request) -> bool:
body = request['body']
future = request['future']
verified = True
paramsExpect = ('client_ref', 'account', 'action', 'stock', 'price', 'vol')
paramsMissing = list(filter(lambda i: i not in body, paramsExpect))
if len(paramsMissing) > 0:
body['verified'] = False
future.set_result(web.json_response({'status':'FAIL', 'order_id': None, 'client_ref': None, 'reason':f"Missing parameter {','.join(paramsMissing)}"},
status=422))
return False
if body['action'] not in ('BUY', 'SELL'):
verified = False
reason = 'Action must be BUY or SELL'
if not isinstance(body['price'], int) or not isinstance(body['vol'], int):
verified = False
reason = 'Price and volume must be integer'
elif body['price'] <= 0 or body['vol'] <= 0:
verified = False
reason = 'Price and volume must be > 0'
if not verified:
future.set_result(web.json_response({'status':'FAIL', 'order_id': None, 'client_ref': body['client_ref'], 'reason': reason},
status=422))
return verified
class AccountHandler(Observer):
def __init__(self):
super().__init__()
def on_next(self, request: web.Request):
loop = request['loop']
async def asyncAccount():
body = request['body']
future = request['future']
channel = request['channel']
queue = request['queue']
clientRef = body['client_ref']
action = body['action']
account = body['params']['account']
amount = body['params']['amount']
requestId = str(uuid4())[:8]
msg = json.dumps({'action':action, 'params':{'account':account, 'amount':amount}})
msg = Message(msg.encode(), delivery_mode=DeliveryMode.PERSISTENT)
await channel.default_exchange.publish(msg, routing_key=queue)
future.set_result(web.json_response({'status':'OK', 'request_id': requestId, 'client_ref':clientRef}))
loop.create_task(asyncAccount())
def accountVerificator(self, request: web.Request) -> bool:
body = request['body']
future = request['future']
verified = True
paramsExpect = ('client_ref', 'action', 'params')
paramsMissing = list(filter(lambda i: i not in body, paramsExpect))
if len(paramsMissing) > 0:
future.set_result(web.json_response({'status':'FAIL', 'request_id': None, 'client_ref': None,
'reason':f"Missing parameter {', '.join(paramsMissing)}"},
status=422))
return False
if not isinstance(body['params'], dict):
future.set_result(web.json_response({'status': 'FAIL', 'request_id': None, 'client_ref': None,
'reason': f"params field is not an object"}, status=422))
return False
if body['action'] not in ('DEPOSIT', 'WITHDRAW'):
verified = False
reason = 'Action must be DEPOSIT or WITHDRAW'
if not isinstance(body['params'].get('amount'), int):
verified = False
reason = 'Amount must be integer'
elif body['params']['amount'] <= 0 :
verified = False
reason = 'Amount must be > 0'
if body['params'].get('account') is None:
verified = False
reason = 'Missing account parameter'
if not verified:
future.set_result(web.json_response({'status':'FAIL', 'request_id': None, 'client_ref': body['client_ref'], 'reason': reason},
status=422))
return verified