Skip to content

Commit

Permalink
Deprecate hostname in UserTokenAuth (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
svc-excavator-bot authored Dec 4, 2024
1 parent 406d4d2 commit 3eddba1
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 38 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
```
Expand All @@ -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()
Expand Down
16 changes: 15 additions & 1 deletion foundry/_core/foundry_token_auth_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@


import os
import warnings
from typing import Callable
from typing import Optional
from typing import Tuple
from typing import TypeVar

Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion foundry/_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
# using the autorelease bot
__version__ = "0.0.0"

__openapi_document_version__ = "1.1008.0"
__openapi_document_version__ = "1.1009.0"
5 changes: 1 addition & 4 deletions foundry/v1/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)

Expand Down
5 changes: 1 addition & 4 deletions foundry/v2/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)

Expand Down
26 changes: 8 additions & 18 deletions tests/auth/test_foundry_auth_token_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


import os
import warnings

import pytest

Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ class MockRequest(TypedDict):
@pytest.fixture
def client_v1():
yield FoundryV1Client(
auth=foundry.UserTokenAuth(hostname="example.palantirfoundry.com", token="<TOKEN>"),
auth=foundry.UserTokenAuth(token="<TOKEN>"),
hostname="example.palantirfoundry.com",
)


@pytest.fixture
def client_v2():
yield FoundryV2Client(
auth=foundry.UserTokenAuth(hostname="example.palantirfoundry.com", token="<TOKEN>"),
auth=foundry.UserTokenAuth(token="<TOKEN>"),
hostname="example.palantirfoundry.com",
)

Expand Down

0 comments on commit 3eddba1

Please sign in to comment.