Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPT: Support list query parameters #133

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions combadge/support/http/markers/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ class QueryParam(ParameterMarker[ContainsQueryParams]):

@override
def __call__(self, request: ContainsQueryParams, value: Any) -> None: # noqa: D102
if isinstance(value, list):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd actually prefer to accept any iterable here (say, a tuple). The docs say the most reliable way would be:

try:
    values = iter(value)
except TypeError:
    ...  # old code for scalar values
else:
    for sub_value in values:
        ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i thought about accepting any iterable, however that would also cover strings which we don't want to iter through here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good one, didn't think of strings. They'd also pass isinstance(..., Collection) check

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))


Expand Down
26 changes: 11 additions & 15 deletions tests/integration/cassettes/test_httpbin/test_query_params.yaml
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
interactions:
- request:
body: '{}'
body: ''
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate
connection:
- keep-alive
content-length:
- '2'
content-type:
- application/json
host:
- httpbin.org
user-agent:
- python-httpx/0.23.3
- python-httpx/0.25.2
method: GET
uri: https://httpbin.org/anything?foobar=100500&foobar=100501
uri: https://httpbin.org/anything?foobar=100500&foobar=100501&multivalue=value1&multivalue=value2
response:
content: "{\n \"args\": {\n \"foobar\": [\n \"100500\", \n \"100501\"\n
\ ]\n }, \n \"data\": \"{}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\":
\ ], \n \"multivalue\": [\n \"value1\", \n \"value2\"\n ]\n
\ }, \n \"data\": \"\", \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.23.3\",
\n \"X-Amzn-Trace-Id\": \"Root=1-6408b26f-2f167e16119c85211f5273ad\"\n },
\n \"json\": {}, \n \"method\": \"GET\", \n \"origin\": \"86.94.162.190\",
\n \"url\": \"https://httpbin.org/anything?foobar=100500&foobar=100501\"\n}\n"
\ \"Host\": \"httpbin.org\", \n \"User-Agent\": \"python-httpx/0.25.2\",
\n \"X-Amzn-Trace-Id\": \"Root=1-67110318-43fea4d527e15c1c6985b29d\"\n },
\n \"json\": null, \n \"method\": \"GET\", \n \"origin\": \"199.103.8.49\",
\n \"url\": \"https://httpbin.org/anything?foobar=100500&foobar=100501&multivalue=value1&multivalue=value2\"\n}\n"
headers:
Access-Control-Allow-Credentials:
- 'true'
Expand All @@ -35,11 +31,11 @@ interactions:
Connection:
- keep-alive
Content-Length:
- '546'
- '572'
Content-Type:
- application/json
Date:
- Wed, 08 Mar 2023 16:06:08 GMT
- Thu, 17 Oct 2024 12:29:12 GMT
Server:
- gunicorn/19.9.0
http_version: HTTP/1.1
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/test_httpbin.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ def get_anything(
self,
foo: Annotated[int, QueryParam("foobar")],
bar: Annotated[int, QueryParam("foobar")],
multivalue: Annotated[list[str], QueryParam("multivalue")],
) -> Response: ...

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

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


class _HeadersResponse(BaseModel):
Expand Down
Loading