-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSprecialDataTypes.py
92 lines (76 loc) · 2.34 KB
/
SprecialDataTypes.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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import nest_asyncio
from pyngrok import ngrok
import uvicorn
from pydantic.types import PaymentCardBrand, PaymentCardNumber, constr #Special datatypes
from pdantic.color import Color
from datetime import datetime, time, timedelta
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)
@app.get('/')
async def root():
return {'hello': 'world'}
## Special Datatypes(date and time, PaymentCardNumber, color)
# datetime Type
@app.post("/items")
async def update_item(*,
start_datetime: datetime = Body(None),
end_datetime: datetime = Body(None),
repeat_at: time = Body(None),
process_after: timedelta = Body(None),):
start_process = start_datetime + process_after
duration = end_datetime - start_process
return {
"start_datetime": start_datetime,
"end_datetime": end_datetime,
"repeat_at": repeat_at,
"process_after": process_after,
"start_process": start_process,
"duration": duration,
"process_after": process_after.seconds
}
# Color type
@app.post("/items")
async def update_item(*, setColor: Color,):
return {
"Item color": setColor,
"Name": setColor.as_named(),
"Hex": setColor.as_hex(),
"RGB": setColor.as_rgb_tuple()
}
# Payment Card type
@app.post("/items")
async def update_item(*, cardNumber: PaymentCardNumber, cardBrand: PaymentCardBrand):
return {
"Card Number": cardNumber,
"Brand": cardBrand
}
class Card(BaseModel):
name: constr(strip_whitespace=True, min_length=1)
number: PaymentCardNumber
exp: datetime
@property
def brand(self) -> PaymentCardBrand:
return self.number.brand
@property
def expired(self) -> bool:
return self.exp < datetime.today()
@app.post("/pay")
async def update_payType(*, card: Card):
return {
"Brand": card.number.brand ,
"Bin": card.number.bin,
"Last4 Digits": card.number.last4,
"Masked Num": card.number.masked
}
ngrok_tunnel = ngrok.connect(8000)
print('Public URL:', ngrok_tunnel.public_url)
nest_asyncio.apply()
uvicorn.run(app, port=8000)