From 3eddba101bb42751df674c057b50544d5ade10fd Mon Sep 17 00:00:00 2001 From: Excavator Bot <33266368+svc-excavator-bot@users.noreply.github.com> Date: Wed, 4 Dec 2024 08:33:14 -0800 Subject: [PATCH] Deprecate hostname in UserTokenAuth (#82) --- README.md | 7 ++---- foundry/_core/foundry_token_auth_client.py | 16 +++++++++++- foundry/_versions.py | 2 +- foundry/v1/cli.py | 5 +--- foundry/v2/cli.py | 5 +--- tests/auth/test_foundry_auth_token_client.py | 26 ++++++-------------- tests/test_api_client.py | 6 ++--- tests/utils.py | 4 +-- 8 files changed, 33 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index af6a9f34..3048dcca 100644 --- a/README.md +++ b/README.md @@ -84,10 +84,7 @@ initializing the `UserTokenAuth`: import foundry foundry_client = foundry.v2.FoundryClient( - auth=foundry.UserTokenAuth( - hostname="example.palantirfoundry.com", - token=os.environ["BEARER_TOKEN"], - ), + auth=foundry.UserTokenAuth(token=os.environ["BEARER_TOKEN"]), hostname="example.palantirfoundry.com", ) ``` @@ -110,7 +107,7 @@ auth = foundry.ConfidentialClientAuth( client_id=os.environ["CLIENT_ID"], client_secret=os.environ["CLIENT_SECRET"], hostname="example.palantirfoundry.com", - scopes=["api:read-data"], + scopes=[...], # optional list of scopes ) auth.sign_in_as_service_user() diff --git a/foundry/_core/foundry_token_auth_client.py b/foundry/_core/foundry_token_auth_client.py index 918ea17e..c147ad19 100644 --- a/foundry/_core/foundry_token_auth_client.py +++ b/foundry/_core/foundry_token_auth_client.py @@ -14,7 +14,9 @@ import os +import warnings from typing import Callable +from typing import Optional from typing import Tuple from typing import TypeVar @@ -39,7 +41,19 @@ def access_token(self) -> str: class UserTokenAuth(Auth): - def __init__(self, hostname: str, token: str) -> None: + def __init__(self, hostname: Optional[str] = None, token: str = "") -> None: + if hostname is not None: + warnings.warn( + "The 'hostname' parameter is deprecated and will be removed in the next major version.", + DeprecationWarning, + stacklevel=2, + ) + + if token == "": + raise TypeError( + "UserTokenAuth.__init__() missing 1 required keyword-only argument: 'token'" + ) + self._hostname = hostname self._token = _UserToken(token) diff --git a/foundry/_versions.py b/foundry/_versions.py index f26bf91e..566c6bea 100644 --- a/foundry/_versions.py +++ b/foundry/_versions.py @@ -17,4 +17,4 @@ # using the autorelease bot __version__ = "0.0.0" -__openapi_document_version__ = "1.1008.0" +__openapi_document_version__ = "1.1009.0" diff --git a/foundry/v1/cli.py b/foundry/v1/cli.py index ee7aa51b..cdce0ea3 100644 --- a/foundry/v1/cli.py +++ b/foundry/v1/cli.py @@ -46,10 +46,7 @@ def get_from_environ(key: str) -> str: def cli(ctx: _Context): "An experimental CLI for the Foundry API" ctx.obj = foundry.v1.FoundryClient( - auth=foundry.UserTokenAuth( - hostname=get_from_environ("FOUNDRY_HOSTNAME"), - token=get_from_environ("FOUNDRY_TOKEN"), - ), + auth=foundry.UserTokenAuth(token=get_from_environ("FOUNDRY_TOKEN")), hostname=get_from_environ("FOUNDRY_HOSTNAME"), ) diff --git a/foundry/v2/cli.py b/foundry/v2/cli.py index 2068be78..3851662f 100644 --- a/foundry/v2/cli.py +++ b/foundry/v2/cli.py @@ -46,10 +46,7 @@ def get_from_environ(key: str) -> str: def cli(ctx: _Context): "An experimental CLI for the Foundry API" ctx.obj = foundry.v2.FoundryClient( - auth=foundry.UserTokenAuth( - hostname=get_from_environ("FOUNDRY_HOSTNAME"), - token=get_from_environ("FOUNDRY_TOKEN"), - ), + auth=foundry.UserTokenAuth(token=get_from_environ("FOUNDRY_TOKEN")), hostname=get_from_environ("FOUNDRY_HOSTNAME"), ) diff --git a/tests/auth/test_foundry_auth_token_client.py b/tests/auth/test_foundry_auth_token_client.py index d616bfa1..ba7ed37d 100644 --- a/tests/auth/test_foundry_auth_token_client.py +++ b/tests/auth/test_foundry_auth_token_client.py @@ -14,6 +14,7 @@ import os +import warnings import pytest @@ -56,27 +57,16 @@ def test_load_from_env_missing_host(temp_os_environ): @pytest.mark.skip def test_can_pass_config(): - os.environ["PALANTIR_HOSTNAME"] = "host_test" os.environ["PALANTIR_TOKEN"] = "token_test" - config = UserTokenAuth(hostname="host_test2", token="token_test2") - assert config.hostname == "host_test2" # type: ignore + config = UserTokenAuth(token="token_test2") assert config._token == "token_test2" -def test_can_pass_config_missing_token(): - assert pytest.raises(TypeError, lambda: UserTokenAuth(hostname="test")) # type: ignore - +def test_missing_token_raises_type_error(): + assert pytest.raises(TypeError, lambda: UserTokenAuth()) # type: ignore -def test_can_pass_config_missing_host(): - assert pytest.raises(TypeError, lambda: UserTokenAuth(token="test")) # type: ignore - -@pytest.mark.skip -def test_checks_host_type(): - assert pytest.raises(ValueError, lambda: UserTokenAuth(hostname=1)) # type: ignore - - -@pytest.mark.skip -def test_checks_token_type(): - assert pytest.raises(ValueError, lambda: UserTokenAuth(token=1)) # type: ignore - assert pytest.raises(ValueError, lambda: UserTokenAuth(token=1)) # type: ignore +def test_warns_if_given_hostname(): + with warnings.catch_warnings(record=True) as w: + UserTokenAuth(hostname="foo", token="bar") + assert len(w) == 1 diff --git a/tests/test_api_client.py b/tests/test_api_client.py index a4726c08..a1a65b71 100644 --- a/tests/test_api_client.py +++ b/tests/test_api_client.py @@ -47,7 +47,7 @@ def __init__(self, *args: Any, **kwargs: Any): def test_user_agent(): """Test that the user agent is set correctly.""" - client = ApiClient(auth=UserTokenAuth(hostname="foo", token="bar"), hostname="foo") + client = ApiClient(auth=UserTokenAuth(token="bar"), hostname="foo") client.session.request = Mock(return_value=AttrDict(status_code=200, headers={})) client.call_api( @@ -79,7 +79,7 @@ def test_user_agent(): def test_path_encoding(): """Test that the user agent is set correctly.""" - client = ApiClient(auth=UserTokenAuth(hostname="foo", token="bar"), hostname="foo") + client = ApiClient(auth=UserTokenAuth(token="bar"), hostname="foo") client.session.request = Mock(return_value=AttrDict(status_code=200, headers={})) client.call_api( @@ -112,7 +112,7 @@ def call_api_helper( data: str, headers: Dict[str, str], ): - client = ApiClient(auth=UserTokenAuth(hostname="foo", token="bar"), hostname="foo") + client = ApiClient(auth=UserTokenAuth(token="bar"), hostname="foo") client.session.request = Mock( return_value=AttrDict( diff --git a/tests/utils.py b/tests/utils.py index 100bedf4..f4d634da 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -52,7 +52,7 @@ class MockRequest(TypedDict): @pytest.fixture def client_v1(): yield FoundryV1Client( - auth=foundry.UserTokenAuth(hostname="example.palantirfoundry.com", token=""), + auth=foundry.UserTokenAuth(token=""), hostname="example.palantirfoundry.com", ) @@ -60,7 +60,7 @@ def client_v1(): @pytest.fixture def client_v2(): yield FoundryV2Client( - auth=foundry.UserTokenAuth(hostname="example.palantirfoundry.com", token=""), + auth=foundry.UserTokenAuth(token=""), hostname="example.palantirfoundry.com", )