-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
83 lines (63 loc) · 2.11 KB
/
main.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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, constr
import tpo
import mongodb
import phone_number
import website
import verifier
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["*"],
)
class TPOGenerate(BaseModel):
website: str
phone_number: str
mongodb_init = mongodb.setup_mongodb()
@app.post("/tpo/generate")
async def tpo_generate(login_data: TPOGenerate):
valid_phone_number = phone_number.is_valid(login_data.phone_number)
if not valid_phone_number:
return verifier.non_valid_phone_number
valid_website = website.is_valid(login_data.website)
if not valid_website:
return verifier.non_valid_website
exist = await tpo.already_exists(
login_data.phone_number, login_data.website, mongodb_init["collection"]
)
if exist:
return verifier.tpo_already_exists(login_data.phone_number, login_data.website)
tpo_code = await tpo.new_tpo(
login_data.phone_number, login_data.website, mongodb_init["collection"]
)
if not tpo_code["ok"]:
return verifier.tpo_failed_to_generate(
login_data.phone_number, login_data.website
)
return verifier.tpo_generated(login_data.phone_number, login_data.website)
class TPOVerify(BaseModel):
website: str
phone_number: str
tpo: constr(min_length=6, max_length=6)
@app.post("/tpo/verify")
async def tpo_verify(login_data: TPOVerify):
valid_tpo = tpo.is_valid(login_data.tpo)
if not valid_tpo:
return verifier.tpo_non_valid
valid_phone_number = phone_number.is_valid(login_data.phone_number)
if not valid_phone_number:
return verifier.non_valid_phone_number
valid_website = website.is_valid(login_data.website)
if not valid_website:
return verifier.non_valid_website
verify = await tpo.verify_tpo(
login_data.website,
login_data.phone_number,
login_data.tpo,
mongodb_init["collection"],
)
return verify