Skip to content

Commit 53baa79

Browse files
Append /v1 to api_url if not provided.
1 parent 6e86283 commit 53baa79

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

authsignal/client.py

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import decimal
2+
import authsignal
23
from authsignal.version import VERSION
34

45
import humps
@@ -9,8 +10,7 @@
910

1011
_UNICODE_STRING = str
1112

12-
API_BASE_URL = 'https://signal.authsignal.com'
13-
API_CHALLENGE_URL = 'https://api.authsignal.com/v1'
13+
API_BASE_URL = 'https://api.authsignal.com/v1'
1414

1515
BLOCK = "BLOCK"
1616
ALLOW = "ALLOW"
@@ -52,8 +52,8 @@ def __init__(
5252
self.url = api_url
5353
self.timeout = timeout
5454
self.version = version
55+
self.api_version = 'v1'
5556

56-
5757
def track(self, user_id, action, payload=None, path=None):
5858
"""Tracks an action to authsignal, scoped to the user_id and action
5959
Returns the status of the action so that you can determine to whether to continue
@@ -236,7 +236,7 @@ def enroll_verified_authenticator(self, user_id, authenticator_payload, path=No
236236
raise ApiException(str(e), path) from e
237237

238238
def validate_challenge(self, token: str, user_id: Optional[str] = None, action: Optional[str] = None) -> Dict[str, Any]:
239-
path = f"{API_CHALLENGE_URL}/validate"
239+
path = self._validate_challenge_url()
240240
headers = {
241241
'Content-Type': 'application/json',
242242
'Accept': 'application/json'
@@ -267,20 +267,34 @@ def _default_headers(self):
267267
return {'Content-type': 'application/json',
268268
'Accept': '*/*',
269269
'User-Agent': self._user_agent()}
270+
270271
def _user_agent(self):
271272
return f'Authsignal Python v{self.version}'
272273

273274
def _track_url(self, user_id, action):
274-
return f'{self.url}/v1/users/{user_id}/actions/{action}'
275-
275+
path = self._ensure_versioned_path(f'/users/{user_id}/actions/{action}')
276+
return f'{self.url}{path}'
277+
276278
def _get_action_url(self, user_id, action, idempotency_key):
277-
return f'{self.url}/v1/users/{user_id}/actions/{action}/{idempotency_key}'
278-
279+
path = self._ensure_versioned_path(f'/users/{user_id}/actions/{action}/{idempotency_key}')
280+
return f'{self.url}{path}'
281+
279282
def _get_user_url(self, user_id):
280-
return f'{self.url}/v1/users/{user_id}'
283+
path = self._ensure_versioned_path(f'/users/{user_id}')
284+
return f'{self.url}{path}'
281285

282286
def _post_enrollment_url(self, user_id):
283-
return f'{self.url}/v1/users/{user_id}/authenticators'
287+
path = self._ensure_versioned_path(f'/users/{user_id}/authenticators')
288+
return f'{self.url}{path}'
289+
290+
def _validate_challenge_url(self):
291+
path = self._ensure_versioned_path(f'/validate')
292+
return f'{self.url}{path}'
293+
294+
def _ensure_versioned_path(self, path):
295+
if not self.url.endswith(f'/{self.api_version}'):
296+
return f'/{self.api_version}{path}'
297+
return path
284298

285299
class ApiException(Exception):
286300
def __init__(self, message, url, http_status_code=None, body=None, api_status=None,
@@ -311,4 +325,3 @@ def _assert_non_empty_dict(val, name):
311325
raise TypeError('{0} must be a non-empty dict'.format(name))
312326
elif not val:
313327
raise ValueError('{0} must be a non-empty dict'.format(name))
314-

0 commit comments

Comments
 (0)