-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
61 lines (49 loc) · 1.44 KB
/
app.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
# -*- coding: utf-8 -*-
'''
Simple API
'''
import base64
import json
from time import time
import requests
import uvicorn
from fastapi import FastAPI, Request
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.middleware.cors import CORSMiddleware
_ORIGINS = [
"http://localhost",
"http://localhost:8080",
"http://localhost:8000",
"http://localhost:4200",
"https://wvcode-persona.herokuapp.com"
]
app = FastAPI(title='API Model')
app.add_middleware(GZipMiddleware, minimum_size=1000)
@app.middleware('http')
async def remove_file_before_leave(request: Request, call_next):
start_time = time()
response = await call_next(request)
process_time = time()
response.headers['X-Process-Time'] = str(process_time - start_time)
return response
app.add_middleware(
CORSMiddleware,
allow_origins=_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get('/', tags=['Version 1'])
async def get_main():
response = requests.get('https://randomuser.me/api/')
return response.json()
@app.get('/xkcd/{id}', tags=['XKCD'])
async def get_comic(id: str):
response = None
if id == 'last':
response = requests.get('http://xkcd.com/info.0.json')
elif id.isnumeric():
response = requests.get('http://xkcd.com/{0}/info.0.json'.format(id))
return response.json()
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=8000, debug=True)