diff --git a/README.md b/README.md index 9e1e17212..917b59da5 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,11 @@ from descope import DescopeClient # Initialized after setting the DESCOPE_PROJECT_ID and DESCOPE_AUTH_MANAGEMENT_KEY env vars descope_client = DescopeClient() -# ** Or directly ** +# ** Or directly (w/ optional base URL) ** descope_client = DescopeClient( - project_id="" - auth_management_key=" + project_id="", + auth_management_key=", + base_url="" ) ``` @@ -1245,8 +1246,9 @@ descope_client = DescopeClient( ``` When the `fga_cache_url` is configured, the following FGA methods will automatically use the cache proxy instead of the default Descope API: + - `save_schema` -- `create_relations` +- `create_relations` - `delete_relations` - `check` diff --git a/descope/descope_client.py b/descope/descope_client.py index 0fc2213ed..0877635b0 100644 --- a/descope/descope_client.py +++ b/descope/descope_client.py @@ -34,6 +34,8 @@ def __init__( jwt_validation_leeway: int = 5, auth_management_key: Optional[str] = None, fga_cache_url: Optional[str] = None, + *, + base_url: Optional[str] = None, ): # validate project id project_id = project_id or os.getenv("DESCOPE_PROJECT_ID", "") @@ -50,6 +52,7 @@ def __init__( # Auth Initialization auth_http_client = HTTPClient( project_id=project_id, + base_url=base_url, timeout_seconds=timeout_seconds, secure=not skip_verify, management_key=auth_management_key diff --git a/tests/test_descope_client.py b/tests/test_descope_client.py index 5c7b669ee..d63ab83d4 100644 --- a/tests/test_descope_client.py +++ b/tests/test_descope_client.py @@ -1052,6 +1052,34 @@ def test_auth_management_key_with_refresh_token(self): timeout=DEFAULT_TIMEOUT_SECONDS, ) + def test_base_url_setting(self): + """Test that base_url parameter is correctly set in DescopeClient""" + custom_base_url = "https://api.use1.descope.com" + client = DescopeClient( + project_id=self.dummy_project_id, + base_url=custom_base_url, + public_key=self.public_key_dict, + ) + + # Verify that the base_url is set in the auth HTTP client + self.assertEqual(client._auth.http_client.base_url, custom_base_url) + + # Verify that the base_url is set in the mgmt HTTP client + self.assertEqual(client._mgmt._http.base_url, custom_base_url) + + def test_base_url_none(self): + """Test that base_url=None uses default base URL from environment or project ID""" + # When base_url is None, it should use DESCOPE_BASE_URI env var or computed default + client = DescopeClient( + project_id=self.dummy_project_id, + base_url=None, + public_key=self.public_key_dict, + ) + + expected_base_url = common.DEFAULT_BASE_URL + self.assertEqual(client._auth.http_client.base_url, expected_base_url) + self.assertEqual(client._mgmt._http.base_url, expected_base_url) + if __name__ == "__main__": unittest.main()