Magery183

Random FastAPI api

Mar 28th, 2022 (edited)
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. # --------------------------------------------------------------------------------------------------------------------------
  2. # Random Fast API application lmao
  3.  
  4. from fastapi import FastAPI
  5. from pydantic import BaseModel
  6.  
  7. app = FastAPI()
  8.  
  9.  
  10. class BioSummary(BaseModel):
  11.     name: str
  12.     age: int
  13.     hobbies: list
  14.     fav_food: str
  15.     school: str
  16.  
  17.  
  18. @app.get("/")
  19. async def root():
  20.     return {"message": "Hello World"}
  21.  
  22.  
  23. @app.get("/hello/{name}")
  24. async def say_hello(name: str):
  25.     return {"message": f"Hello {name}"}
  26.  
  27.  
  28. @app.post('/bio/create')
  29. async def create_bio(summary: BioSummary):
  30.     hobbies = []
  31.     if not len(summary.hobbies) == 1:
  32.         for h in range(len(summary.hobbies)):
  33.             if h == len(summary.hobbies) - 1:
  34.                 hobbies.append(f'and {summary.hobbies[h]}')
  35.             else:
  36.                 hobbies.append(summary.hobbies[h]+",")
  37.     else:
  38.         hobbies.append(summary.hobbies[0])
  39.     bio = f'Hello, my name is {summary.name}, I am {summary.age} and my hobbies are {" ".join(hobbies)}. My favourite food is {summary.fav_food} and I go to {summary.school}.'
  40.     return bio
Add Comment
Please, Sign In to add comment