Skip to content

Commit

Permalink
FIx bug in api provider
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervdw committed Nov 25, 2024
1 parent 8f92df1 commit 65b81a7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

- Test against Python 3.13 (bumped asyncpg to `0.30.*` and aiohttp to `3.10.*`).

- Fixed a bug in (Sync)ApiProvider and Gateway: if a query parameter with value `None`
is presented, this now hides the query parameter from the generated url. Before,
it resulted in `path?foo=None`.


## 0.18.0 (2024-10-21)
----------------------
Expand Down
5 changes: 4 additions & 1 deletion clean_python/api_client/api_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ def join(url: str, path: str, trailing_slash: bool = False) -> str:


def add_query_params(url: str, params: Json | None) -> str:
# explicitly filter out None values
if params is None:
return url
return url + "?" + urlencode(params, doseq=True)
params = {k: v for k, v in params.items() if v is not None}
query_str = urlencode(params, doseq=True)
return url + "?" + query_str if query_str else url


class FileFormPost(ValueObject):
Expand Down
3 changes: 3 additions & 0 deletions tests/api_client/test_api_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ async def test_post_json(api_provider: ApiProvider, response, request_m):
("bar/", {"a": 2}, "http://testserver/foo/bar?a=2"),
("", {"a": [1, 2]}, "http://testserver/foo?a=1&a=2"),
("", {"a": 1, "b": "foo"}, "http://testserver/foo?a=1&b=foo"),
("", {"a": None}, "http://testserver/foo"),
("", {"a": ""}, "http://testserver/foo?a="),
("", {"a": []}, "http://testserver/foo"),
],
)
async def test_url(api_provider: ApiProvider, path, params, expected_url, request_m):
Expand Down
3 changes: 3 additions & 0 deletions tests/api_client/test_sync_api_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def test_post_json(api_provider: SyncApiProvider, response):
("bar/", {"a": 2}, "http://testserver/foo/bar?a=2"),
("", {"a": [1, 2]}, "http://testserver/foo?a=1&a=2"),
("", {"a": 1, "b": "foo"}, "http://testserver/foo?a=1&b=foo"),
("", {"a": None}, "http://testserver/foo"),
("", {"a": ""}, "http://testserver/foo?a="),
("", {"a": []}, "http://testserver/foo"),
],
)
def test_url(api_provider: SyncApiProvider, path, params, expected_url):
Expand Down

0 comments on commit 65b81a7

Please sign in to comment.