-
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.
Merge pull request #10 from CrazyProger1/experiments
V0.0.6
- Loading branch information
Showing
22 changed files
with
662 additions
and
249 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -99,8 +99,9 @@ from resty.clients.httpx import RESTClient | |
async def main(): | ||
client = RESTClient(httpx.AsyncClient(base_url="https://localhost:8000")) | ||
|
||
response = await UserManager.create( | ||
client=client, | ||
manager = UserManager(client=client) | ||
|
||
response = await manager.create( | ||
obj=UserCreateSchema( | ||
username="admin", | ||
email="[email protected]", | ||
|
@@ -111,44 +112,44 @@ async def main(): | |
) | ||
print(response) # id=1 username='admin' email='[email protected]' age=19 | ||
|
||
response = await UserManager.read( | ||
client=client, | ||
response = await manager.read( | ||
response_type=UserReadSchema, | ||
) | ||
|
||
for obj in response: | ||
print(obj) # id=1 username='admin' email='[email protected]' age=19 | ||
|
||
response = await UserManager.read_one( | ||
client=client, | ||
response = await manager.read_one( | ||
obj_or_pk=1, | ||
response_type=UserReadSchema, | ||
) | ||
|
||
print(response) # id=1 username='admin' email='[email protected]' age=19 | ||
|
||
response = await UserManager.update( | ||
client=client, | ||
obj=UserUpdateSchema(id=1, username="admin123", ), | ||
response = await manager.update( | ||
obj=UserUpdateSchema( | ||
id=1, | ||
username="admin123", | ||
), | ||
response_type=UserReadSchema, | ||
) | ||
|
||
print(response) # id=1 username='admin123' email='[email protected]' age=19 | ||
|
||
await UserManager.delete( | ||
client=client, | ||
await manager.delete( | ||
obj_or_pk=1, | ||
expected_status=204, | ||
) | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) | ||
``` | ||
|
||
## Status | ||
|
||
``0.0.5`` - **RELEASED** | ||
``0.0.6`` - **RELEASED** | ||
|
||
## Licence | ||
|
||
|
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
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 |
---|---|---|
|
@@ -43,8 +43,9 @@ class UserManager(Manager): | |
async def main(): | ||
client = RESTClient(httpx.AsyncClient(base_url="https://localhost:8000")) | ||
|
||
response = await UserManager.create( | ||
client=client, | ||
manager = UserManager(client=client) | ||
|
||
response = await manager.create( | ||
obj=UserCreateSchema( | ||
username="admin", | ||
email="[email protected]", | ||
|
@@ -55,32 +56,31 @@ async def main(): | |
) | ||
print(response) # id=1 username='admin' email='[email protected]' age=19 | ||
|
||
response = await UserManager.read( | ||
client=client, | ||
response = await manager.read( | ||
response_type=UserReadSchema, | ||
) | ||
|
||
for obj in response: | ||
print(obj) # id=1 username='admin' email='[email protected]' age=19 | ||
|
||
response = await UserManager.read_one( | ||
client=client, | ||
response = await manager.read_one( | ||
obj_or_pk=1, | ||
response_type=UserReadSchema, | ||
) | ||
|
||
print(response) # id=1 username='admin' email='[email protected]' age=19 | ||
|
||
response = await UserManager.update( | ||
client=client, | ||
obj=UserUpdateSchema(id=1, username="admin123", ), | ||
response = await manager.update( | ||
obj=UserUpdateSchema( | ||
id=1, | ||
username="admin123", | ||
), | ||
response_type=UserReadSchema, | ||
) | ||
|
||
print(response) # id=1 username='admin123' email='[email protected]' age=19 | ||
|
||
await UserManager.delete( | ||
client=client, | ||
await manager.delete( | ||
obj_or_pk=1, | ||
expected_status=204, | ||
) | ||
|
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()) |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "resty-client" | ||
version = "0.0.5" | ||
version = "0.0.6" | ||
description = "RestyClient is a simple, easy-to-use Python library for interacting with REST APIs using Pydantic's powerful data validation and deserialization tools." | ||
authors = ["CrazyProger1 <[email protected]>"] | ||
license = "MIT" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__title__ = "Resty-Client" | ||
__version__ = "0.0.5" | ||
__version__ = "0.0.6" | ||
__description__ = """RestyClient is a simple, easy-to-use Python library for interacting with REST APIs using Pydantic's | ||
powerful data validation and deserialization tools.""" |
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
Empty file.
Empty file.
Empty file.
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,6 @@ | ||
from .middlewares import LimitOffsetPaginationMiddleware, PagePaginationMiddleware | ||
|
||
__all__ = [ | ||
"LimitOffsetPaginationMiddleware", | ||
"PagePaginationMiddleware", | ||
] |
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 @@ | ||
DEFAULT_LIMIT = 100 |
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,58 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Container | ||
|
||
from resty.enums import Endpoint | ||
from resty.types import Request, Response | ||
from resty.middlewares import BaseRequestMiddleware, BaseResponseMiddleware | ||
|
||
from .constants import DEFAULT_LIMIT | ||
|
||
|
||
class PaginationMiddleware(BaseRequestMiddleware, BaseResponseMiddleware, ABC): | ||
def __init__(self, endpoints: Container[Endpoint] = None): | ||
self._endpoints = endpoints or { | ||
Endpoint.READ, | ||
} | ||
|
||
@abstractmethod | ||
async def paginate(self, request: Request, **kwargs): # pragma: nocover | ||
... | ||
|
||
async def unpaginate(self, response: Response, **kwargs): | ||
response.json = response.json.get("results", response.json) | ||
|
||
async def _handle_request(self, request: Request, **kwargs): | ||
if request.endpoint in self._endpoints: | ||
await self.paginate(request=request, **kwargs) | ||
|
||
async def _handle_response(self, response: Response, **kwargs): | ||
if response.request.endpoint in self._endpoints: | ||
await self.unpaginate(response=response, **kwargs) | ||
|
||
async def __call__(self, reqresp: Request | Response, **kwargs): | ||
if isinstance(reqresp, Request): | ||
return await self._handle_request(request=reqresp, **kwargs) | ||
return await self._handle_response(response=reqresp, **kwargs) | ||
|
||
|
||
class LimitOffsetPaginationMiddleware(PaginationMiddleware): | ||
def __init__(self, limit: int = DEFAULT_LIMIT, **kwargs): | ||
self._limit = limit | ||
super().__init__(**kwargs) | ||
|
||
async def paginate(self, request: Request, **kwargs): | ||
request.params.update( | ||
{ | ||
"limit": kwargs.pop("limit", self._limit), | ||
"offset": kwargs.pop("offset", 0), | ||
} | ||
) | ||
|
||
|
||
class PagePaginationMiddleware(PaginationMiddleware): | ||
async def paginate(self, request: Request, **kwargs): | ||
request.params.update( | ||
{ | ||
"page": kwargs.pop("page", 1), | ||
} | ||
) |
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
Oops, something went wrong.