Skip to content

Instantly share code, notes, and snippets.

@jrgavilanes
Created July 7, 2023 06:17
Show Gist options
  • Save jrgavilanes/9f537feb212f8ae994f93729de87d323 to your computer and use it in GitHub Desktop.
Save jrgavilanes/9f537feb212f8ae994f93729de87d323 to your computer and use it in GitHub Desktop.
ejemplo completo REST con fastapi
from typing import Optional
import uvicorn
from fastapi import FastAPI, HTTPException, Path
from pydantic import BaseModel, Field
from starlette import status
app = FastAPI()
class Libro:
id: int
title: str
author: str
description: str
rating: int
def __init__(self, id, title, author, description, rating):
self.id = id
self.title = title
self.author = author
self.description = description
self.rating = rating
class LibroRequest(BaseModel):
id: Optional[int] = Field(title="not needed in inserts")
title: str = Field(min_length=1, max_length=100)
author: str = Field(min_length=1, max_length=100)
description: str = Field(min_length=1, max_length=255)
rating: int = Field(gt=0, lt=6)
class Config:
schema_extra = {
'example': {
"title": "libro 7",
"author": "author 1",
"description": "description 7",
"rating": 5
}
}
LIBROS = [
Libro(id=1, title="libro 1", author="author 1", description="description 1", rating=5),
Libro(id=2, title="libro 2", author="author 2", description="description 2", rating=4),
Libro(id=3, title="libro 3", author="author 2", description="description 3", rating=4),
Libro(id=4, title="libro 4", author="author 1", description="description 4", rating=3),
Libro(id=5, title="libro 5", author="author 3", description="description 5", rating=2),
Libro(id=6, title="libro 6", author="author 1", description="description 6", rating=1),
]
@app.get("/libros", status_code=status.HTTP_200_OK)
async def get_libros():
return LIBROS
@app.get("/libros/{id}", status_code=status.HTTP_200_OK)
async def get_libro(id: int = Path(gt=0)):
for libro in LIBROS:
if libro.id == id:
return libro
raise HTTPException(status_code=404, detail=f'Item {id} not found')
@app.post("/libros", status_code=status.HTTP_201_CREATED)
async def post_libro(libro_request: LibroRequest):
new_record = Libro(**libro_request.dict())
new_record.id = 1 if len(LIBROS) == 0 else LIBROS[-1].id + 1
LIBROS.append(new_record)
@app.put("/libros", status_code=status.HTTP_204_NO_CONTENT)
async def put_libro(libro_request: LibroRequest):
for i in range(len(LIBROS)):
if LIBROS[i].id == libro_request.id:
LIBROS[i].title = libro_request.title
LIBROS[i].author = libro_request.author
LIBROS[i].description = libro_request.description
LIBROS[i].rating = libro_request.rating
return
raise HTTPException(status_code=404, detail=f'Item {libro_request.id} not found')
@app.delete("/libros/{book_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_book(book_id: int = Path(gt=0)):
for i in range(len(LIBROS)):
if LIBROS[i].id == book_id:
LIBROS.pop(i)
return
raise HTTPException(status_code=404, detail=f'Item {book_id} not found')
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=7000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment