From 84689720f109ec14678a47020071a5b7ff916dac Mon Sep 17 00:00:00 2001 From: huangsong Date: Tue, 8 Oct 2024 16:09:21 +0800 Subject: [PATCH] update example --- example/use_config_to_init.py | 17 ++++++++++------- example/use_factory.py | 12 ------------ example/use_toml_to_init.py | 11 ----------- 3 files changed, 10 insertions(+), 30 deletions(-) delete mode 100644 example/use_factory.py delete mode 100644 example/use_toml_to_init.py diff --git a/example/use_config_to_init.py b/example/use_config_to_init.py index a656765..30e7a6b 100644 --- a/example/use_config_to_init.py +++ b/example/use_config_to_init.py @@ -1,14 +1,17 @@ -from example.protocol import BookProtocol, Book -from pydantic_client import PydanticClient, ClientConfig -from pydantic_client.clients import RequestsClient +from example.protocol import BookProtocol, Book, AuthorProtocol, Author +from pydantic_client import pydantic_client_factory, ClientConfig if __name__ == "__main__": cfg = ClientConfig( + client_type="requests", base_url="https://example.com/api" ) - client = PydanticClient(cfg) \ - .bind_client(RequestsClient) \ - .bind_protocol(BookProtocol) \ - .build() + pydantic_client_factory.register(cfg)(BookProtocol) + pydantic_client_factory.register(cfg)(AuthorProtocol) + + client = pydantic_client_factory.get_client(BookProtocol) book: Book = client.get_book(1, "name") + + author_client = pydantic_client_factory.get_client(AuthorProtocol) + author: Author = author_client.get_author() diff --git a/example/use_factory.py b/example/use_factory.py deleted file mode 100644 index d70406b..0000000 --- a/example/use_factory.py +++ /dev/null @@ -1,12 +0,0 @@ -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 deleted file mode 100644 index acca40b..0000000 --- a/example/use_toml_to_init.py +++ /dev/null @@ -1,11 +0,0 @@ -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")