Skip to content

Commit c0fa5b4

Browse files
Explicit coverage over creation of parameterized URLs (#256)
* Explicit coverage over creation of parameterized URLs * black . * Update request.py * Update request.py
1 parent 092a1aa commit c0fa5b4

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

tests/utils/test_requests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,10 @@ def test_request_parses_json_when_encoding_in_content_type(
149149
)
150150

151151
assert RequestHelper().request("ok_place") == {"foo": "bar"}
152+
153+
def test_build_url(self):
154+
assert RequestHelper().build_parameterized_url("a/b/c") == "a/b/c"
155+
assert RequestHelper().build_parameterized_url("a/{b}/c", b="b") == "a/b/c"
156+
assert (
157+
RequestHelper().build_parameterized_url("a/{b}/c", b="test") == "a/test/c"
158+
)

workos/mfa.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ def get_factor(
106106
raise ValueError("Incomplete arguments. Need to specify a factor ID")
107107

108108
response = self.request_helper.request(
109-
"auth/factors/{authentication_factor_id}".format(
110-
authentication_factor_id=authentication_factor_id
109+
self.request_helper.build_parameterized_url(
110+
"auth/factors/{authentication_factor_id}",
111+
authentication_factor_id=authentication_factor_id,
111112
),
112113
method=REQUEST_METHOD_GET,
113114
token=workos.api_key,
@@ -137,8 +138,9 @@ def delete_factor(
137138
raise ValueError("Incomplete arguments. Need to specify a factor ID.")
138139

139140
return self.request_helper.request(
140-
"auth/factors/{authentication_factor_id}".format(
141-
authentication_factor_id=authentication_factor_id
141+
self.request_helper.build_parameterized_url(
142+
"auth/factors/{authentication_factor_id}",
143+
authentication_factor_id=authentication_factor_id,
142144
),
143145
method=REQUEST_METHOD_DELETE,
144146
token=workos.api_key,
@@ -169,8 +171,8 @@ def challenge_factor(
169171
)
170172

171173
response = self.request_helper.request(
172-
"auth/factors/{factor_id}/challenge".format(
173-
factor_id=authentication_factor_id
174+
self.request_helper.build_parameterized_url(
175+
"auth/factors/{factor_id}/challenge", factor_id=authentication_factor_id
174176
),
175177
method=REQUEST_METHOD_POST,
176178
params=params,
@@ -228,8 +230,9 @@ def verify_challenge(
228230
)
229231

230232
response = self.request_helper.request(
231-
"auth/challenges/{challenge_id}/verify".format(
232-
challenge_id=authentication_challenge_id
233+
self.request_helper.build_parameterized_url(
234+
"auth/challenges/{challenge_id}/verify",
235+
challenge_id=authentication_challenge_id,
233236
),
234237
method=REQUEST_METHOD_POST,
235238
params=params,

workos/utils/request.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import platform
22

33
import requests
4+
import urllib
45

56
import workos
67
from workos.exceptions import (
@@ -45,6 +46,10 @@ def set_base_api_url(self, base_api_url):
4546
def generate_api_url(self, path):
4647
return self.base_api_url.format(path)
4748

49+
def build_parameterized_url(self, url, **params):
50+
escaped_params = {k: urllib.parse.quote(str(v)) for k, v in params.items()}
51+
return url.format(**escaped_params)
52+
4853
def request(
4954
self,
5055
path,

0 commit comments

Comments
 (0)