-
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
768e34f
commit a65aa3b
Showing
9 changed files
with
78 additions
and
122 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
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,17 +1,25 @@ | ||
from example.protocol import BookProtocol, Book, AuthorProtocol, Author | ||
from pydantic_client import pydantic_client_factory, ClientConfig | ||
from pydantic_client import pydantic_client_manager, ClientConfig, ClientType | ||
|
||
if __name__ == "__main__": | ||
|
||
def get_book(): | ||
cfg = ClientConfig( | ||
client_type="requests", | ||
base_url="https://example.com/api" | ||
) | ||
pydantic_client_manager.register(cfg)(BookProtocol) | ||
client = pydantic_client_manager.get(BookProtocol) | ||
book: Book = client.get_book(1, "name") | ||
print(book) | ||
|
||
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") | ||
async def get_author(): | ||
cfg_2 = ClientConfig( | ||
client_type=ClientType.httpx, | ||
base_url="https://example.com/api" | ||
) | ||
|
||
pydantic_client_manager.register(cfg_2)(AuthorProtocol) | ||
|
||
author_client = pydantic_client_factory.get_client(AuthorProtocol) | ||
author: Author = author_client.get_author() | ||
author_client = pydantic_client_manager.get(AuthorProtocol) | ||
author: Author = await author_client.get_author() | ||
print(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
This file was deleted.
Oops, something went wrong.
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,56 +1,33 @@ | ||
from typing import Type | ||
|
||
from pydantic_client.clients.abstract_client import AbstractClient | ||
from pydantic_client.container import container | ||
from pydantic_client.schema.client_config import ClientConfig | ||
|
||
|
||
class PydanticClient: | ||
""" | ||
class User(BaseModel): | ||
id: int | ||
name: str | ||
class WebClient: | ||
@get("/users/{user_id}") | ||
def get_user(self, user_id: int) -> User: | ||
... | ||
web_client = PydanticClient.from_toml(...) | ||
.bind_client(RequestsClient) | ||
.bind_protocol(WebClient) | ||
.build() | ||
# or | ||
client: WebClient = PydanticClient( | ||
ClientConfig( | ||
base_url="https://example.com", | ||
headers={"Authorization": "Bearer abcdefg"}, | ||
timeout=10 | ||
) | ||
).bind_client(RequestsClient) | ||
.bind_protocol(WebClient) | ||
.build() | ||
user: User = web_client.get_user(123) | ||
""" | ||
|
||
def __init__(self, config: ClientConfig): | ||
self.config = config | ||
self.client = None | ||
self.protocol = None | ||
|
||
def bind_client(self, client_class: Type[AbstractClient]): | ||
self.client = client_class(self.config) | ||
return self | ||
|
||
def bind_protocol(self, protocol_class, *args, **kwargs): | ||
self.protocol = protocol_class(*args, **kwargs) | ||
return self | ||
|
||
def build(self): | ||
container.bind_protocol(self.protocol, self.client) | ||
return self.protocol | ||
class PydanticClientManager: | ||
def __init__(self): | ||
# proto_class | ||
self.pydantic_clients = {} | ||
# self.config = config | ||
|
||
def register( | ||
self, | ||
client_config: ClientConfig | ||
): | ||
def wrapper(protocol_class): | ||
web_client = client_config.get_client()(client_config) | ||
protocol = protocol_class() | ||
container.bind_protocol(protocol, web_client) | ||
self.pydantic_clients[protocol_class] = protocol | ||
return protocol_class | ||
|
||
return wrapper | ||
|
||
def get(self, protocol_class=None): | ||
if len(self.pydantic_clients) == 1: | ||
return self.pydantic_clients[list(self.pydantic_clients.keys())[0]] | ||
if not protocol_class: | ||
raise ValueError( | ||
"Multiple clients exists, protocol_class is required") | ||
client = self.pydantic_clients.get(protocol_class) | ||
if not client: | ||
raise ValueError(f"client for {protocol_class} not found") | ||
return client |
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,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pydantic-client" | ||
version = "1.0.1" | ||
version = "1.0.2" | ||
description = "Http client base pydantic, with requests or aiohttp" | ||
authors = ["ponytailer <[email protected]>"] | ||
readme = "README.md" | ||
|
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