Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save encryptblockr/c0934e96198990239163ba67bba951ef to your computer and use it in GitHub Desktop.
Save encryptblockr/c0934e96198990239163ba67bba951ef to your computer and use it in GitHub Desktop.
from fastapi import FastAPI, Query
from fastapi.responses import JSONResponse
from typing import Optional
from pydantic import BaseModel, Field
tags_metadata = [
{
"name": "create-user",
"description": "Create new user based on *ID*. Will **overwrite** existing user.",
},
{
"name": "get-user",
"description": "Get the name of user based on *ID*. Admin access required if `id == '007'`",
},
]
app = FastAPI(
title="Users Management System",
description="API to get and create users.",
version="1.1.2",
openapi_tags=tags_metadata,
)
class Message(BaseModel):
message: str
class User(BaseModel):
id: str = Field(..., title="3-digit identity number of the user", example="010")
name: str = Field(..., title="Name of the user", example="Jane Doe")
class Result(BaseModel):
status_code: str = Field(..., title="Status code of the response. `0 = no error`, `1 = error`", example="0")
status_message: str = Field(..., title="Status message of the response, either Succcess or failure message", example="Success")
data: Optional[User] = Field(None, title="Additional data returned by the API")
# sample data pulled from database
users = {"000": "admin","001": "Wai Foong", "002": "Jane", "003": "Jessie", "007": "Five Six Seven"}
# pre-defined responses
responses = {
403: {"model": Message, "description": "Insufficient privileges for this action"},
}
@app.post("/create-user", tags=["create-user"], response_model=Result,
responses={
**responses,
200: {
"description": "Successfully created new user",
"content": {
"application/json": {
"example": {'status_code': '0', 'status_message' : 'Success', 'data': {'id': '001', 'name': 'John Doe'}}
}
},
},
},)
async def create_user(user: User):
if id == "000":
return {"status_code": "1", "status_message" : "Failed"}
elif id == "007":
return JSONResponse(status_code=403, content={"message": "Insufficient privileges!"})
users[user.id] = user.name
return {"status_code": "0", "status_message" : "Success", "data": {"id": user.id, "name": user.name}}
@app.get("/get-user", tags=["get-user"], response_model=Result,
responses={
**responses, # unpacking pre-defined responses
404: {
"model": Message,
"description": "No user with this ID in the database",
},
200: {
"description": "Successfully retrieved information of the user",
"content": {
"application/json": {
"example": {'status_code': '0', 'status_message' : 'Success', 'data': {'id': '001', 'name': 'John Doe'}}
}
},
},
},)
async def get_user(id: str = Query(..., title="3-digit identity number of the user", example="010")):
if id in users:
if id == "007":
return JSONResponse(status_code=403, content={"message": "Insufficient privileges!"})
return {"status_code": "0", "status_message" : "Success", "data": {"id": id, "name": users[id]}}
else:
return JSONResponse(status_code=404, content={"message": "User not found!"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment