├── Instalação FastAPI-Uvicorn.png ├── TP1 - Exercício 10.png ├── TP1 - Exercício 11.png ├── TP1 - Exercício 12.1.png ├── TP1 - Exercício 12.2.png ├── TP1 - Exercício 12.3.png ├── TP1 - Exercício 2.png ├── TP1 - Exercício 3.1.png ├── TP1 - Exercício 3.2.png ├── TP1 - Exercício 5.png ├── TP1 - Exercício 6.png ├── TP1 - Exercício 7.png ├── TP1 - Exercício 8.1.png ├── TP1 - Exercício 8.2.png ├── TP1 - Exercício 9.1.png ├── TP1 - Exercício 9.2.png ├── __pycache__ ├── main.cpython-311.pyc ├── models.cpython-311.pyc ├── routes.cpython-311.pyc └── utils.cpython-311.pyc ├── main.py ├── models.py ├── routes.py └── utils.py /Instalação FastAPI-Uvicorn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/Instalação FastAPI-Uvicorn.png -------------------------------------------------------------------------------- /TP1 - Exercício 10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 10.png -------------------------------------------------------------------------------- /TP1 - Exercício 11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 11.png -------------------------------------------------------------------------------- /TP1 - Exercício 12.1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 12.1.png -------------------------------------------------------------------------------- /TP1 - Exercício 12.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 12.2.png -------------------------------------------------------------------------------- /TP1 - Exercício 12.3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 12.3.png -------------------------------------------------------------------------------- /TP1 - Exercício 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 2.png -------------------------------------------------------------------------------- /TP1 - Exercício 3.1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 3.1.png -------------------------------------------------------------------------------- /TP1 - Exercício 3.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 3.2.png -------------------------------------------------------------------------------- /TP1 - Exercício 5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 5.png -------------------------------------------------------------------------------- /TP1 - Exercício 6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 6.png -------------------------------------------------------------------------------- /TP1 - Exercício 7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 7.png -------------------------------------------------------------------------------- /TP1 - Exercício 8.1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 8.1.png -------------------------------------------------------------------------------- /TP1 - Exercício 8.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 8.2.png -------------------------------------------------------------------------------- /TP1 - Exercício 9.1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 9.1.png -------------------------------------------------------------------------------- /TP1 - Exercício 9.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/TP1 - Exercício 9.2.png -------------------------------------------------------------------------------- /__pycache__/main.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/__pycache__/main.cpython-311.pyc -------------------------------------------------------------------------------- /__pycache__/models.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/__pycache__/models.cpython-311.pyc -------------------------------------------------------------------------------- /__pycache__/routes.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/__pycache__/routes.cpython-311.pyc -------------------------------------------------------------------------------- /__pycache__/utils.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danimoreira90/Python---FastAPI/ec174312b5f609497ab4877086741a765605182f/__pycache__/utils.cpython-311.pyc -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI 2 | from routes import router 3 | 4 | app = FastAPI() 5 | app.include_router(router) 6 | -------------------------------------------------------------------------------- /models.py: -------------------------------------------------------------------------------- 1 | from pydantic import BaseModel, constr, conint 2 | from datetime import date 3 | 4 | class UserGreeting(BaseModel): 5 | username: str 6 | message: str 7 | 8 | class SimpleMessage(BaseModel): 9 | message: str 10 | 11 | class User(BaseModel): 12 | username: constr(min_length=1) 13 | age: conint(gt=0) 14 | 15 | class Item(BaseModel): 16 | name: str 17 | description: str 18 | price: float 19 | 20 | class BirthdayInfo(BaseModel): 21 | name: constr(min_length=1) 22 | birthday: date -------------------------------------------------------------------------------- /routes.py: -------------------------------------------------------------------------------- 1 | from fastapi import APIRouter, HTTPException, Path 2 | from models import UserGreeting, SimpleMessage, User, Item, BirthdayInfo 3 | from typing import Dict 4 | from utils import calculate_days_until_birthday 5 | 6 | router = APIRouter() 7 | 8 | @router.get("/", response_model=SimpleMessage) 9 | def read_root(): 10 | return SimpleMessage(message="Hello, FastAPI!") 11 | 12 | @router.get("/status", response_model=SimpleMessage) 13 | def read_status(): 14 | return SimpleMessage(message="O servidor está funcionando!") 15 | 16 | @router.get("/user/{username}", response_model=UserGreeting) 17 | def read_user(username: str): 18 | if len(username) < 3: 19 | raise HTTPException(status_code=404, detail="Usuário não encontrado") 20 | return UserGreeting(username=username, message=f"Welcome, {username}!") 21 | 22 | @router.post("/create-user", response_model=User) 23 | def create_user(user: User): 24 | return user 25 | 26 | items: Dict[int, Item] = { 27 | 1: Item(name="Tesla Model S", description="Battery-electric, four-door full-size car that has been produced by the American automaker Tesla since 2012.", price=242000), 28 | 2: Item(name="Bugatti Chiron", description="Mid-engine two-seater sports car designed in Germany by Bugatti Engineering GmbH[7] and manufactured in France, by French automobile manufacturer Bugatti Automobiles S.A.S. ", price=10000000) 29 | } 30 | 31 | @router.get("/item/{item_id}", response_model=Item) 32 | def get_item(item_id: int = Path(..., description="The ID of the item to retrieve")): 33 | if item_id in items: 34 | return items[item_id] 35 | raise HTTPException(status_code=404, detail="Item not found") 36 | 37 | @router.delete("/item/{item_id}", response_model=dict) 38 | def delete_item(item_id: int = Path(..., description="The ID of the item to delete")): 39 | if item_id in items: 40 | del items[item_id] 41 | return {"message": "Item deleted successfully"} 42 | raise HTTPException(status_code=404, detail="Item not found") 43 | 44 | @router.post("/birthday") 45 | def get_birthday_message(birthday_info: BirthdayInfo): 46 | days_until_birthday = calculate_days_until_birthday(birthday_info.birthday) 47 | return {"message": f"Hello {birthday_info.name}, there are {days_until_birthday} days until your next birthday!"} -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | from datetime import date 2 | 3 | def calculate_days_until_birthday(birthday: date) -> int: 4 | today = date.today() 5 | this_year_birthday = birthday.replace(year=today.year) 6 | 7 | if this_year_birthday < today: 8 | this_year_birthday = this_year_birthday.replace(year=today.year + 1) 9 | 10 | return (this_year_birthday - today).days --------------------------------------------------------------------------------