-
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
944bac4
commit a3752a3
Showing
9 changed files
with
479 additions
and
15 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 |
---|---|---|
|
@@ -39,6 +39,112 @@ poetry add resty-client | |
|
||
## Getting-Started | ||
|
||
See [examples](examples) for more. | ||
|
||
### Schemas | ||
|
||
```python | ||
from resty.types import Schema | ||
|
||
|
||
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 | ||
``` | ||
|
||
### Manager | ||
|
||
```python | ||
from resty.managers import Manager | ||
from resty.enums import Endpoint, Field | ||
|
||
|
||
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", | ||
} | ||
``` | ||
|
||
### CRUD | ||
|
||
```python | ||
import asyncio | ||
|
||
import httpx | ||
|
||
from resty.clients.httpx import RESTClient | ||
|
||
|
||
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()) | ||
``` | ||
|
||
## Status | ||
|
||
|
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 |
---|---|---|
|
@@ -22,4 +22,4 @@ | |
|
||
- Improved test coverage to 100% | ||
- Improved architecture | ||
- Expanded Readme docs & examples | ||
- Added examples |
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,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()) |
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
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
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.