Skip to content

Commit

Permalink
NEW: allow for callable default func arguments (#22)
Browse files Browse the repository at this point in the history
Co-authored-by: Bart Veraart <bart.veraart@kpn.com>
  • Loading branch information
beerdeaap and Bart Veraart authored Apr 17, 2023
1 parent 38a5785 commit 8dbe2f2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
3 changes: 3 additions & 0 deletions combadge/core/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def build_request(
except KeyError:
pass
else:
# allow for lazy loaded default parameters
if callable(value):
value = value()
marker.prepare_request(request, value)

# Validate and return the request.
Expand Down
11 changes: 7 additions & 4 deletions tests/integration/cassettes/test_httpbin/test_headers_async.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interactions:
- python-httpx/0.23.3
x-bar:
- barval
x-baz:
- bazval
x-foo:
- fooval
method: GET
Expand All @@ -26,8 +28,9 @@ interactions:
content: "{\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-643d0f74-258491ed49f5affd6aa5cd03\",
\n \"X-Bar\": \"barval\", \n \"X-Foo\": \"fooval\"\n }\n}\n"
\"python-httpx/0.23.3\", \n \"X-Amzn-Trace-Id\": \"Root=1-643d15ec-37354434277996811873cc13\",
\n \"X-Bar\": \"barval\", \n \"X-Baz\": \"bazval\", \n \"X-Foo\": \"fooval\"\n
\ }\n}\n"
headers:
Access-Control-Allow-Credentials:
- 'true'
Expand All @@ -36,11 +39,11 @@ interactions:
Connection:
- keep-alive
Content-Length:
- '339'
- '363'
Content-Type:
- application/json
Date:
- Mon, 17 Apr 2023 09:20:56 GMT
- Mon, 17 Apr 2023 09:48:29 GMT
Server:
- gunicorn/19.9.0
http_version: HTTP/1.1
Expand Down
11 changes: 7 additions & 4 deletions tests/integration/cassettes/test_httpbin/test_headers_sync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interactions:
- python-httpx/0.23.3
x-bar:
- barval
x-baz:
- bazval
x-foo:
- fooval
method: GET
Expand All @@ -26,8 +28,9 @@ interactions:
content: "{\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-64396832-4d7d2c31342b06257ae681ec\",
\n \"X-Bar\": \"barval\", \n \"X-Foo\": \"fooval\"\n }\n}\n"
\"python-httpx/0.23.3\", \n \"X-Amzn-Trace-Id\": \"Root=1-643d1b44-37172fe20eb4793848a2d302\",
\n \"X-Bar\": \"barval\", \n \"X-Baz\": \"bazval\", \n \"X-Foo\": \"fooval\"\n
\ }\n}\n"
headers:
Access-Control-Allow-Credentials:
- 'true'
Expand All @@ -36,11 +39,11 @@ interactions:
Connection:
- keep-alive
Content-Length:
- '339'
- '363'
Content-Type:
- application/json
Date:
- Fri, 14 Apr 2023 14:50:28 GMT
- Mon, 17 Apr 2023 10:11:17 GMT
Server:
- gunicorn/19.9.0
http_version: HTTP/1.1
Expand Down
6 changes: 5 additions & 1 deletion tests/integration/test_httpbin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import abstractmethod
from typing import Any, Dict
from typing import Any, Callable, Dict, Union

from httpx import AsyncClient, Client
from pydantic import BaseModel
Expand Down Expand Up @@ -73,13 +73,15 @@ def get_headers(
self,
foo: Annotated[str, Header("x-foo")],
bar: Annotated[str, Header("x-bar")] = "barval",
baz: Annotated[Union[str, Callable[[], str]], Header("x-baz")] = lambda: "bazval",
) -> Response:
...

service = SupportsHttpbin.bind(SyncHttpxBackend(Client(base_url="https://httpbin.org")))
response = service.get_headers(foo="fooval")
assert response.headers["X-Foo"] == "fooval"
assert response.headers["X-Bar"] == "barval"
assert response.headers["X-Baz"] == "bazval"


@mark.vcr
Expand All @@ -95,10 +97,12 @@ async def get_headers(
self,
foo: Annotated[str, Header("x-foo")],
bar: Annotated[str, Header("x-bar")] = "barval",
baz: Annotated[Union[str, Callable[[], str]], Header("x-baz")] = lambda: "bazval",
) -> Response:
...

service = SupportsHttpbin.bind(AsyncHttpxBackend(AsyncClient(base_url="https://httpbin.org")))
response = await service.get_headers(foo="fooval")
assert response.headers["X-Foo"] == "fooval"
assert response.headers["X-Bar"] == "barval"
assert response.headers["X-Baz"] == "bazval"

0 comments on commit 8dbe2f2

Please sign in to comment.