From ff36f34195b6b2fa638708ff636ca28bf3f11323 Mon Sep 17 00:00:00 2001 From: huangsong Date: Thu, 26 Sep 2024 16:47:00 +0800 Subject: [PATCH] add examples --- README.md | 66 +++-------------------------------- example/__init__.py | 0 example/config.tmol | 13 +++++++ example/protocol.py | 40 +++++++++++++++++++++ example/use_config_to_init.py | 14 ++++++++ example/use_factory.py | 12 +++++++ example/use_toml_to_init.py | 11 ++++++ 7 files changed, 95 insertions(+), 61 deletions(-) create mode 100644 example/__init__.py create mode 100644 example/config.tmol create mode 100644 example/protocol.py create mode 100644 example/use_config_to_init.py create mode 100644 example/use_factory.py create mode 100644 example/use_toml_to_init.py diff --git a/README.md b/README.md index 015e4ae..af42daf 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,8 @@ Only support the json response. from pydantic import BaseModel from pydantic_client import delete, get, post, put, PydanticClient -from pydantic_client.clients import RequestsClient, AIOHttpClient, HttpxClient -from pydantic_client import ClientConfig, PydanticClientFactory +from pydantic_client.clients import RequestsClient +from pydantic_client import ClientConfig class Book(BaseModel): @@ -54,23 +54,6 @@ class WebClient: def change_book(self, book_id: int) -> Book: ... -""" -toml config example: - -[tools.pydantic-client.config] -base_url = "http://localhost:5000" (have to set) -headers.authorization = "Bearer xxxxxx" (optional) -http2 = true (optional) -timeout = 10 (optional) -""" - - -client = PydanticClient.from_toml("your_toml_path.toml") \ - .bind_client(RequestsClient) \ - .bind_protocol(WebClient) \ - .build() - -# or client: WebClient = PydanticClient( ClientConfig( @@ -83,51 +66,12 @@ client: WebClient = PydanticClient( .build() -get_book: Book = client.get_book(1) +book: Book = client.get_book(1) -# use the factory - -""" -toml file example: -[[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 -[[tools.pydantic_client.factory]] -name = "address_client -base_url = "https://example.com/api/v3" -timeout = 1 -""" - -class BookProtocol: - @get("/books/{book_id}?query={query}") - def get_book(self, book_id: int, query: str) -> Book: - ... - -class AuthorProtocol: - @get("/books/{book_id}?query={query}") - def get_book(self, book_id: int, query: str) -> Book: - ... - -class AddressProtocol: - @get("/books/{book_id}?query={query}") - def get_book(self, book_id: int, query: str) -> Book: - ... - -factory = PydanticClientFactory.from_toml("pydantic_client.toml") \ - .register_client("book_client", RequestsClient, BookProtocol) \ - .register_client("author_client", HttpxClient, AuthorProtocol) \ - .register_client("address_client", AIOHttpClient, AddressProtocol) \ - .build() - -book: Book = factory.get_client(BookProtocol).get_book(1, "name") -author: Book = factory.get_client(AuthorProtocol).get_book(1, "name") ``` +And see the examples to get more examples. + # change log ### v1.0.0: refactor all the code, to be simple. remove the group client. diff --git a/example/__init__.py b/example/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/example/config.tmol b/example/config.tmol new file mode 100644 index 0000000..e333856 --- /dev/null +++ b/example/config.tmol @@ -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 \ No newline at end of file diff --git a/example/protocol.py b/example/protocol.py new file mode 100644 index 0000000..07ce6ba --- /dev/null +++ b/example/protocol.py @@ -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: + ... diff --git a/example/use_config_to_init.py b/example/use_config_to_init.py new file mode 100644 index 0000000..a656765 --- /dev/null +++ b/example/use_config_to_init.py @@ -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") diff --git a/example/use_factory.py b/example/use_factory.py new file mode 100644 index 0000000..d70406b --- /dev/null +++ b/example/use_factory.py @@ -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() diff --git a/example/use_toml_to_init.py b/example/use_toml_to_init.py new file mode 100644 index 0000000..acca40b --- /dev/null +++ b/example/use_toml_to_init.py @@ -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")