Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ponytailer committed Sep 26, 2024
1 parent 3b2b4fb commit ff36f34
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 61 deletions.
66 changes: 5 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand All @@ -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.
Empty file added example/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions example/config.tmol
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
40 changes: 40 additions & 0 deletions example/protocol.py
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:
...
14 changes: 14 additions & 0 deletions example/use_config_to_init.py
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")
12 changes: 12 additions & 0 deletions example/use_factory.py
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()
11 changes: 11 additions & 0 deletions example/use_toml_to_init.py
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")

0 comments on commit ff36f34

Please sign in to comment.