Skip to content

Commit

Permalink
NEW: QueryArrayParam marker for HTTP GET parameters ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Oct 17, 2024
1 parent b502d85 commit cd48cd9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
28 changes: 23 additions & 5 deletions combadge/support/http/markers/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,32 @@ class QueryParam(ParameterMarker[ContainsQueryParams]):

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

request.query_params.append((self.name, value.value if isinstance(value, Enum) else value))


@dataclass(**SLOTS)
class QueryArrayParam(ParameterMarker[ContainsQueryParams]):
"""
Mark parameter as an array-like query parameter.
Supports any iterable value as a call argument.
Examples:
>>> def call(query: Annotated[list[str], QueryParam("query")]) -> ...:
>>> ...
>>> def call(query: Annotated[Iterable[str], QueryParam("query")]) -> ...:
>>> ...
"""

name: str

@override
def __call__(self, request: ContainsQueryParams, 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))


if not TYPE_CHECKING:

@dataclass(**SLOTS)
Expand Down
15 changes: 12 additions & 3 deletions tests/integration/test_httpbin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@
from combadge.core.errors import BackendError
from combadge.core.interfaces import SupportsService
from combadge.core.markers import Mixin
from combadge.support.http.markers import CustomHeader, FormData, FormField, Header, QueryParam, http_method, path
from combadge.support.http.markers import (
CustomHeader,
FormData,
FormField,
Header,
QueryArrayParam,
QueryParam,
http_method,
path,
)
from combadge.support.httpx.backends.async_ import HttpxBackend as AsyncHttpxBackend
from combadge.support.httpx.backends.sync import HttpxBackend as SyncHttpxBackend

Expand Down Expand Up @@ -51,11 +60,11 @@ def get_anything(
self,
foo: Annotated[int, QueryParam("foobar")],
bar: Annotated[int, QueryParam("foobar")],
multivalue: Annotated[list[str], QueryParam("multivalue")],
multivalue: Annotated[list[str], QueryArrayParam("multivalue")],
) -> Response: ...

service = SupportsHttpbin.bind(SyncHttpxBackend(Client(base_url="https://httpbin.org")))
response = service.get_anything(foo=100500, bar=100501, multivalue=["value1","value2"])
response = service.get_anything(foo=100500, bar=100501, multivalue=["value1", "value2"])

assert response == Response(args={"foobar": ["100500", "100501"], "multivalue": ["value1", "value2"]})

Expand Down

0 comments on commit cd48cd9

Please sign in to comment.