-
Notifications
You must be signed in to change notification settings - Fork 0
/
without_redis.py
executable file
·52 lines (36 loc) · 997 Bytes
/
without_redis.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
#!/usr/bin/env python3
import argparse
import requests
import uvicorn
from fastapi import FastAPI, HTTPException
URL = "https://jsonplaceholder.typicode.com/photos"
app = FastAPI()
def get_port() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("port", type=int)
args = parser.parse_args()
port = vars(args)["port"]
return port
def get_single(id: int):
response = requests.get(f"{URL}/{id}")
data = response.json()
if response.status_code != 200:
raise HTTPException(status_code=500)
return data
def get_many():
response = requests.get(URL)
data = response.json()
if response.status_code != 200:
raise HTTPException(status_code=500)
return data
@app.get("/photos")
def get_photos():
photos = get_many()
return photos
@app.get("/photos/{id}")
def get_photo(id: int):
photo = get_single(id)
return photo
if __name__ == "__main__":
port = get_port()
uvicorn.run(app, port=port)