Skip to content

Commit

Permalink
Merge pull request #9 from CrazyProger1/experiments
Browse files Browse the repository at this point in the history
Experiments
  • Loading branch information
CrazyProger1 authored Apr 26, 2024
2 parents 3084093 + 2cbb5c9 commit b4df3f4
Show file tree
Hide file tree
Showing 56 changed files with 1,508 additions and 818 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,5 @@ cython_debug/


# Test File
main.py
resty_proto
main.py
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ coverage-report: coverage;
.PHONY: format
format:
poetry run python -m black tests
poetry run python -m black resty
poetry run python -m black --exclude "types.py" resty
poetry run python -m black examples
114 changes: 70 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<a href="https://github.com/CrazyProger1/Resty-Client/releases/latest"><img alt="GitHub release (latest by date)" src="https://img.shields.io/github/v/release/CrazyProger1/Resty-Client"></a>
<a href="https://pypi.org/project/resty-client/"><img alt="PyPI - Downloads" src="https://img.shields.io/pypi/dm/resty-client"></a>
<a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg" alt="Code Style"></a>
<img src="https://img.shields.io/badge/coverage-100%25-brightgreen" alt="Coverage"/>
</p>


Expand Down Expand Up @@ -38,91 +39,116 @@ poetry add resty-client

## Getting-Started

### Schema
See [examples](examples) for more.

### Schemas

```python
from pydantic import BaseModel
from resty.types import Schema


class Product(BaseModel):
id: int | None = None
name: str
description: str
code: str
```
class UserCreateSchema(Schema):
username: str
email: str
password: str
age: int

### Serializer

```python
from resty.serializers import Serializer
class UserReadSchema(Schema):
id: int
username: str
email: str
age: int


class ProductSerializer(Serializer):
schema = Product
class UserUpdateSchema(Schema):
username: str = None
email: str = None
```

### Manager

```python
from resty.enums import (
Endpoint,
Field
)
from resty.managers import Manager
from resty.enums import Endpoint, Field


class ProductManager(Manager):
serializer = ProductSerializer
class UserManager(Manager):
endpoints = {
Endpoint.CREATE: '/products/',
Endpoint.READ: '/products/',
Endpoint.READ_ONE: '/products/{pk}/',
Endpoint.UPDATE: '/products/{pk}/',
Endpoint.DELETE: '/products/{pk}/',
Endpoint.CREATE: "users/",
Endpoint.READ: "users/",
Endpoint.READ_ONE: "users/{pk}",
Endpoint.UPDATE: "users/{pk}",
Endpoint.DELETE: "users/{pk}",
}
fields = {
Field.PRIMARY: 'id',
Field.PRIMARY: "id",
}
```

### CRUD

```python
from httpx import AsyncClient
import asyncio

import httpx

from resty.clients.httpx import RESTClient


async def main():
xclient = AsyncClient(base_url='http://localhost:8000/')
rest_client = RESTClient(xclient=xclient)
client = RESTClient(httpx.AsyncClient(base_url="https://localhost:8000"))

response = await UserManager.create(
client=client,
obj=UserCreateSchema(
username="admin",
email="[email protected]",
password="admin",
age=19,
),
response_type=UserReadSchema,
)
print(response) # id=1 username='admin' email='[email protected]' age=19

product = Product(
name='First prod',
description='My Desc',
code='123W31Q'
response = await UserManager.read(
client=client,
response_type=UserReadSchema,
)

# Create
created = await ProductManager.create(rest_client, product)
for obj in response:
print(obj) # id=1 username='admin' email='[email protected]' age=19

# Read
my_product = await ProductManager.read_one(rest_client, created.id)
response = await UserManager.read_one(
client=client,
obj_or_pk=1,
response_type=UserReadSchema,
)

for prod in await ProductManager.read(rest_client):
print(prod.name)
print(response) # id=1 username='admin' email='[email protected]' age=19

response = await UserManager.update(
client=client,
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,
obj_or_pk=1,
expected_status=204,
)

# Update
my_product.description = 'QWERTY'
await ProductManager.update(rest_client, my_product)

# Delete
await ProductManager.delete(rest_client, my_product.id)
if __name__ == "__main__":
asyncio.run(main())
```

## Status

``0.0.4`` - **RELEASED**
``0.0.5`` - **RELEASED**

## Licence

Expand Down
8 changes: 7 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@

## v0.0.4

- Manager fixes
- Manager important fixes!!!!

## v0.0.5

- Improved test coverage to 100%
- Improved architecture
- Added examples
90 changes: 90 additions & 0 deletions examples/crud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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


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"))

response = await UserManager.create(
client=client,
obj=UserCreateSchema(
username="admin",
email="[email protected]",
password="admin",
age=19,
),
response_type=UserReadSchema,
)
print(response) # id=1 username='admin' email='[email protected]' age=19

response = await UserManager.read(
client=client,
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,
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_type=UserReadSchema,
)

print(response) # id=1 username='admin123' email='[email protected]' age=19

await UserManager.delete(
client=client,
obj_or_pk=1,
expected_status=204,
)


if __name__ == "__main__":
asyncio.run(main())
42 changes: 0 additions & 42 deletions examples/crud/main.py

This file was deleted.

21 changes: 0 additions & 21 deletions examples/crud/managers.py

This file was deleted.

9 changes: 0 additions & 9 deletions examples/crud/schemas.py

This file was deleted.

7 changes: 0 additions & 7 deletions examples/crud/serializers.py

This file was deleted.

21 changes: 0 additions & 21 deletions examples/crud_many_layers/managers.py

This file was deleted.

Loading

0 comments on commit b4df3f4

Please sign in to comment.