-
Notifications
You must be signed in to change notification settings - Fork 3
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 #12 from ponytailer/add-group
Add group
- Loading branch information
Showing
7 changed files
with
125 additions
and
5 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
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,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", | ||
] |
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 |
---|---|---|
@@ -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) |
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
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 |