diff --git a/src/onepasswordconnectsdk/__init__.py b/src/onepasswordconnectsdk/__init__.py index ab7dcd4..ff34f77 100644 --- a/src/onepasswordconnectsdk/__init__.py +++ b/src/onepasswordconnectsdk/__init__.py @@ -6,3 +6,5 @@ from onepasswordconnectsdk.config import load_dict from onepasswordconnectsdk.client import new_client from onepasswordconnectsdk.client import new_client_from_environment +from onepasswordconnectsdk.async_client import new_async_client +from onepasswordconnectsdk.async_client import new_async_client_from_environment diff --git a/src/onepasswordconnectsdk/async_client.py b/src/onepasswordconnectsdk/async_client.py index 7df6ad1..542883b 100644 --- a/src/onepasswordconnectsdk/async_client.py +++ b/src/onepasswordconnectsdk/async_client.py @@ -5,9 +5,14 @@ from typing import Dict, List, Union import os +from onepasswordconnectsdk.client import ENV_SERVICE_ACCOUNT_JWT_VARIABLE +from onepasswordconnectsdk.models.constants import CONNECT_HOST_ENV_VARIABLE + from onepasswordconnectsdk.serializer import Serializer from onepasswordconnectsdk.utils import build_headers, is_valid_uuid, PathBuilder from onepasswordconnectsdk.errors import ( + EnvironmentHostNotSetException, + EnvironmentTokenNotSetException, FailedToRetrieveItemException, FailedToRetrieveVaultException, ) @@ -372,3 +377,43 @@ def deserialize(self, response, response_type): def sanitize_for_serialization(self, obj): return self.serializer.sanitize_for_serialization(obj) + + +def new_async_client(url: str, token: str) -> AsyncClient: + """Builds a new client for interacting with 1Password Connect + Parameters: + url: The url of the 1Password Connect API + token: The 1Password Service Account token + + Returns: + AsyncClient: The 1Password Connect client + """ + return AsyncClient(url, token) + + +def new_async_client_from_environment(url: str = None) -> AsyncClient: + """Builds a new client for interacting with 1Password Connect + using the OP_CONNECT_TOKEN environment variable + + Parameters: + url: The url of the 1Password Connect API + + Returns: + AsyncClient: The 1Password Connect client + """ + token = os.environ.get(ENV_SERVICE_ACCOUNT_JWT_VARIABLE) + + if url is None: + url = os.environ.get(CONNECT_HOST_ENV_VARIABLE) + if url is None: + raise EnvironmentHostNotSetException( + f"{CONNECT_HOST_ENV_VARIABLE} environment variable is not set" + ) + + if token is None: + raise EnvironmentTokenNotSetException( + "There is no token available in the " + f"{ENV_SERVICE_ACCOUNT_JWT_VARIABLE} variable" + ) + + return new_async_client(url, token) diff --git a/src/onepasswordconnectsdk/client.py b/src/onepasswordconnectsdk/client.py index a62bae1..80a533a 100644 --- a/src/onepasswordconnectsdk/client.py +++ b/src/onepasswordconnectsdk/client.py @@ -5,7 +5,6 @@ from typing import Dict, List, Union import os -from onepasswordconnectsdk.async_client import AsyncClient from onepasswordconnectsdk.serializer import Serializer from onepasswordconnectsdk.utils import build_headers, is_valid_uuid, PathBuilder from onepasswordconnectsdk.errors import ( @@ -18,7 +17,6 @@ from onepasswordconnectsdk.models.constants import CONNECT_HOST_ENV_VARIABLE ENV_SERVICE_ACCOUNT_JWT_VARIABLE = "OP_CONNECT_TOKEN" -ENV_IS_ASYNC_CLIENT = "OP_CONNECT_CLIENT_ASYNC" class Client: @@ -381,34 +379,29 @@ def sanitize_for_serialization(self, obj): return self.serializer.sanitize_for_serialization(obj) -def new_client(url: str, token: str, is_async: bool = False) -> Union[AsyncClient, Client]: +def new_client(url: str, token: str) -> Client: """Builds a new client for interacting with 1Password Connect Parameters: url: The url of the 1Password Connect API token: The 1Password Service Account token - is_async: Initialize async or sync client Returns: Client: The 1Password Connect client """ - if is_async: - return AsyncClient(url, token) return Client(url, token) -def new_client_from_environment(url: str = None) -> Union[AsyncClient, Client]: +def new_client_from_environment(url: str = None) -> Client: """Builds a new client for interacting with 1Password Connect - using the OP_TOKEN environment variable + using the OP_CONNECT_TOKEN environment variable Parameters: url: The url of the 1Password Connect API - token: The 1Password Service Account token Returns: Client: The 1Password Connect client """ token = os.environ.get(ENV_SERVICE_ACCOUNT_JWT_VARIABLE) - is_async = os.environ.get(ENV_IS_ASYNC_CLIENT) == "True" if url is None: url = os.environ.get(CONNECT_HOST_ENV_VARIABLE) @@ -423,4 +416,4 @@ def new_client_from_environment(url: str = None) -> Union[AsyncClient, Client]: f"{ENV_SERVICE_ACCOUNT_JWT_VARIABLE} variable" ) - return new_client(url, token, is_async) + return new_client(url, token) diff --git a/src/tests/test_client_items.py b/src/tests/test_client_items.py index 1f5f74d..3f81a88 100644 --- a/src/tests/test_client_items.py +++ b/src/tests/test_client_items.py @@ -1,6 +1,6 @@ import pytest from httpx import Response -from onepasswordconnectsdk import client, models +from onepasswordconnectsdk import async_client, client, models VAULT_ID = "hfnjvi6aymbsnfc2xeeoheizda" VAULT_TITLE = "VaultA" @@ -9,7 +9,7 @@ HOST = "https://mock_host" TOKEN = "jwt_token" SS_CLIENT = client.new_client(HOST, TOKEN) -SS_CLIENT_ASYNC = client.new_client(HOST, TOKEN, True) +SS_CLIENT_ASYNC = async_client.new_async_client(HOST, TOKEN) def test_get_item_by_id(respx_mock): diff --git a/src/tests/test_client_vaults.py b/src/tests/test_client_vaults.py index 9a3eab3..d0aa8ab 100644 --- a/src/tests/test_client_vaults.py +++ b/src/tests/test_client_vaults.py @@ -1,13 +1,13 @@ import pytest from httpx import Response -from onepasswordconnectsdk import client +from onepasswordconnectsdk import async_client, client VAULT_ID = "hfnjvi6aymbsnfc2xeeoheizda" VAULT_NAME = "VaultA" HOST = "https://mock_host" TOKEN = "jwt_token" SS_CLIENT = client.new_client(HOST, TOKEN) -SS_CLIENT_ASYNC = client.new_client(HOST, TOKEN, True) +SS_CLIENT_ASYNC = async_client.new_async_client(HOST, TOKEN) def test_get_vaults(respx_mock):