Python | Algorithms | Data Structures | Cyber ​​Security | Networks
38.6K subscribers
778 photos
23 videos
21 files
713 links
This channel is for Programmers, Coders, Software Engineers.

1) Python
2) django
3) python frameworks
4) Data Structures
5) Algorithms
6) DSA

Admin: @Hussein_Sheikho

Ad & Earn money form your channel:
https://telega.io/?r=nikapsOH
Download Telegram
📚 Building Python Microservices with FastAPI (2024)

1⃣ Join Channel Download:
https://t.iss.one/+MhmkscCzIYQ2MmM8

2⃣ Download Book: https://t.iss.one/c/1854405158/1362

💬 Tags: #FastAPI

👉 BEST DATA SCIENCE CHANNELS ON TELEGRAM 👈
👍41
📚 FastAPI Cookbook (2024)

1⃣ Join Channel Download:
https://t.iss.one/+MhmkscCzIYQ2MmM8

2⃣ Download Book: https://t.iss.one/c/1854405158/1785

💬 Tags: #FastAPI

USEFUL CHANNELS FOR YOU
👍31
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
return {"filename": file.filename, "content_type": file.content_type}

• Set a response cookie.
from fastapi import Response
@app.post("/cookie/")
def create_cookie(response: Response):
response.set_cookie(key="fakesession", value="fake-cookie-session-value")
return {"message": "Cookie set"}

• Read a request cookie.
from fastapi import Cookie
@app.get("/read-cookie/")
def read_cookie(fakesession: Optional[str] = Cookie(None)):
return {"session": fakesession}


X. Error Handling & Advanced Responses

• Handle HTTPException.
from fastapi import HTTPException
@app.get("/items/{item_id}")
def read_item(item_id: str):
if item_id not in items_db:
raise HTTPException(status_code=404, detail="Item not found")

• Return a custom JSONResponse.
from fastapi.responses import JSONResponse
@app.get("/custom/")
def custom_response():
return JSONResponse(status_code=202, content={"message": "Accepted"})

• Return an HTMLResponse.
from fastapi.responses import HTMLResponse
@app.get("/page", response_class=HTMLResponse)
def read_page():
return "<h1>Hello World</h1>"

• Define a WebSocket endpoint.
from fastapi import WebSocket
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
await websocket.send_text("Hello from WebSocket")
await websocket.close()

• Handle a path operation that may not exist.
@app.get("/{full_path:path}")
def read_catch_all(full_path: str):
return {"path": full_path}

• Mount a static files directory.
from fastapi.staticfiles import StaticFiles
app.mount("/static", StaticFiles(directory="static"), name="static")

• Add a custom exception handler.
from fastapi import Request
from fastapi.responses import JSONResponse

class MyCustomException(Exception): pass

@app.exception_handler(MyCustomException)
async def custom_exception_handler(request: Request, exc: MyCustomException):
return JSONResponse(status_code=418, content={"message": "I'm a teapot"})

• Read a request header.
from fastapi import Header
@app.get("/headers/")
async def read_headers(user_agent: Optional[str] = Header(None)):
return {"User-Agent": user_agent}

• Get a raw Request object.
from fastapi import Request
@app.get("/request-info")
def get_request_info(request: Request):
return {"client_host": request.client.host}


#Python #FastAPI #API #WebDevelopment #Pydantic

━━━━━━━━━━━━━━━
By: @DataScience4