-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
193 lines (164 loc) · 7.02 KB
/
api.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import datetime
import json
import pydantic
from aiohttp import web
from sqlalchemy.future import select
from config.config import settings
from config.session import async_session
from model import ChatRoomModel, CommentModel, ConnectedChatRoomModel, MessageModel, UserModel
from schemas import (CommentCreateSchema, ConnectedChatRoomSchema, MassageCreateSchema,
MassageGetSchema)
from server import Server
class UserHandle:
@staticmethod
async def get(request):
async with async_session() as session, session.begin():
stmt = select(UserModel)
if user_id := request.match_info.get('user_id'):
stmt = stmt.filter(UserModel.id == user_id)
user_list = await session.execute(stmt)
users_list_obj = []
for a1 in user_list.scalars():
users_list_obj.append(a1.to_dict)
users_json = json.dumps(users_list_obj)
return web.json_response(body=users_json.encode())
@staticmethod
async def post(request):
body = await request.json()
if name := body.get('name'):
user = UserModel(name=name)
async with async_session() as session, session.begin():
session.add(user)
await session.commit()
return web.json_response(status=201)
return web.json_response(status=400)
class ChatRoomHandle:
@staticmethod
async def get(request):
async with async_session() as session, session.begin():
stmt = select(ChatRoomModel)
if chat_room_id := request.match_info.get('chat_room_id'):
stmt = stmt.filter(ChatRoomModel.id == chat_room_id)
chat_rooms_list = await session.execute(stmt)
chat_rooms_list_obj = []
for a1 in chat_rooms_list.scalars():
chat_rooms_list_obj.append(a1.to_dict)
chat_rooms_json = json.dumps(chat_rooms_list_obj)
return web.json_response(body=chat_rooms_json.encode())
@staticmethod
async def post(request):
body = await request.json()
if name := body.get('name'):
chat_room = ChatRoomModel(name=name)
async with async_session() as session, session.begin():
session.add(chat_room)
await session.commit()
return web.json_response(status=201)
return web.json_response(status=400)
class ConnectHandle:
@staticmethod
async def get(request):
user_id = request.match_info.get('user_id')
async with async_session() as session, session.begin():
stmt = select(ConnectedChatRoomModel).filter(
ConnectedChatRoomModel.user_id == user_id,
)
chat_rooms_list = await session.execute(stmt)
chat_rooms_list_obj = []
for a1 in chat_rooms_list.scalars():
chat_rooms_list_obj.append(a1.to_dict)
chat_rooms_json = json.dumps(chat_rooms_list_obj)
return web.json_response(body=chat_rooms_json.encode())
@staticmethod
async def post(request):
body = await request.json()
try:
value = ConnectedChatRoomSchema(**body)
async with async_session() as session, session.begin():
session.add(ConnectedChatRoomModel(**value.__dict__))
await session.commit()
except pydantic.error_wrappers.ValidationError as e:
return web.json_response(status=400, body=str(e).encode())
finally:
return web.json_response(status=201)
class MessageHandle:
@staticmethod
async def get(request):
body = await request.json()
try:
value = MassageGetSchema(**body)
except pydantic.error_wrappers.ValidationError as e:
return web.json_response(status=400, body=str(e).encode())
if (get_message_from := value.get_message_from) is None:
get_message_from = 0
if (get_message_to := value.get_message_to) is None:
get_message_to = datetime.datetime.now(tz=datetime.timezone.utc).timestamp()
server = Server()
connect_to_chat_at = await server.check_connect_to_chat_room(
user_id=value.author_id,
chat_room_id=value.chat_room_id
)
if connect_to_chat_at:
message_json, _ = await server.messages_for_sent_client(
chat_room_id=value.chat_room_id,
get_message_from=float(get_message_from),
get_message_to=float(get_message_to),
connect_to_chat_at=connect_to_chat_at
)
return web.json_response(body=message_json.encode())
@staticmethod
async def post(request):
body = await request.json()
try:
message = MassageCreateSchema(**body)
async with async_session() as session, session.begin():
session.add(MessageModel(**message.__dict__))
await session.commit()
except pydantic.error_wrappers.ValidationError as e:
return web.json_response(status=400, body=str(e).encode())
finally:
return web.json_response(status=201)
class CommentHandle:
@staticmethod
async def get(request):
async with async_session() as session, session.begin():
stmt = select(CommentModel)
if message_id := request.match_info.get('message_id'):
stmt = stmt.filter(CommentModel.message_id == message_id)
comment_list = await session.execute(stmt)
comment_list_obj = []
for a1 in comment_list.scalars():
comment_list_obj.append(a1.to_dict)
comments_json = json.dumps(comment_list_obj)
return web.json_response(body=comments_json.encode())
@staticmethod
async def post(request):
body = await request.json()
try:
comment = CommentCreateSchema(**body)
async with async_session() as session, session.begin():
session.add(CommentModel(**comment.__dict__))
await session.commit()
except pydantic.error_wrappers.ValidationError as e:
return web.json_response(status=400, body=str(e).encode())
finally:
return web.json_response(status=201)
app = web.Application()
app.add_routes(
[
web.get('/user/', UserHandle.get),
web.get('/user/{user_id}', UserHandle.get),
web.post('/user/', UserHandle.post),
web.get('/chat_room/', ChatRoomHandle.get),
web.get('/chat_room/{chat_room_id}', ChatRoomHandle.get),
web.post('/chat_room/', ChatRoomHandle.post),
web.get('/message/', MessageHandle.get),
web.post('/message/', MessageHandle.post),
web.get('/connect/{user_id}', ConnectHandle.get),
web.post('/connect/', ConnectHandle.post),
web.get('/comment/{message_id}', CommentHandle.get),
web.post('/comment/', CommentHandle.post),
]
)
if __name__ == '__main__':
web.run_app(app, host=settings.API_HOST, port=settings.API_PORT)