Skip to content

Commit

Permalink
0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyProger1 committed Apr 23, 2024
1 parent 2183743 commit 5621113
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async def main():

## Status

``0.0.2`` - **RELEASED**
``0.0.3`` - **RELEASED**

## Licence

Expand Down
11 changes: 10 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@

## v0.0.2

- Serializer many schemas support
- Added serializer many schemas support
- Added get_schema serializer method

## v0.0.3

- Refactored manager & httpx client
- Added more examples
- Added serializer tests
- Added URL injecting mechanism (allows to perform many-layer requests: `api/v1/users/123/product/321`).
See [example](../examples/crud_many_layers)
- Added deserialize_many serializer method
4 changes: 2 additions & 2 deletions examples/crud/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from resty.ext.django.middlewares import DjangoPagePaginationMiddleware

from managers import ProductManager
from schemas import Product
from schemas import ProductSchema


async def main():
Expand All @@ -15,7 +15,7 @@ async def main():

rest_client.add_middleware(DjangoPagePaginationMiddleware())

product = Product(
product = ProductSchema(
name='My Product',
description='My Desc',
code='123W31QQW'
Expand Down
2 changes: 1 addition & 1 deletion examples/crud/schemas.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pydantic import BaseModel


class Product(BaseModel):
class ProductSchema(BaseModel):
id: int | None = None
name: str
description: str
Expand Down
4 changes: 2 additions & 2 deletions examples/crud/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from resty.serializers import Serializer

from schemas import Product
from schemas import ProductSchema


class ProductSerializer(Serializer):
schema = Product
schema = ProductSchema
21 changes: 21 additions & 0 deletions examples/crud_many_layers/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from resty.enums import (
Endpoint,
Field
)
from resty.managers import Manager

from serializers import ProductSerializer


class UserProductManager(Manager):
serializer = ProductSerializer
endpoints = {
Endpoint.CREATE: 'users/{user_pk}/products/',
Endpoint.READ: 'users/{user_pk}/products/',
Endpoint.READ_ONE: 'users/{user_pk}/products/{pk}/',
Endpoint.UPDATE: 'users/{user_pk}/products/{pk}/',
Endpoint.DELETE: 'users/{user_pk}/products/{pk}/',
}
fields = {
Field.PRIMARY: 'id',
}
9 changes: 9 additions & 0 deletions examples/crud_many_layers/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pydantic import BaseModel


class ProductSchema(BaseModel):
id: int | None = None
name: str
description: str
code: str

7 changes: 7 additions & 0 deletions examples/crud_many_layers/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from resty.serializers import Serializer

from schemas import ProductSchema


class ProductSerializer(Serializer):
schema = ProductSchema
21 changes: 21 additions & 0 deletions examples/serializer_schemas/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from resty.enums import (
Endpoint,
Field
)
from resty.managers import Manager

from serializers import ProductSerializer


class ProductManager(Manager):
serializer = ProductSerializer
endpoints = {
Endpoint.CREATE: '/products/',
Endpoint.READ: '/products/',
Endpoint.READ_ONE: '/products/{pk}/',
Endpoint.UPDATE: '/products/{pk}/',
Endpoint.DELETE: '/products/{pk}/',
}
fields = {
Field.PRIMARY: 'id',
}
18 changes: 18 additions & 0 deletions examples/serializer_schemas/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pydantic import BaseModel


class ProductCreateSchema(BaseModel):
name: str
description: str


class ProductReadSchema(BaseModel):
id: int | None = None
name: str
description: str
code: str


class ProductUpdateSchema(BaseModel):
name: str = None
description: str = None
17 changes: 17 additions & 0 deletions examples/serializer_schemas/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from resty.serializers import Serializer
from resty.enums import Endpoint

from schemas import (
ProductCreateSchema,
ProductReadSchema,
ProductUpdateSchema
)


class ProductSerializer(Serializer):
schemas = {
Endpoint.CREATE: ProductCreateSchema,
Endpoint.READ: ProductReadSchema,
Endpoint.READ_ONE: ProductReadSchema,
Endpoint.UPDATE: ProductUpdateSchema
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "resty-client"
version = "0.0.2"
version = "0.0.3"
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"
Expand Down
6 changes: 5 additions & 1 deletion resty/serializers/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pydantic import BaseModel

from resty.enums import Endpoint
from resty.types import BaseSerializer


Expand All @@ -18,7 +19,10 @@ def get_schema(cls, **context) -> type[BaseModel]:
schema = cls.schema

if cls.schemas is not None:
schema = cls.schemas.get(endpoint, cls.schema)
schema = cls.schemas.get(
endpoint,
cls.schema or cls.schemas.get(Endpoint.BASE)
)

if schema is None:
raise TypeError(f"Schema should be specified for {endpoint}")
Expand Down
Empty file.
44 changes: 44 additions & 0 deletions tests/resty/serializers/test_serializer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from pydantic import BaseModel

from resty.enums import Endpoint
from resty.serializers import Serializer


Expand All @@ -10,6 +11,14 @@ class Schema1(BaseModel):
private: bool


class CreateSchema(BaseModel):
pass


class ReadSchema(BaseModel):
pass


@pytest.mark.parametrize(
"model, data", [(Schema1, {"id": 1, "name": "test", "private": True})]
)
Expand All @@ -33,3 +42,38 @@ class Ser(Serializer):

assert obj.model_dump() == data


@pytest.mark.parametrize(
"model, data", [(Schema1, [{"id": 1, "name": "test", "private": True}])]
)
def test_serializer_deserialize_many(model, data):
class Ser(Serializer):
schema = model

objects = Ser.deserialize_many(data)

assert len(objects) == len(data)


@pytest.mark.parametrize(
"models, context, expected_schema", [
({Endpoint.CREATE: CreateSchema}, {"endpoint": Endpoint.CREATE}, CreateSchema),
({Endpoint.READ: ReadSchema}, {"endpoint": Endpoint.READ}, ReadSchema),
({Endpoint.READ_ONE: Schema1}, {"endpoint": Endpoint.READ_ONE}, Schema1),
({Endpoint.BASE: Schema1}, {"endpoint": Endpoint.READ_ONE}, Schema1),
({Endpoint.BASE: Schema1}, {"schema": ReadSchema}, ReadSchema),
]
)
def test_get_schema(models, context, expected_schema):
class Ser(Serializer):
schemas = models

assert Ser.get_schema(**context) == expected_schema


def test_get_unspecified_schema():
class Ser(Serializer):
pass

with pytest.raises(TypeError):
Ser.get_schema()

0 comments on commit 5621113

Please sign in to comment.