Skip to content

Commit 2f062a9

Browse files
committed
Added AioHttpRouter and examples. Fixed falcon example
1 parent 92809bf commit 2f062a9

File tree

21 files changed

+399
-2
lines changed

21 files changed

+399
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ FastOpenAPI follows the [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
77
## [0.5.0] - Unreleased
88

99
### Added
10+
- `AioHttpRouter` for integration with the `AioHttp` framework
1011
- Class-level cache for model schemas
1112

1213
## [0.4.0] - 2025-03-20

examples/aiohttp/app/__init__.py

Whitespace-only changes.

examples/aiohttp/app/api/__init__.py

Whitespace-only changes.

examples/aiohttp/app/api/routes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from fastopenapi.routers import AioHttpRouter
2+
3+
from .v1 import v1_router
4+
5+
api_router = AioHttpRouter()
6+
api_router.include_router(v1_router, prefix="/v1")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from fastopenapi.routers import AioHttpRouter
2+
3+
from .authors import router as authors_router
4+
from .posts import router as posts_router
5+
6+
v1_router = AioHttpRouter()
7+
v1_router.include_router(authors_router)
8+
v1_router.include_router(posts_router)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from aiohttp import web
2+
3+
from fastopenapi.routers import AioHttpRouter
4+
5+
from ...schemas.authors import (
6+
AuthorSchema,
7+
CreateAuthorSchema,
8+
FilterAuthorSchema,
9+
UpdateAuthorSchema,
10+
)
11+
from ...services.authors import AuthorService
12+
13+
author_service = AuthorService()
14+
15+
router = AioHttpRouter()
16+
17+
18+
@router.post("/authors", tags=["Authors"], status_code=201, response_model=AuthorSchema)
19+
async def create_author(body: CreateAuthorSchema) -> AuthorSchema:
20+
return await author_service.create_author(body)
21+
22+
23+
@router.get("/authors/{author_id}", tags=["Authors"], response_model=AuthorSchema)
24+
async def get_author(author_id: int) -> AuthorSchema:
25+
author = await author_service.get_author(author_id)
26+
if not author:
27+
raise web.HTTPNotFound(reason="Author not found")
28+
return author
29+
30+
31+
@router.get("/authors/", tags=["Authors"], response_model=list[AuthorSchema])
32+
async def get_authors(body: FilterAuthorSchema) -> list[AuthorSchema]:
33+
return await author_service.get_authors(body)
34+
35+
36+
@router.delete("/authors/{author_id}", tags=["Authors"], status_code=204)
37+
async def delete_author(author_id: int) -> None:
38+
author = await author_service.delete_author(author_id)
39+
if not author:
40+
raise web.HTTPNotFound(reason="Author not found")
41+
42+
43+
@router.patch("/authors/{author_id}", tags=["Authors"], response_model=AuthorSchema)
44+
async def update_author(author_id: int, body: UpdateAuthorSchema) -> AuthorSchema:
45+
author = await author_service.update_author(author_id, body)
46+
if not author:
47+
raise web.HTTPNotFound(reason="Author not found")
48+
return author
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from aiohttp import web
2+
3+
from fastopenapi.routers import AioHttpRouter
4+
5+
from ...schemas.posts import (
6+
CreatePostSchema,
7+
FilterPostSchema,
8+
PostSchema,
9+
UpdatePostSchema,
10+
)
11+
from ...services.posts import PostService
12+
13+
post_service = PostService()
14+
router = AioHttpRouter()
15+
16+
17+
@router.post("/posts", tags=["Posts"], status_code=201, response_model=PostSchema)
18+
async def create_post(body: CreatePostSchema) -> PostSchema:
19+
return await post_service.create_post(body)
20+
21+
22+
@router.get("/posts/{post_id}", tags=["Posts"], response_model=PostSchema)
23+
async def get_post(post_id: int) -> PostSchema:
24+
post = await post_service.get_post(post_id)
25+
if not post:
26+
raise web.HTTPNotFound(reason="Author not found")
27+
return post
28+
29+
30+
@router.get("/posts/", tags=["Posts"], response_model=list[PostSchema])
31+
async def get_posts(body: FilterPostSchema) -> list[PostSchema]:
32+
return await post_service.get_posts(body)
33+
34+
35+
@router.delete("/posts/{post_id}", tags=["Posts"], status_code=204)
36+
async def delete_post(post_id: int) -> None:
37+
post = await post_service.delete_post(post_id)
38+
if not post:
39+
raise web.HTTPNotFound(reason="Post not found")
40+
41+
42+
@router.patch("/posts/{post_id}", tags=["Posts"], response_model=PostSchema)
43+
async def update_post(post_id: int, body: UpdatePostSchema) -> PostSchema:
44+
post = await post_service.update_post(post_id, body)
45+
if not post:
46+
raise web.HTTPNotFound(reason="Post not found")
47+
return post

examples/aiohttp/app/schemas/__init__.py

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from pydantic import BaseModel, Field
2+
3+
4+
class AuthorSchema(BaseModel):
5+
id: int
6+
name: str
7+
bio: str | None = None
8+
9+
10+
class FilterAuthorSchema(BaseModel):
11+
id: int = Field(default=None)
12+
name: str = Field(default=None)
13+
14+
15+
class CreateAuthorSchema(BaseModel):
16+
name: str = Field(..., max_length=50)
17+
bio: str | None = Field(None, max_length=200)
18+
19+
20+
class UpdateAuthorSchema(BaseModel):
21+
name: str = Field(default=None, max_length=50)
22+
bio: str = Field(default=None, max_length=200)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from pydantic import BaseModel, Field
2+
3+
4+
class PostSchema(BaseModel):
5+
id: int
6+
title: str
7+
content: str
8+
author_id: int
9+
10+
11+
class FilterPostSchema(BaseModel):
12+
id: int = Field(default=None)
13+
title: str = Field(default=None)
14+
15+
16+
class CreatePostSchema(BaseModel):
17+
title: str = Field(..., max_length=100)
18+
content: str
19+
author_id: int
20+
21+
22+
class UpdatePostSchema(BaseModel):
23+
title: str = Field(default=None, max_length=100)
24+
content: str = Field(default=None)

0 commit comments

Comments
 (0)