-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfastapi-example.py
90 lines (64 loc) · 2.11 KB
/
fastapi-example.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
import asyncio
import uvicorn
from autometrics import autometrics
from fastapi import FastAPI, Response
from fastapi.responses import JSONResponse
from prometheus_client import generate_latest
app = FastAPI()
# Set up a metrics endpoint for Prometheus to scrape
# `generate_latest` returns the latest metrics data in the Prometheus text format
@app.get("/metrics")
def metrics():
return Response(generate_latest())
# Set up the root endpoint of the API
@app.get("/")
@autometrics
def read_root():
do_something()
return {"Hello": "World"}
# Set up an async handler
@app.get("/async")
@autometrics
async def async_route():
message = await do_something_async()
return {"Hello": message}
@autometrics
def do_something():
print("done")
@autometrics
async def do_something_async():
print("async start")
await asyncio.sleep(2.0)
print("async done")
return "async world"
def response_is_error(response: Response):
if response.status_code >= 400:
return True
@app.get("/not-implemented")
@autometrics(record_error_if=response_is_error)
def not_implemented():
return JSONResponse(
status_code=501, content={"message": "This endpoint is not implemented"}
)
@app.get("/flowers/{flower_name}")
def flower(flower_name: str):
try:
return JSONResponse(content={"message": get_pretty_flower(flower_name)})
except NotFoundError as error:
return JSONResponse(status_code=404, content={"message": str(error)})
class NotFoundError(Exception):
pass
def is_not_found_error(error: Exception):
return isinstance(error, NotFoundError)
@autometrics(record_success_if=is_not_found_error)
def get_pretty_flower(flower_name: str):
"""Returns whether the flower is pretty"""
print(f"Getting pretty flower for {flower_name}")
flowers = ["rose", "tulip", "daisy"]
if flower_name not in flowers:
raise NotFoundError(
f"Flower {flower_name} not found. Perhaps you meant one of these: {', '.join(flowers)}?"
)
return f"A {flower_name} is pretty"
if __name__ == "__main__":
uvicorn.run(app, host="localhost", port=8080)