-
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.
- Loading branch information
1 parent
3b2b4fb
commit ff36f34
Showing
7 changed files
with
95 additions
and
61 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
Empty file.
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,13 @@ | ||
[tools.pydantic-client.config] | ||
base_url = "http://localhost:5000" | ||
headers.authorization = "Bearer xxxxxx" | ||
timeout = 10 | ||
|
||
[[tools.pydantic_client.factory]] | ||
name = "book_client | ||
base_url = "https://example.com/api/v1" | ||
timeout = 1 | ||
[[tools.pydantic_client.factory]] | ||
name = "author_client | ||
base_url = "https://example.com/api/v2" | ||
timeout = 1 |
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,40 @@ | ||
from pydantic import BaseModel | ||
|
||
from pydantic_client import get, post, put, delete | ||
|
||
|
||
class Book(BaseModel): | ||
name: str | ||
year: int | ||
|
||
|
||
class Author(BaseModel): | ||
name: str | ||
|
||
|
||
class BookProtocol: | ||
|
||
@get("/books/{book_id}?query={query}") | ||
def get_book(self, book_id: int, query: str) -> Book: | ||
... | ||
|
||
@post("/books", form_body=True) | ||
def create_book_form(self, book: Book) -> Book: | ||
""" will post the form with book""" | ||
... | ||
|
||
@put("/books/{book_id}") | ||
def change_book(self, book_id: int, book: Book) -> Book: | ||
"""will put the json body""" | ||
... | ||
|
||
@delete("/books/{book_id}") | ||
def change_book(self, book_id: int) -> Book: | ||
... | ||
|
||
|
||
class AuthorProtocol: | ||
|
||
@get("/authors") | ||
def get_author(self) -> Author: | ||
... |
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,14 @@ | ||
from example.protocol import BookProtocol, Book | ||
from pydantic_client import PydanticClient, ClientConfig | ||
from pydantic_client.clients import RequestsClient | ||
|
||
if __name__ == "__main__": | ||
cfg = ClientConfig( | ||
base_url="https://example.com/api" | ||
) | ||
client = PydanticClient(cfg) \ | ||
.bind_client(RequestsClient) \ | ||
.bind_protocol(BookProtocol) \ | ||
.build() | ||
|
||
book: Book = client.get_book(1, "name") |
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,12 @@ | ||
from example.protocol import AuthorProtocol, BookProtocol, Book, Author | ||
from pydantic_client import PydanticClientFactory | ||
from pydantic_client.clients import RequestsClient, HttpxClient | ||
|
||
if __name__ == '__main__': | ||
factory = PydanticClientFactory.from_toml("pydantic_client.toml") \ | ||
.register_client("book_client", RequestsClient, BookProtocol) \ | ||
.register_client("author_client", HttpxClient, AuthorProtocol) \ | ||
.build() | ||
|
||
book: Book = factory.get_client(BookProtocol).get_book(1, "name") | ||
author: Author = factory.get_client(AuthorProtocol).get_author() |
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,11 @@ | ||
from example.protocol import BookProtocol, Book | ||
from pydantic_client import PydanticClient | ||
from pydantic_client.clients import RequestsClient | ||
|
||
if __name__ == "__main__": | ||
client = PydanticClient.from_toml("config.toml") \ | ||
.bind_client(RequestsClient) \ | ||
.bind_protocol(BookProtocol) \ | ||
.build() | ||
|
||
book: Book = client.get_book(1, "name") |