Skip to content

Commit 8bd0832

Browse files
authored
Merge pull request #60 from opsmill/fac-fix-query-params
fix: urlencode query params
2 parents ceb4d73 + 4880e57 commit 8bd0832

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

infrahub_sdk/branch.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from typing import TYPE_CHECKING, Any, Optional, Union
4+
from urllib.parse import urlencode
45

56
from pydantic import BaseModel
67

@@ -55,14 +56,16 @@ def generate_diff_data_url(
5556
time_to: Optional[str] = None,
5657
) -> str:
5758
"""Generate the URL for the diff_data function."""
58-
url = f"{client.address}/api/diff/data?branch={branch_name}"
59-
url += f"&branch_only={str(branch_only).lower()}"
59+
url = f"{client.address}/api/diff/data"
60+
url_params = {}
61+
url_params["branch"] = branch_name
62+
url_params["branch_only"] = str(branch_only).lower()
6063
if time_from:
61-
url += f"&time_from={time_from}"
64+
url_params["time_from"] = time_from
6265
if time_to:
63-
url += f"&time_to={time_to}"
66+
url_params["time_to"] = time_to
6467

65-
return url
68+
return url + urlencode(url_params)
6669

6770

6871
class InfrahubBranchManager(InfraHubBranchManagerBase):

infrahub_sdk/client.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Union,
1919
overload,
2020
)
21+
from urllib.parse import urlencode
2122

2223
import httpx
2324
import ujson
@@ -200,7 +201,7 @@ def _graphql_url(
200201
if at:
201202
at = Timestamp(at)
202203
url_params["at"] = at.to_string()
203-
url += "?" + "&".join([f"{key}={value}" for key, value in url_params.items()])
204+
url += "?" + urlencode(url_params)
204205

205206
return url
206207

@@ -951,14 +952,19 @@ async def query_gql_query(
951952

952953
if url_params:
953954
url_params_str = []
955+
url_params_dict = {}
954956
for key, value in url_params.items():
955957
if isinstance(value, (list)):
956958
for item in value:
957-
url_params_str.append(f"{key}={item}")
959+
url_params_str.append((key, item))
958960
else:
959-
url_params_str.append(f"{key}={value}")
961+
url_params_dict[key] = value
960962

961-
url += "?" + "&".join(url_params_str)
963+
url += "?"
964+
if url_params_dict:
965+
url += urlencode(url_params_dict) + "&"
966+
if url_params_str:
967+
url += urlencode(url_params_str)
962968

963969
payload = {}
964970
if variables:
@@ -1928,14 +1934,19 @@ def query_gql_query(
19281934

19291935
if url_params:
19301936
url_params_str = []
1937+
url_params_dict = {}
19311938
for key, value in url_params.items():
19321939
if isinstance(value, (list)):
19331940
for item in value:
1934-
url_params_str.append(f"{key}={item}")
1941+
url_params_str.append((key, item))
19351942
else:
1936-
url_params_str.append(f"{key}={value}")
1943+
url_params_dict[key] = value
19371944

1938-
url += "?" + "&".join(url_params_str)
1945+
url += "?"
1946+
if url_params_dict:
1947+
url += urlencode(url_params_dict) + "&"
1948+
if url_params_str:
1949+
url += urlencode(url_params_str)
19391950

19401951
payload = {}
19411952
if variables:

0 commit comments

Comments
 (0)