-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW: support non-Pydantic arguments in
Payload
and FormData
, reso…
…lves #77 ✨
- Loading branch information
Showing
11 changed files
with
268 additions
and
19 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
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,10 @@ | ||
from functools import lru_cache | ||
from typing import Any | ||
|
||
from pydantic import TypeAdapter | ||
|
||
|
||
@lru_cache(maxsize=None) | ||
def get_type_adapter(type_: Any) -> TypeAdapter[Any]: | ||
"""Get cached type adapter for the given type.""" | ||
return TypeAdapter(type_) |
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,89 @@ | ||
# Models | ||
|
||
Combadge is built on top of [Pydantic](https://docs.pydantic.dev/), hence Pydantic models are natively supported in service protocols. | ||
|
||
However, thanks to the Pydantic's [`TypeAdapter`](https://docs.pydantic.dev/latest/api/type_adapter/), Combadge automatically supports: | ||
|
||
## Built-in Python types | ||
|
||
```python title="builtin.py" hl_lines="12 17" | ||
from typing_extensions import Annotated, Protocol | ||
|
||
from combadge.core.markers import Extract | ||
from combadge.support.httpx.backends.sync import HttpxBackend | ||
from combadge.support.http.markers import Payload, http_method, path | ||
from httpx import Client | ||
|
||
|
||
class Httpbin(Protocol): | ||
@http_method("POST") | ||
@path("/anything") | ||
def post_anything(self, foo: Payload[int]) -> Annotated[int, Extract("data")]: | ||
... | ||
|
||
|
||
backend = HttpxBackend(Client(base_url="https://httpbin.org")) | ||
assert backend[Httpbin].post_anything(42) == 42 | ||
``` | ||
|
||
## Standard [dataclasses](https://docs.python.org/3/library/dataclasses.html) | ||
|
||
```python title="dataclasses.py" hl_lines="10-12 15-17 23 28" | ||
from dataclasses import dataclass | ||
|
||
from typing_extensions import Protocol | ||
|
||
from combadge.support.httpx.backends.sync import HttpxBackend | ||
from combadge.support.http.markers import Payload, http_method, path | ||
from httpx import Client | ||
|
||
|
||
@dataclass | ||
class Request: | ||
foo: int | ||
|
||
|
||
@dataclass | ||
class Response: | ||
data: str | ||
|
||
|
||
class Httpbin(Protocol): | ||
@http_method("POST") | ||
@path("/anything") | ||
def post_anything(self, foo: Payload[Request]) -> Response: | ||
... | ||
|
||
|
||
backend = HttpxBackend(Client(base_url="https://httpbin.org")) | ||
assert backend[Httpbin].post_anything(Request(42)) == Response(data='{"foo": 42}') | ||
``` | ||
|
||
## [Typed dictionaries](https://docs.python.org/3/library/typing.html#typing.TypedDict) | ||
|
||
```python title="typed_dict.py" hl_lines="8-9 12-13 19 24" | ||
from typing_extensions import Protocol, TypedDict | ||
|
||
from combadge.support.httpx.backends.sync import HttpxBackend | ||
from combadge.support.http.markers import Payload, http_method, path | ||
from httpx import Client | ||
|
||
|
||
class Request(TypedDict): | ||
foo: int | ||
|
||
|
||
class Response(TypedDict): | ||
data: str | ||
|
||
|
||
class Httpbin(Protocol): | ||
@http_method("POST") | ||
@path("/anything") | ||
def post_anything(self, foo: Payload[Request]) -> Response: | ||
... | ||
|
||
|
||
backend = HttpxBackend(Client(base_url="https://httpbin.org")) | ||
assert backend[Httpbin].post_anything({"foo": 42}) == {"data": '{"foo": 42}'} | ||
``` |
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
46 changes: 46 additions & 0 deletions
46
...on/cassettes/test_docs/test_documentation_snippet[docs-support-models.md#builtin.py].yaml
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,46 @@ | ||
interactions: | ||
- request: | ||
body: '42' | ||
headers: | ||
accept: | ||
- '*/*' | ||
accept-encoding: | ||
- gzip, deflate | ||
connection: | ||
- keep-alive | ||
content-length: | ||
- '2' | ||
content-type: | ||
- application/json | ||
host: | ||
- httpbin.org | ||
user-agent: | ||
- python-httpx/0.25.1 | ||
method: POST | ||
uri: https://httpbin.org/anything | ||
response: | ||
content: "{\n \"args\": {}, \n \"data\": \"42\", \n \"files\": {}, \n \"form\": | ||
{}, \n \"headers\": {\n \"Accept\": \"*/*\", \n \"Accept-Encoding\": | ||
\"gzip, deflate\", \n \"Content-Length\": \"2\", \n \"Content-Type\": | ||
\"application/json\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": | ||
\"python-httpx/0.25.1\", \n \"X-Amzn-Trace-Id\": \"Root=1-654e3c0b-67b7ec060f0795b446f2cac2\"\n | ||
\ }, \n \"json\": 42, \n \"method\": \"POST\", \n \"origin\": \"86.94.162.190\", | ||
\n \"url\": \"https://httpbin.org/anything\"\n}\n" | ||
headers: | ||
Access-Control-Allow-Credentials: | ||
- 'true' | ||
Access-Control-Allow-Origin: | ||
- '*' | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '462' | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Fri, 10 Nov 2023 14:19:55 GMT | ||
Server: | ||
- gunicorn/19.9.0 | ||
http_version: HTTP/1.1 | ||
status_code: 200 | ||
version: 1 |
46 changes: 46 additions & 0 deletions
46
...assettes/test_docs/test_documentation_snippet[docs-support-models.md#dataclasses.py].yaml
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,46 @@ | ||
interactions: | ||
- request: | ||
body: '{"foo": 42}' | ||
headers: | ||
accept: | ||
- '*/*' | ||
accept-encoding: | ||
- gzip, deflate | ||
connection: | ||
- keep-alive | ||
content-length: | ||
- '11' | ||
content-type: | ||
- application/json | ||
host: | ||
- httpbin.org | ||
user-agent: | ||
- python-httpx/0.25.1 | ||
method: POST | ||
uri: https://httpbin.org/anything | ||
response: | ||
content: "{\n \"args\": {}, \n \"data\": \"{\\\"foo\\\": 42}\", \n \"files\": | ||
{}, \n \"form\": {}, \n \"headers\": {\n \"Accept\": \"*/*\", \n \"Accept-Encoding\": | ||
\"gzip, deflate\", \n \"Content-Length\": \"11\", \n \"Content-Type\": | ||
\"application/json\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": | ||
\"python-httpx/0.25.1\", \n \"X-Amzn-Trace-Id\": \"Root=1-654e3e5b-7a9b143d4b620fdc7f26dca5\"\n | ||
\ }, \n \"json\": {\n \"foo\": 42\n }, \n \"method\": \"POST\", \n \"origin\": | ||
\"86.94.162.190\", \n \"url\": \"https://httpbin.org/anything\"\n}\n" | ||
headers: | ||
Access-Control-Allow-Credentials: | ||
- 'true' | ||
Access-Control-Allow-Origin: | ||
- '*' | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '491' | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Fri, 10 Nov 2023 14:29:47 GMT | ||
Server: | ||
- gunicorn/19.9.0 | ||
http_version: HTTP/1.1 | ||
status_code: 200 | ||
version: 1 |
46 changes: 46 additions & 0 deletions
46
...cassettes/test_docs/test_documentation_snippet[docs-support-models.md#typed_dict.py].yaml
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,46 @@ | ||
interactions: | ||
- request: | ||
body: '{"foo": 42}' | ||
headers: | ||
accept: | ||
- '*/*' | ||
accept-encoding: | ||
- gzip, deflate | ||
connection: | ||
- keep-alive | ||
content-length: | ||
- '11' | ||
content-type: | ||
- application/json | ||
host: | ||
- httpbin.org | ||
user-agent: | ||
- python-httpx/0.25.1 | ||
method: POST | ||
uri: https://httpbin.org/anything | ||
response: | ||
content: "{\n \"args\": {}, \n \"data\": \"{\\\"foo\\\": 42}\", \n \"files\": | ||
{}, \n \"form\": {}, \n \"headers\": {\n \"Accept\": \"*/*\", \n \"Accept-Encoding\": | ||
\"gzip, deflate\", \n \"Content-Length\": \"11\", \n \"Content-Type\": | ||
\"application/json\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": | ||
\"python-httpx/0.25.1\", \n \"X-Amzn-Trace-Id\": \"Root=1-654e3f28-6dc22fa87c99307a215093d4\"\n | ||
\ }, \n \"json\": {\n \"foo\": 42\n }, \n \"method\": \"POST\", \n \"origin\": | ||
\"86.94.162.190\", \n \"url\": \"https://httpbin.org/anything\"\n}\n" | ||
headers: | ||
Access-Control-Allow-Credentials: | ||
- 'true' | ||
Access-Control-Allow-Origin: | ||
- '*' | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '491' | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Fri, 10 Nov 2023 14:33:12 GMT | ||
Server: | ||
- gunicorn/19.9.0 | ||
http_version: HTTP/1.1 | ||
status_code: 200 | ||
version: 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
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