Skip to content

Commit 4880e57

Browse files
committed
fix: urlencode query params
This fixes issues when using timestamps containing reserved characters. Signed-off-by: Fatih Acar <[email protected]>
1 parent 656b382 commit 4880e57

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
@@ -228,7 +229,7 @@ def _graphql_url(
228229
if at:
229230
at = Timestamp(at)
230231
url_params["at"] = at.to_string()
231-
url += "?" + "&".join([f"{key}={value}" for key, value in url_params.items()])
232+
url += "?" + urlencode(url_params)
232233

233234
return url
234235

@@ -979,14 +980,19 @@ async def query_gql_query(
979980

980981
if url_params:
981982
url_params_str = []
983+
url_params_dict = {}
982984
for key, value in url_params.items():
983985
if isinstance(value, (list)):
984986
for item in value:
985-
url_params_str.append(f"{key}={item}")
987+
url_params_str.append((key, item))
986988
else:
987-
url_params_str.append(f"{key}={value}")
989+
url_params_dict[key] = value
988990

989-
url += "?" + "&".join(url_params_str)
991+
url += "?"
992+
if url_params_dict:
993+
url += urlencode(url_params_dict) + "&"
994+
if url_params_str:
995+
url += urlencode(url_params_str)
990996

991997
payload = {}
992998
if variables:
@@ -1971,14 +1977,19 @@ def query_gql_query(
19711977

19721978
if url_params:
19731979
url_params_str = []
1980+
url_params_dict = {}
19741981
for key, value in url_params.items():
19751982
if isinstance(value, (list)):
19761983
for item in value:
1977-
url_params_str.append(f"{key}={item}")
1984+
url_params_str.append((key, item))
19781985
else:
1979-
url_params_str.append(f"{key}={value}")
1986+
url_params_dict[key] = value
19801987

1981-
url += "?" + "&".join(url_params_str)
1988+
url += "?"
1989+
if url_params_dict:
1990+
url += urlencode(url_params_dict) + "&"
1991+
if url_params_str:
1992+
url += urlencode(url_params_str)
19821993

19831994
payload = {}
19841995
if variables:

0 commit comments

Comments
 (0)