Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions src/onepasswordconnectsdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Client:

def __init__(self, url: str, token: str, config: Optional[ClientConfig] = None) -> None:
"""Initialize client

Args:
url (str): The url of the 1Password Connect API
token (str): The 1Password Service Account token
Expand All @@ -42,11 +42,11 @@ def __init__(self, url: str, token: str, config: Optional[ClientConfig] = None)
def create_session(self, url: str, token: str) -> httpx.Client:
headers = self.build_headers(token)
timeout = get_timeout()

if self.config:
client_args = self.config.get_client_args(url, headers, timeout)
return httpx.Client(**client_args)

return httpx.Client(base_url=url, headers=headers, timeout=timeout)

def build_headers(self, token: str) -> Dict[str, str]:
Expand Down Expand Up @@ -398,13 +398,13 @@ def sanitize_for_serialization(self, obj):

def new_client(url: str, token: str, is_async: bool = False, config: Optional[ClientConfig] = None) -> Union[AsyncClient, Client]:
"""Builds a new client for interacting with 1Password Connect

Args:
url (str): The url of the 1Password Connect API
token (str): The 1Password Service Account token
is_async (bool): Initialize async or sync client
config (Optional[ClientConfig]): Optional configuration for httpx client

Returns:
Union[AsyncClient, Client]: The 1Password Connect client
"""
Expand All @@ -413,18 +413,19 @@ def new_client(url: str, token: str, is_async: bool = False, config: Optional[Cl
return Client(url, token, config)


def new_client_from_environment(url: str = None) -> Union[AsyncClient, Client]:
def new_client_from_environment(
url: Optional[str] = None, token: Optional[str] = None
) -> Union[AsyncClient, Client]:
"""Builds a new client for interacting with 1Password Connect
using the OP_TOKEN environment variable
using OP_CONNECT_HOST and OP_CONNECT_TOKEN when url or token are omitted.

Parameters:
url: The url of the 1Password Connect API
token: The 1Password Service Account token
url: The url of the 1Password Connect API; if omitted, read from OP_CONNECT_HOST.
token: The Connect token; if omitted, read from OP_CONNECT_TOKEN.

Returns:
Client: The 1Password Connect client
Union[AsyncClient, Client]: The 1Password Connect client (async if OP_CONNECT_CLIENT_ASYNC is True).
"""
token = os.environ.get(ENV_SERVICE_ACCOUNT_JWT_VARIABLE)
is_async = os.environ.get(ENV_IS_ASYNC_CLIENT) == "True"

if url is None:
Expand All @@ -435,9 +436,12 @@ def new_client_from_environment(url: str = None) -> Union[AsyncClient, Client]:
)

if token is None:
raise EnvironmentTokenNotSetException(
"There is no token available in the "
f"{ENV_SERVICE_ACCOUNT_JWT_VARIABLE} variable"
)
token = os.environ.get(ENV_SERVICE_ACCOUNT_JWT_VARIABLE)
if token is None:
raise EnvironmentTokenNotSetException(
"There is no token available in the "
f"{ENV_SERVICE_ACCOUNT_JWT_VARIABLE} variable"
)

return new_client(url, token, is_async)

Loading