@@ -41,42 +41,39 @@ A common question people have as they become more comfortable with FastAPI is ho
41
41
- Example:
42
42
43
43
``` python
44
- from fastapi import FastAPI, APIRouter, Query
44
+ from fastapi import FastAPI, Query
45
45
from pydantic import BaseModel
46
46
from fastapi_class import View
47
47
48
48
app = FastAPI()
49
- router = APIRouter()
50
49
51
50
class ItemModel (BaseModel ):
52
51
id : int
53
52
name: str
54
53
description: str = None
55
54
56
- @View (router )
55
+ @View (app )
57
56
class ItemView :
58
- def post (self , item : ItemModel):
57
+ async def post (self , item : ItemModel):
59
58
return item
60
59
61
- def get (self , item_id : int = Query(... , gt = 0 )):
60
+ async def get (self , item_id : int = Query(... , gt = 0 )):
62
61
return {" item_id" : item_id}
63
62
64
- app.include_router(router)
65
63
```
66
64
67
65
### Response model 📦
68
66
69
67
` Exception ` in list need to be either function that return ` fastapi.HTTPException ` itself. In case of a function it is required to have all of it's arguments to be ` optional ` .
70
68
71
69
``` py
72
- from fastapi import FastAPI, APIRouter, HTTPException, status
70
+ from fastapi import FastAPI, HTTPException, status
73
71
from fastapi.responses import PlainTextResponse
74
72
from pydantic import BaseModel
75
73
76
74
from fastapi_class import View
77
75
78
76
app = FastAPI()
79
- router = APIRouter()
80
77
81
78
NOT_AUTHORIZED = HTTPException(401 , " Not authorized." )
82
79
NOT_ALLOWED = HTTPException(405 , " Method not allowed." )
@@ -85,7 +82,7 @@ NOT_FOUND = lambda item_id="item_id": HTTPException(404, f"Item with {item_id} n
85
82
class ItemResponse (BaseModel ):
86
83
field: str | None = None
87
84
88
- @View (router )
85
+ @View (app )
89
86
class MyView :
90
87
exceptions = {
91
88
" __all__" : [NOT_AUTHORIZED ],
@@ -100,29 +97,26 @@ class MyView:
100
97
" delete" : PlainTextResponse
101
98
}
102
99
103
- def get (self ):
100
+ async def get (self ):
104
101
...
105
102
106
- def put (self ):
103
+ async def put (self ):
107
104
...
108
105
109
- def delete (self ):
106
+ async def delete (self ):
110
107
...
111
-
112
- app.include_router(router)
113
108
```
114
109
115
110
### Customized Endpoints
116
111
117
112
``` py
118
- from fastapi import FastAPI, APIRouter, HTTPException
113
+ from fastapi import FastAPI, HTTPException
119
114
from fastapi.responses import PlainTextResponse
120
115
from pydantic import BaseModel
121
116
122
117
from fastapi_class import View, endpoint
123
118
124
119
app = FastAPI()
125
- router = APIRouter()
126
120
127
121
NOT_AUTHORIZED = HTTPException(401 , " Not authorized." )
128
122
NOT_ALLOWED = HTTPException(405 , " Method not allowed." )
@@ -132,7 +126,7 @@ EXCEPTION = HTTPException(400, "Example.")
132
126
class UserResponse (BaseModel ):
133
127
field: str | None = None
134
128
135
- @View (router )
129
+ @View (app )
136
130
class MyView :
137
131
exceptions = {
138
132
" __all__" : [NOT_AUTHORIZED ],
@@ -149,17 +143,17 @@ class MyView:
149
143
" delete" : PlainTextResponse
150
144
}
151
145
152
- def get (self ):
146
+ async def get (self ):
153
147
...
154
148
155
- def put (self ):
149
+ async def put (self ):
156
150
...
157
151
158
- def delete (self ):
152
+ async def delete (self ):
159
153
...
160
154
161
- @endpoint ((" PUT" , ), path = " edit" )
162
- def edit (self ):
155
+ @endpoint ((" PUT" ), path = " edit" )
156
+ async def edit (self ):
163
157
...
164
158
```
165
159
@@ -182,9 +176,17 @@ source venv/bin/activate
182
176
183
177
And then install the development dependencies:
184
178
179
+ __ Note:__ You should have ` uv ` installed, if not you can install it with:
180
+
181
+ ``` bash
182
+ pip install uv
183
+ ```
184
+
185
+ Then you can install the dependencies with:
186
+
185
187
``` bash
186
188
# Install dependencies
187
- pip install -e .[test,lint]
189
+ uv pip install -r requirements/all.txt
188
190
```
189
191
190
192
### Run tests 🌝
0 commit comments