An open API service indexing awesome lists of open source software.

https://github.com/sean-njela/fastapi_demo

A fully customisable personal implementation of fastAPI designed to highlight professional experience. It offers a seamless integration with modern web technologies.
https://github.com/sean-njela/fastapi_demo

api devops django django-rest-framework fastapi python rest-api restful-api

Last synced: about 2 months ago
JSON representation

A fully customisable personal implementation of fastAPI designed to highlight professional experience. It offers a seamless integration with modern web technologies.

Awesome Lists containing this project

README

          

# FastAPI Todo Demo

A tiny CRUD Todo API built with [FastAPI](https://fastapi.tiangolo.com/). It uses an in-memory list for storage (great for learning and demos), showcases middleware, background tasks, and simple filtering.

## Prerequisites

* Python 3.9+
* [Poetry](https://python-poetry.org/) for dependency management
* (Optional) [Devbox](https://www.jetify.com/devbox/) for a reproducible shell

```bash
# enter the dev environment (optional)
devbox shell
```

## Installation

```bash
# install dependencies
poetry install
```

## Running the server

```bash
poetry run uvicorn app:app --host 127.0.0.1 --port 8080 --reload
# General form: uvicorn :
# Example above assumes a file named app.py with: app = FastAPI()
```

Now open:

* Swagger UI (OpenAPI): `http://127.0.0.1:8080/docs`
* ReDoc: `http://127.0.0.1:8080/redoc`

## API Overview

Base URL: `http://127.0.0.1:8080`

### Todo model

```json
{
"id": 1,
"task": "Buy milk",
"is_completed": false
}
```

### Endpoints

#### Create

`POST /todos`

```bash
curl -X POST http://127.0.0.1:8080/todos \
-H "Content-Type: application/json" \
-d '{"task":"Buy milk"}'
```

#### Read (all, with optional filter)

`GET /todos?completed=true|false`

```bash
curl "http://127.0.0.1:8080/todos"
curl "http://127.0.0.1:8080/todos?completed=true"
```

#### Read (by id)

`GET /todos/{id}`

```bash
curl http://127.0.0.1:8080/todos/1
```

#### Update

`PUT /todos/{id}`

> Send the full Todo object in the body. The id in the path is authoritative.

```bash
curl -X PUT http://127.0.0.1:8080/todos/1 \
-H "Content-Type: application/json" \
-d '{"id":1,"task":"Buy oat milk","is_completed":true}'
```

#### Delete

`DELETE /todos/{id}`

```bash
curl -X DELETE http://127.0.0.1:8080/todos/1
```

## What the demo shows

* **FastAPI basics:** Path/verb handlers for CRUD.
* **Pydantic models:** `Todo` for request/response validation.
* **Middleware:** Simple request timing logger.
* **Background tasks:** Simulated email notification after creating a todo.
* **CORS:** Wide-open configuration for local testing.
* **OpenAPI docs:** Auto-generated Swagger UI at `/docs`.

## Project structure (suggested)

```
.
├── app.py # FastAPI app (see snippet below)
├── pyproject.toml # Poetry config
└── README.md
```

## Code sample (for reference)

```python
class Todo(BaseModel):
"""
Model for Todos.
Inherits from pydantic's BaseModel (acts like a dataclass with validation).
"""
id: Optional[int] = None # Optional: DBs usually handle this
task: str # Always use 'str' (not 'string')
is_completed: bool = False # Default value

# CREATE
@app.post("/todos")
async def add_todo(todo: Todo, background_task: BackgroundTasks):
todo.id = len(todos_list) + 1
todos_list.append(todo)
background_task.add_task(send_email, todo) # background side-effect
return todo
```

## Notes & gotchas

* The storage is **in-memory**; data resets when the server restarts.
* Function names in a single Python module should be unique.
* `HTTPException` is used to return proper HTTP status codes.
* For a real app, add a database (e.g., SQLite/Postgres), auth, logging, and tests.

## License

MIT

## Author

Sean Njela