-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d39caf6
commit 2755fcb
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import asyncio | ||
|
||
import httpx | ||
|
||
from resty.enums import Endpoint, Field | ||
from resty.types import Schema | ||
from resty.managers import Manager | ||
from resty.clients.httpx import RESTClient | ||
from resty.ext.django.middlewares.pagination import ( | ||
LimitOffsetPaginationMiddleware, | ||
PagePaginationMiddleware, | ||
) | ||
|
||
|
||
class UserCreateSchema(Schema): | ||
username: str | ||
email: str | ||
password: str | ||
age: int | ||
|
||
|
||
class UserReadSchema(Schema): | ||
id: int | ||
username: str | ||
email: str | ||
age: int | ||
|
||
|
||
class UserUpdateSchema(Schema): | ||
username: str = None | ||
email: str = None | ||
|
||
|
||
class UserManager(Manager): | ||
endpoints = { | ||
Endpoint.CREATE: "users/", | ||
Endpoint.READ: "users/", | ||
Endpoint.READ_ONE: "users/{pk}", | ||
Endpoint.UPDATE: "users/{pk}", | ||
Endpoint.DELETE: "users/{pk}", | ||
} | ||
fields = { | ||
Field.PRIMARY: "id", | ||
} | ||
|
||
|
||
async def main(): | ||
client = RESTClient(httpx.AsyncClient(base_url="https://localhost:8000")) | ||
|
||
# Using LimitOffset pagination middleware | ||
with client.middlewares.middleware(LimitOffsetPaginationMiddleware(limit=200)): | ||
manager = UserManager(client=client) | ||
|
||
paginated_response = await manager.read( | ||
response_type=UserReadSchema, | ||
offset=100, | ||
) | ||
|
||
# Using Page pagination middleware | ||
with client.middlewares.middleware(PagePaginationMiddleware()): | ||
manager = UserManager(client=client) | ||
|
||
paginated_response = await manager.read( | ||
response_type=UserReadSchema, | ||
page=3, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |