|
3 | 3 | from fastapi.responses import JSONResponse |
4 | 4 | from pydantic import BaseModel, Field |
5 | 5 |
|
6 | | -router = APIRouter(prefix='/sample_controller') |
| 6 | +router = APIRouter(prefix="/sample_controller") |
| 7 | + |
| 8 | +controller = Controller( |
| 9 | + router, |
| 10 | + openapi_tag={ |
| 11 | + "name": "sample_controller", |
| 12 | + }, |
| 13 | +) |
7 | 14 |
|
8 | | -controller = Controller(router, openapi_tag={ |
9 | | - 'name': 'sample_controller', |
10 | | -}) |
11 | 15 |
|
12 | 16 | class SampleObject(BaseModel): |
13 | | - id: str = Field(..., description='sample id') |
| 17 | + id: str = Field(..., description="sample id") |
14 | 18 |
|
15 | 19 | def to_json(self): |
16 | | - return {'id': self.id} |
| 20 | + return {"id": self.id} |
| 21 | + |
17 | 22 |
|
18 | | -class Foo(): |
| 23 | +class Foo: |
19 | 24 | def create_foo(_, item): |
20 | | - print('Created Foo', str(item)) |
| 25 | + print("Created Foo", str(item)) |
| 26 | + |
21 | 27 |
|
22 | 28 | def get_foo(): |
23 | 29 | return Foo() |
24 | 30 |
|
25 | | -# With the "resource" decorator define the controller Class linked to the Controller router arguments |
| 31 | + |
| 32 | +# With the "resource" decorator define the controller Class |
| 33 | +# linked to the Controller router arguments |
26 | 34 | @controller.resource() |
27 | | -class SampleController(): |
| 35 | +class SampleController: |
28 | 36 | def __init__(self, foo: Foo = Depends(get_foo)): |
29 | 37 | self.foo = foo |
30 | 38 |
|
31 | 39 | @controller.route.get( |
32 | | - '/', |
33 | | - tags=['sample_controller'], |
34 | | - summary='return a sample object') |
35 | | - def sample_get_request(self, id: str = Query(..., title="itemId", description="The id of the sample object")): |
36 | | - return JSONResponse(status_code=status.HTTP_200_OK, content=SampleObject(**{'id': id}).to_json()) |
| 40 | + "/", tags=["sample_controller"], summary="return a sample object" |
| 41 | + ) |
| 42 | + def sample_get_request( |
| 43 | + self, |
| 44 | + id: str = Query(..., title="itemId", description="The id of the sample object"), |
| 45 | + ): |
| 46 | + return JSONResponse( |
| 47 | + status_code=status.HTTP_200_OK, content=SampleObject(**{"id": id}).to_json() |
| 48 | + ) |
37 | 49 |
|
38 | 50 | @controller.route.post( |
39 | | - '/', |
40 | | - tags=['sample_controller'], |
41 | | - summary='create another sample object', |
42 | | - status_code=201) |
43 | | - def sample_post_request(self, simple_object: SampleObject = Body({}, title="SampleObject", description="A sample object model")): |
| 51 | + "/", |
| 52 | + tags=["sample_controller"], |
| 53 | + summary="create another sample object", |
| 54 | + status_code=201, |
| 55 | + ) |
| 56 | + def sample_post_request( |
| 57 | + self, |
| 58 | + simple_object: SampleObject = Body( |
| 59 | + {}, title="SampleObject", description="A sample object model" |
| 60 | + ), |
| 61 | + ): |
44 | 62 | self.foo.create_foo(simple_object) |
45 | 63 | return JSONResponse(status_code=status.HTTP_201_CREATED, content={}) |
0 commit comments