-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathREADME.md-tpl
More file actions
156 lines (124 loc) · 3.82 KB
/
Copy pathREADME.md-tpl
File metadata and controls
156 lines (124 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Async Item Management API with Custom Response System
A FastAPI-based REST API demonstrating asynchronous processing and standardized response handling.
## Tech Stack
- Python 3.12+
- FastAPI + uvicorn
- Pydantic 2 & pydantic-settings
- pytest, pytest-asyncio
- mypy + black + isort
- aiofiles (async file handling)
## Project Structure
```
.
├── src/
│ ├── core
│ │ └── config.py
│ └── api
│ │ ├── api.py
│ │ └── routes
│ │ └── items.py
│ ├── schemas
│ │ ├── base.py # Base response schema
│ │ └── items.py
│ ├── helper
│ │ ├── exceptions.py # Exception handlers
│ │ └── pagination.py # Paginator
│ ├── crud
│ │ └── items.py
│ ├── mocks
│ │ └── mock_items.json
│ └── utils
│ └── documents.py # custom OpenAPI docs generator
└── tests
├── __init__.py
├── test_items.py
└── conftest.py
```
## How to Run
Run in virtual environment:
```bash
# Install dependencies
$ pip install -r requirements.txt
# Start development server (using script)
$ bash scripts/run-server.sh
# Manual run
$ uvicorn src.main:app --reload
```
Access API documentation after server starts:
```bash
<domain:port>/docs # Swagger format
<domain:port>/redoc # ReDoc format
# example
http://127.0.0.1:8000/docs
```
## API Endpoints
| Method | Endpoint | Description |
|--------|------------------|----------------------------|
| GET | `/items/` | List all items |
| GET | `/items/{id}` | Get single item |
| POST | `/items/` | Create new item |
| PUT | `/items/{id}` | Update existing item |
| DELETE | `/items/{id}` | Delete item |
## Key Features
### 1. Custom Response System
- **Standardized Response Format** (`schemas/base.py`):
```python
class ResponseSchema(BaseModel, Generic[T]):
timestamp: str # ISO 8601 format
status: int # HTTP status code
code: str # Custom error code
path: str # Request endpoint
message: T # Response payload/error
```
- **Exception Handling** (`helper/exceptions.py`):
- `InternalException` class with error code mapping
- Automatic error response formatting
- Custom error codes with HTTP status mapping
#### Response Examples
##### Success Response
```json
{
"timestamp": "2024-02-15T09:30:00.000Z",
"status": 200,
"code": "HTTP-200",
"path": "/items/1",
"message": {
"id": 1,
"name": "Test Item 1",
"price": 20.0,
"category": "test"
}
}
```
##### Error Response
```json
{
"timestamp": "2024-02-15T09:30:00.000Z",
"status": 404,
"code": "DATA-002",
"path": "/items/999",
"message": "ERROR: Item not found"
}
```
### 2. Async Operations
- Asynchronous CRUD operations with aiofiles
- Non-blocking I/O for JSON data management
- Async test client with pytest-asyncio
### 3. Enhanced Documentation
- Custom OpenAPI tags and descriptions (`utils/documents.py`)
- Automatic API documentation generation
- Response schema examples in Swagger/ReDoc
## Running Tests
```bash
# Run all tests
$ pytest tests/
# Run specific test file
$ pytest tests/test_items.py -v
```
For FastAPI documentation: <https://fastapi.tiangolo.com/>
## Project Origin
This project was created using the [FastAPI-fastkit](https://github.com/bnbong/FastAPI-fastkit) template.
FastAPI-fastkit is an open-source project that helps developers quickly set up FastAPI-based applications with proper structure and tooling.
### Template Information
- Template author: [bnbong](mailto:bbbong9@gmail.com)
- Project maintainer: [bnbong](mailto:bbbong9@gmail.com)