Skip to content

Commit e5111b3

Browse files
moverestJguer
authored andcommitted
fix: base URI without tailing /
Because of the way `urllib.parse.urljoin` works, if the base URI didn't have a tailing `/`, the path would be truncated. E.g. `https://onprem.company.com/exposed` with the enpoint `scan` (and version `v1`) would call `https://onprem.company.com/v1/scan` and not `https://onprem.company.com/exposed/v1/scan`.
1 parent bffadfe commit e5111b3

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

pygitguardian/client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ def request(
104104
extra_headers: Dict[str, str] = None,
105105
**kwargs
106106
) -> Response:
107-
if version:
108-
endpoint = urllib.parse.urljoin(version + "/", endpoint)
109-
110-
url = urllib.parse.urljoin(self.base_uri, endpoint)
107+
url = self._url_from_endpoint(endpoint, version)
111108

112109
headers = (
113110
{**self.session.headers, **extra_headers}
@@ -118,6 +115,12 @@ def request(
118115
method=method, url=url, timeout=self.timeout, headers=headers, **kwargs
119116
)
120117

118+
def _url_from_endpoint(self, endpoint: str, version: Optional[str]) -> str:
119+
if version:
120+
endpoint = urllib.parse.urljoin(version + "/", endpoint)
121+
122+
return urllib.parse.urljoin(self.base_uri + "/", endpoint)
123+
121124
def get(
122125
self,
123126
endpoint: str,

tests/test_client.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,42 @@ def test_client_creation(
226226
assert client.session.headers["Authorization"] == "Token {0}".format(api_key)
227227

228228

229+
@pytest.mark.parametrize(
230+
("base_uries", "version", "endpoints_and_urls"),
231+
[
232+
(
233+
("https://api.gitguardian.com",),
234+
"v1",
235+
(
236+
("multiscan", "https://api.gitguardian.com/v1/multiscan"),
237+
("scan", "https://api.gitguardian.com/v1/scan"),
238+
),
239+
),
240+
(
241+
(
242+
"https://gg-onprem-instance.company.com/exposed",
243+
"https://gg-onprem-instance.company.com/exposed/",
244+
),
245+
"v1",
246+
(
247+
(
248+
"multiscan",
249+
"https://gg-onprem-instance.company.com/exposed/v1/multiscan",
250+
),
251+
("scan", "https://gg-onprem-instance.company.com/exposed/v1/scan"),
252+
),
253+
),
254+
],
255+
)
256+
def test_client__url_from_endpoint(base_uries, version, endpoints_and_urls):
257+
for curr_base_uri in base_uries:
258+
client = GGClient(api_key="validapi_keyforsure", base_uri=curr_base_uri)
259+
for endpoint, expected_url in endpoints_and_urls:
260+
assert (
261+
client._url_from_endpoint(endpoint, version) == expected_url
262+
), "Could not get the expected URL for base_uri=`{}`".format(base_uri)
263+
264+
229265
@my_vcr.use_cassette
230266
def test_health_check(client: GGClient):
231267
health = client.health_check()

0 commit comments

Comments
 (0)