-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.py
43 lines (34 loc) · 975 Bytes
/
location.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
from fastapi import FastAPI, HTTPException, Depends, status
from pydantic import BaseModel
from typing import Annotated
app = FastAPI(
title="Location Finder API",
version="1.0.0",
servers=[
{
"url": "", # ADD NGROK URL Here Before Creating GPT Action
"description": "Development Server",
}
],
)
class Location(BaseModel):
name: str
location: str
locations = {
"zia": Location(name="Zia", location="Karachi"),
"ali": Location(name="Ali", location="Lahore"),
}
# dependency function
def get_location_or_404(name: str) -> Location:
loc = locations.get(name.lower())
if not loc:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"No location found for {name}",
)
return loc
@app.get("/location/{name}")
def get_person_location(
name: str, location: Annotated[Location, Depends(get_location_or_404)]
):
return location