-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
110 lines (98 loc) · 4.28 KB
/
Copy pathserver.py
File metadata and controls
110 lines (98 loc) · 4.28 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
import asyncio
import logging
from grpc import aio
from grpc_generated_files import auth_pb2_grpc, auth_pb2
from helpers.auth import AuthHelper
from helpers.user import UserHelper
class AuthServicer(auth_pb2_grpc.AuthServiceServicer):
async def register(self, request, context):
print("AuthServicer received request from GatewayClient")
status_code, result = await UserHelper.register_user(email=request.email, password=request.password,
phone=request.phone, last_name=request.last_name,
first_name=request.first_name,
middle_name=request.middle_name)
if status_code == 200:
user = auth_pb2.User(id=str(result.id), email=result.email, phone=result.phone,
last_name=result.last_name, first_name=result.first_name,
middle_name=result.middle_name)
response = auth_pb2.RegisterUserResponse(
success_payload=user,
error=False,
status_code=status_code,
)
return response
else:
response = auth_pb2.RegisterUserResponse(
error_payload=result,
error=True,
status_code=status_code
)
return response
async def login(self, request, context):
print("AuthServicer received request from GatewayClient")
status_code, result = await UserHelper.login(email=request.email, password=request.password)
if status_code == 200:
tokens = auth_pb2.TokenSchema(access_token=result['access_token'], refresh_token=result['refresh_token'], )
response = auth_pb2.LoginResponse(
success_payload=tokens,
error=False,
status_code=status_code,
)
return response
else:
response = auth_pb2.LoginResponse(
error_payload=result,
error=True,
status_code=status_code
)
return response
async def refresh_token(self, request, context):
print("AuthServicer received request from GatewayClient")
status_code, result = await AuthHelper.refresh_token(refresh_token=request.refresh_token)
if status_code == 200:
tokens = auth_pb2.TokenSchema(access_token=result)
response = auth_pb2.RefreshTokenResponse(
success_payload=tokens,
error=False,
status_code=status_code,
)
return response
else:
response = auth_pb2.RefreshTokenResponse(
error_payload=result,
error=True,
status_code=status_code
)
return response
async def validate_access_token(self, request, context):
print("AuthServicer received request from GatewayClient")
status_code, result = await AuthHelper.validate_access_token(access_token=request.access_token)
if status_code == 200:
user = auth_pb2.User(id=str(result.id), email=result.email, phone=result.phone,
last_name=result.last_name, first_name=result.first_name,
middle_name=result.middle_name)
response = auth_pb2.ValidateTokenResponse(
success_payload=user,
error=False,
status_code=status_code,
)
return response
else:
response = auth_pb2.ValidateTokenResponse(
error_payload=result,
error=True,
status_code=status_code
)
return response
async def serve():
# server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
server = aio.server()
listen_addr = "[::]:9999"
auth_pb2_grpc.add_AuthServiceServicer_to_server(AuthServicer(), server)
server.add_insecure_port(listen_addr)
logging.info("Starting server on %s", listen_addr)
await server.start()
await server.wait_for_termination()
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
asyncio.run(serve())