Skip to content

Commit

Permalink
BREAK: rename request and response ABCs for better clarity 💥
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Nov 28, 2024
1 parent 8d474d8 commit b15990a
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 69 deletions.
1 change: 0 additions & 1 deletion combadge/core/markers/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from inspect import BoundArguments
from typing import Any, Callable, Generic

# noinspection PyUnresolvedReferences
from typing_extensions import override

from combadge._helpers.dataclasses import SLOTS
Expand Down
4 changes: 2 additions & 2 deletions combadge/support/http/abc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .containers import * # noqa: F403
from .interfaces import * # noqa: F403
from .request import * # noqa: F403
from .response import * # noqa: F403
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@


@dataclass
class ContainsHttpHeaders:
class HttpRequestHeaders:
"""HTTP request headers."""

http_headers: list[tuple[str, Any]] = field(default_factory=list)


@dataclass
class ContainsUrlPath:
"""Request URL path."""
class HttpRequestUrlPath:
"""HTTP request URL path."""

url_path: Optional[str] = None

Expand All @@ -25,7 +25,7 @@ def get_url_path(self) -> str:


@dataclass
class ContainsMethod:
class HttpRequestMethod:
"""HTTP request method."""

method: Optional[str] = None
Expand All @@ -38,14 +38,14 @@ def get_method(self) -> str:


@dataclass
class ContainsQueryParams:
class HttpRequestQueryParams:
"""HTTP request query parameters."""

query_params: list[tuple[str, Any]] = field(default_factory=list)


@dataclass
class ContainsFormData:
class HttpRequestFormData:
"""
HTTP request [form data][1].
Expand All @@ -63,7 +63,7 @@ def append_form_field(self, name: str, value: Any) -> None: # noqa: D102


@dataclass
class ContainsPayload:
class HttpRequestPayload:
"""HTTP request payload."""

payload: Optional[Any] = None
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,34 @@
from typing_extensions import Protocol


class SupportsHeaders(Protocol):
class HttpResponseHeaders(Protocol):
"""Supports read-only case-insensitive mapping of headers."""

@property
def headers(self) -> Mapping[str, str]: # noqa: D102
raise NotImplementedError


class SupportsStatusCode(Protocol):
class HttpResponseStatusCode(Protocol):
"""Supports a read-only status code attribute or property."""

@property
def status_code(self) -> int: # noqa: D102
raise NotImplementedError


class SupportsReasonPhrase(Protocol):
class HttpResponseReasonPhrase(Protocol):
"""Supports a read-only reason phrase attribute or property."""

@property
def reason_phrase(self) -> str: # noqa: D102
raise NotImplementedError


class SupportsText(Protocol):
class HttpResponseText(Protocol):
"""Supports a read-only text attribute or property."""

@property
def text(self) -> str: # noqa: D102
"""Get the response body as text."""
raise NotImplementedError
48 changes: 24 additions & 24 deletions combadge/support/http/markers/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
from combadge.core.markers.parameter import ParameterMarker
from combadge.core.typevars import FunctionT
from combadge.support.http.abc import (
ContainsFormData,
ContainsHttpHeaders,
ContainsMethod,
ContainsPayload,
ContainsQueryParams,
ContainsUrlPath,
HttpRequestFormData,
HttpRequestHeaders,
HttpRequestMethod,
HttpRequestPayload,
HttpRequestQueryParams,
HttpRequestUrlPath,
)

_T = TypeVar("_T")


@dataclass(**SLOTS)
class CustomHeader(ParameterMarker[ContainsHttpHeaders]):
class CustomHeader(ParameterMarker[HttpRequestHeaders]):
"""
Mark a parameter as a header value. Argument is passed «as is» during a service call.
Expand All @@ -39,12 +39,12 @@ class CustomHeader(ParameterMarker[ContainsHttpHeaders]):
name: str

@override
def __call__(self, request: ContainsHttpHeaders, value: Any) -> None: # noqa: D102
def __call__(self, request: HttpRequestHeaders, value: Any) -> None: # noqa: D102
request.http_headers.append((self.name, value))


@dataclass(init=False, **SLOTS)
class Path(Generic[FunctionT], MethodMarker[ContainsUrlPath, FunctionT]): # noqa: D101
class Path(Generic[FunctionT], MethodMarker[HttpRequestUrlPath, FunctionT]): # noqa: D101
_factory: Callable[[BoundArguments], str]

def __init__(self, path_or_factory: str | Callable[[BoundArguments], str]) -> None: # noqa: D107
Expand All @@ -60,7 +60,7 @@ def factory(arguments: BoundArguments) -> str:
self._factory = factory

