Skip to content

Commit

Permalink
NEW: add the __content__ pseudo-field, resolves #15
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Mar 31, 2023
1 parent a671020 commit 314b73a
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 7 deletions.
5 changes: 1 addition & 4 deletions .github/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ changelog:
- title: 💥 Breaking changes
labels:
- breaking-change
- title: ✨ New features
labels:
- enhancement
- title: ⚡️Improvements
labels:
- improvement
- enhancement
- documentation
- title: 🎨 Other changes
labels:
Expand Down
3 changes: 2 additions & 1 deletion .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions combadge/support/http/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@
REASON_ALIAS = "__reason__"
Reason: TypeAlias = Annotated[_ReasonT, Field(alias=REASON_ALIAS)]
"""HTTP reason phrase. Alias for the `__reason__` pseudo-field."""

_ContentT = TypeVar("_ContentT", bound=bytes)
CONTENT_ALIAS = "__content__"
Content: TypeAlias = Annotated[_ContentT, Field(alias=CONTENT_ALIAS)]
"""Original response content. Alias for the `__content__` pseudo-field."""
10 changes: 8 additions & 2 deletions combadge/support/httpx/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from combadge.core.interfaces import ProvidesBinder
from combadge.core.typevars import ResponseT
from combadge.support.http.aliases import REASON_ALIAS, STATUS_CODE_ALIAS
from combadge.support.http.aliases import CONTENT_ALIAS, REASON_ALIAS, STATUS_CODE_ALIAS

_ClientT = TypeVar("_ClientT", Client, AsyncClient)

Expand All @@ -18,6 +18,7 @@ class BaseHttpxBackend(ProvidesBinder, Generic[_ClientT]):
# Available response aliases
- [`Content`][combadge.support.http.aliases.Content]: HTTP response content
- [`StatusCode`][combadge.support.http.aliases.StatusCode]: HTTP response status code
- [`Reason`][combadge.support.http.aliases.Reason]: HTTP reason phrase
"""
Expand All @@ -30,11 +31,16 @@ def __init__(self, client: _ClientT, *, raise_for_status: bool = True) -> None:

@classmethod
def _parse_response(cls, from_response: Response, to_type: Type[ResponseT]) -> ResponseT:
try:
json_fields = from_response.json()
except ValueError:
json_fields = {}
return parse_obj_as(
to_type,
{
STATUS_CODE_ALIAS: from_response.status_code,
REASON_ALIAS: from_response.reason_phrase,
**from_response.json(),
CONTENT_ALIAS: from_response.content,
**json_fields,
},
)
Empty file added tests/support/httpx/__init__.py
Empty file.
Empty file.
20 changes: 20 additions & 0 deletions tests/support/httpx/backends/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from http import HTTPStatus

from httpx import Response
from pydantic import BaseModel

from combadge.support.http.aliases import Content, Reason, StatusCode
from combadge.support.httpx.backends.base import BaseHttpxBackend


def test_aliases() -> None:
class Model(BaseModel):
status_code: StatusCode[HTTPStatus]
reason: Reason[str]
content: Content[bytes]

model = BaseHttpxBackend._parse_response(Response(200, text="hello world"), Model)

assert model.status_code == HTTPStatus.OK
assert model.reason == "OK"
assert model.content == b"hello world"

0 comments on commit 314b73a

Please sign in to comment.