Skip to content

Commit

Permalink
Merge pull request #12 from ponytailer/add-group
Browse files Browse the repository at this point in the history
Add group
  • Loading branch information
ponytailer authored Aug 15, 2023
2 parents dab069d + ac8572f commit 9bf8122
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 5 deletions.
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ Http client base pydantic, with requests or aiohttp
### How to use

```python



from pydantic import BaseModel

from pydantic_client import delete, get, post, put
Expand Down Expand Up @@ -61,3 +58,32 @@ class R(RequestsClient):
my_client = R("http://localhost/v1")
get_book: Book = my_client.get_book(1)
```

The Group
```python

from pydantic_client import Group
from pydantic_client.clients.requests import RequestsClient

group = Group("/book")
person_group = Group("/person")


class GroupClient(RequestsClient):
def __init__(self):
super().__init__("http://localhost")

@group.get("/{book_id}")
def get(self, book_id: int) -> Book: # type: ignore
...

@person_group.get("/{person_id}")
def get_person(self, person_id: int) -> Person: # type: ignore
...

client = GroupClient()
book = client.get(1)
person = client.get_person(1)


```
5 changes: 4 additions & 1 deletion pydantic_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from .decorators import delete, get, post, put, rest
from .decorators import delete, get, patch, post, put, rest
from .group import Group

__all__ = [
"patch",
"rest",
"get",
"post",
"put",
"delete",
"Group",
]
1 change: 1 addition & 0 deletions pydantic_client/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ def wrapper(func: Callable) -> MethodInfo:
post = partial(rest, method="POST")
delete = partial(rest, method="DELETE")
put = partial(rest, method="PUT")
patch = partial(rest, method="PATCH")
46 changes: 46 additions & 0 deletions pydantic_client/group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from typing import Optional

from .decorators import delete, get, post, put


class Group:
"""
Endpoint group.
group = Group("/book", name="Book")
person_group = Group("/person", name="Person")
class R(RequestsClient):
@group.get("/{book_id}")
def get_book(self, book_id: int) -> Book:
...
@group.post("/")
def new_book(book: Book) -> Book:
...
@person_group("/")
def new_person(person: Person) -> Person:
...
"""

def __init__(self, url_prefix: str, *, name: Optional[str] = None):
self.url_prefix = url_prefix.rstrip("/")
self.name = name

def get(self, url: str):
return get(self.url_prefix + url)

def put(self, url: str, form_body: bool = False):
return put(self.url_prefix + url, form_body=form_body)

def post(self, url: str, form_body: bool = False):
return post(self.url_prefix + url, form_body=form_body)

def patch(self, url: str, form_body: bool = False):
return patch(self.url_prefix + url, form_body=form_body)

def delete(self, url: str):
return delete(self.url_prefix + url)
10 changes: 9 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from pydantic_client import delete, get, post, put
from pydantic_client import delete, get, patch, post, put
from pydantic_client.clients.aiohttp import AIOHttpClient
from pydantic_client.clients.httpx import HttpxClient
from pydantic_client.clients.requests import RequestsClient
Expand Down Expand Up @@ -33,6 +33,10 @@ def change_book(self, book_id: int, book: Book) -> Book:
def delete_book(self, book_id: int) -> Book:
...

@patch("/books/{book_id}")
def patch_book(self, book_id: int, book: Book) -> Book:
...


class AsyncR(AIOHttpClient):
def __init__(self):
Expand Down Expand Up @@ -60,6 +64,10 @@ async def change_book(self, book_id: int, book: Book) -> Book:
async def delete_book(self, book_id: int) -> Book:
...

@patch("/books/{book_id}")
async def patch_book(self, book_id: int, book: Book) -> Book:
...


class HttpxR(HttpxClient, AsyncR):
def __init__(self):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ async def test_delete(clients, mock_book):
book = await book
assert book.name == "name"
assert book.age == 1


@pytest.mark.asyncio
async def test_patch(clients, mock_book):
for cl in clients:
book = cl.patch_book(1, Book(name="name2", age=3))
if inspect.isawaitable(book):
book = await book
assert book.name == "name"
assert book.age == 1
26 changes: 26 additions & 0 deletions tests/test_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from pydantic_client import Group
from pydantic_client.clients.requests import RequestsClient
from tests.book import Book
from tests.helpers import mock_requests

group = Group("/book")


class GroupClient(RequestsClient):
def __init__(self):
super().__init__("http://localhost")

@group.get("/{book_id}")
def get(self, book_id: int) -> Book: # type: ignore
...


def test_group_get(monkeypatch):
mock_resp = {"name": "name", "age": 1}
mock_requests(monkeypatch, response=mock_resp)

client = GroupClient()

book = client.get(1)
assert book.name == "name"
assert book.age == 1

0 comments on commit 9bf8122

Please sign in to comment.