@override
def prepare_request(self, request: ContainsUrlPath, arguments: BoundArguments) -> None: # noqa: D102
def prepare_request(self, request: HttpRequestUrlPath, arguments: BoundArguments) -> None: # noqa: D102
request.url_path = self._factory(arguments)


Expand All @@ -85,11 +85,11 @@ def path(path_or_factory: str | Callable[..., str]) -> Callable[[FunctionT], Fun


@dataclass(**SLOTS)
class HttpMethod(Generic[FunctionT], MethodMarker[ContainsMethod, FunctionT]): # noqa: D101
class HttpMethod(Generic[FunctionT], MethodMarker[HttpRequestMethod, FunctionT]): # noqa: D101
method: str

@override
def prepare_request(self, request: ContainsMethod, _arguments: BoundArguments) -> None: # noqa: D102
def prepare_request(self, request: HttpRequestMethod, _arguments: BoundArguments) -> None: # noqa: D102
request.method = self.method


Expand All @@ -105,7 +105,7 @@ def http_method(method: str) -> Callable[[FunctionT], FunctionT]:


@dataclass(**SLOTS)
class QueryParam(ParameterMarker[ContainsQueryParams]):
class QueryParam(ParameterMarker[HttpRequestQueryParams]):
"""
Mark parameter as a query parameter.
Expand All @@ -117,12 +117,12 @@ class QueryParam(ParameterMarker[ContainsQueryParams]):
name: str

@override
def __call__(self, request: ContainsQueryParams, value: Any) -> None: # noqa: D102
def __call__(self, request: HttpRequestQueryParams, value: Any) -> None: # noqa: D102
request.query_params.append((self.name, value.value if isinstance(value, Enum) else value))


@dataclass(**SLOTS)
class QueryArrayParam(ParameterMarker[ContainsQueryParams]):
class QueryArrayParam(ParameterMarker[HttpRequestQueryParams]):
"""
Mark parameter as an array-like query parameter.
Expand All @@ -139,13 +139,13 @@ class QueryArrayParam(ParameterMarker[ContainsQueryParams]):
name: str

@override
def __call__(self, request: ContainsQueryParams, value: Any) -> None: # noqa: D102
def __call__(self, request: HttpRequestQueryParams, value: Any) -> None: # noqa: D102
for sub_value in value:
request.query_params.append((self.name, sub_value.value if isinstance(sub_value, Enum) else sub_value))


@dataclass(**SLOTS)
class Payload(ParameterMarker[ContainsPayload]):
class Payload(ParameterMarker[HttpRequestPayload]):
"""
Mark parameter as a request payload.
Expand All @@ -160,7 +160,7 @@ class Payload(ParameterMarker[ContainsPayload]):
by_alias: bool = False

@override
def __call__(self, request: ContainsPayload, value: Any) -> None: # noqa: D102
def __call__(self, request: HttpRequestPayload, value: Any) -> None: # noqa: D102
value = get_type_adapter(cast(Hashable, type(value))).dump_python(
value,
by_alias=self.by_alias,
Expand All @@ -178,7 +178,7 @@ def __class_getitem__(cls, item: type[Any]) -> Any:


@dataclass(**SLOTS)
class Field(ParameterMarker[ContainsPayload]):
class Field(ParameterMarker[HttpRequestPayload]):
"""
Mark a parameter as a value of a separate payload field.
Expand All @@ -193,14 +193,14 @@ class Field(ParameterMarker[ContainsPayload]):
name: str

@override
def __call__(self, request: ContainsPayload, value: Any) -> None: # noqa: D102
def __call__(self, request: HttpRequestPayload, value: Any) -> None: # noqa: D102
if request.payload is None:
request.payload = {}
request.payload[self.name] = value.value if isinstance(value, Enum) else value


@dataclass(**SLOTS)
class FormData(ParameterMarker[ContainsFormData]):
class FormData(ParameterMarker[HttpRequestFormData]):
"""
Mark parameter as a request form data.
Expand All @@ -212,7 +212,7 @@ class FormData(ParameterMarker[ContainsFormData]):
"""

@override
def __call__(self, request: ContainsFormData, value: Any) -> None: # noqa: D102
def __call__(self, request: HttpRequestFormData, value: Any) -> None: # noqa: D102
value = get_type_adapter(cast(Hashable, type(value))).dump_python(value, by_alias=True)
if not isinstance(value, dict):
raise TypeError(f"form data requires a dictionary, got {type(value)}")
Expand All @@ -224,7 +224,7 @@ def __class_getitem__(cls, item: type[Any]) -> Any:


@dataclass(**SLOTS)
class FormField(ParameterMarker[ContainsFormData]):
class FormField(ParameterMarker[HttpRequestFormData]):
"""
Mark a parameter as a separate form field value.
Expand All @@ -241,5 +241,5 @@ class FormField(ParameterMarker[ContainsFormData]):
name: str

@override
def __call__(self, request: ContainsFormData, value: Any) -> None: # noqa: D102
def __call__(self, request: HttpRequestFormData, value: Any) -> None: # noqa: D102
request.append_form_field(self.name, value.value if isinstance(value, Enum) else value)
16 changes: 10 additions & 6 deletions combadge/support/http/markers/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
from http import HTTPStatus
from typing import Any

# noinspection PyUnresolvedReferences
from typing_extensions import override

from combadge._helpers.dataclasses import SLOTS
from combadge.core.markers.response import ResponseMarker
from combadge.support.http.abc import SupportsHeaders, SupportsReasonPhrase, SupportsStatusCode, SupportsText
from combadge.support.http.abc import (
HttpResponseHeaders,
HttpResponseReasonPhrase,
HttpResponseStatusCode,
HttpResponseText,
)


@dataclass(**SLOTS)
Expand All @@ -26,7 +30,7 @@ class StatusCode(ResponseMarker):
"""Key under which the status code should mapped in the payload."""

@override
def __call__(self, response: SupportsStatusCode, payload: Any) -> dict[Any, Any]: # noqa: D102
def __call__(self, response: HttpResponseStatusCode, payload: Any) -> dict[Any, Any]: # noqa: D102
return {self.key: HTTPStatus(response.status_code)}


Expand All @@ -38,7 +42,7 @@ class ReasonPhrase(ResponseMarker):
"""Key under which the reason message should mapped in the payload."""

@override
def __call__(self, response: SupportsReasonPhrase, payload: Any) -> dict[Any, Any]: # noqa: D102
def __call__(self, response: HttpResponseReasonPhrase, payload: Any) -> dict[Any, Any]: # noqa: D102
return {self.key: response.reason_phrase}


Expand All @@ -62,7 +66,7 @@ class Text(ResponseMarker):
"""Key under which the text contents should assigned in the payload."""

@override
def __call__(self, response: SupportsText, payload: Any) -> dict[Any, Any]: # noqa: D102
def __call__(self, response: HttpResponseText, payload: Any) -> dict[Any, Any]: # noqa: D102
return {self.key: response.text}


Expand Down Expand Up @@ -96,7 +100,7 @@ class Header(ResponseMarker):
"""Key under which the header contents should assigned in the payload."""

@override
def __call__(self, response: SupportsHeaders, payload: Any) -> dict[Any, Any]: # noqa: D102
def __call__(self, response: HttpResponseHeaders, payload: Any) -> dict[Any, Any]: # noqa: D102
try:
value = response.headers[self.header]
except KeyError:
Expand Down
24 changes: 12 additions & 12 deletions combadge/support/http/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

from combadge._helpers.dataclasses import SLOTS
from combadge.support.http.abc import (
ContainsFormData,
ContainsHttpHeaders,
ContainsMethod,
ContainsPayload,
ContainsQueryParams,
ContainsUrlPath,
HttpRequestFormData,
HttpRequestHeaders,
HttpRequestMethod,
HttpRequestPayload,
HttpRequestQueryParams,
HttpRequestUrlPath,
)
from combadge.support.shared.request import BaseBackendRequest


@dataclass(**SLOTS)
class Request(
ContainsMethod,
ContainsUrlPath,
ContainsPayload,
ContainsQueryParams,
ContainsFormData,
ContainsHttpHeaders,
BaseBackendRequest,
HttpRequestFormData,
HttpRequestHeaders,
HttpRequestMethod,
HttpRequestPayload,
HttpRequestQueryParams,
HttpRequestUrlPath,
):
"""Backend-agnostic HTTP request."""
4 changes: 2 additions & 2 deletions combadge/support/soap/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


@dataclass(**SLOTS)
class ContainsSoapOperationName:
class SoapOperationName:
"""SOAP operation name."""

operation_name: Optional[str] = None
Expand All @@ -18,7 +18,7 @@ def get_operation_name(self) -> str:


@dataclass
class ContainsSoapHeader:
class SoapHeader:
"""SOAP request header."""

soap_header: Optional[Any] = None
Expand Down
Loading

0 comments on commit b15990a

Please sign in to comment.