From 81dc4f68585d201dc248664207537e42c084172f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Slettvold?= Date: Mon, 23 Oct 2023 20:43:28 +0200 Subject: [PATCH 1/6] client.py does not need to start async_client. --- src/onepasswordconnectsdk/client.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/onepasswordconnectsdk/client.py b/src/onepasswordconnectsdk/client.py index 4202ccf..4120b75 100644 --- a/src/onepasswordconnectsdk/client.py +++ b/src/onepasswordconnectsdk/client.py @@ -4,7 +4,6 @@ import json 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 ( @@ -380,22 +379,19 @@ def sanitize_for_serialization(self, obj): return self.serializer.sanitize_for_serialization(obj) -def new_client(url: str, token: str, is_async: bool = False): +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): +def new_client_from_environment(url: str = None) -> Client: """Builds a new client for interacting with 1Password Connect using the OP_TOKEN environment variable @@ -407,7 +403,6 @@ def new_client_from_environment(url: str = None): 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) @@ -422,4 +417,4 @@ def new_client_from_environment(url: str = None): f"{ENV_SERVICE_ACCOUNT_JWT_VARIABLE} variable" ) - return new_client(url, token, is_async) + return new_client(url, token) From 634603034a8c13b204a53217d5e73f716e4d2d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Slettvold?= Date: Mon, 23 Oct 2023 20:43:50 +0200 Subject: [PATCH 2/6] Remove token from create from env doc. Fixes #88. --- src/onepasswordconnectsdk/client.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/onepasswordconnectsdk/client.py b/src/onepasswordconnectsdk/client.py index 4120b75..623ef5c 100644 --- a/src/onepasswordconnectsdk/client.py +++ b/src/onepasswordconnectsdk/client.py @@ -397,7 +397,6 @@ def new_client_from_environment(url: str = None) -> Client: Parameters: url: The url of the 1Password Connect API - token: The 1Password Service Account token Returns: Client: The 1Password Connect client From 7f035b9c8e4eaf1b57d736f97dd72d9c1909bebc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Slettvold?= Date: Mon, 23 Oct 2023 20:43:58 +0200 Subject: [PATCH 3/6] Allow async_client to create itself. Add those constructors to the package. --- src/onepasswordconnectsdk/__init__.py | 2 + src/onepasswordconnectsdk/async_client.py | 45 +++++++++++++++++++++++ 2 files changed, 47 insertions(+) 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 415924c..3222ab1 100644 --- a/src/onepasswordconnectsdk/async_client.py +++ b/src/onepasswordconnectsdk/async_client.py @@ -4,9 +4,14 @@ import json 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, ) @@ -371,3 +376,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_TOKEN environment variable + + Parameters: + url: The url of the 1Password Connect API + + Returns: + Client: 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) From 9d9b36da31478275be2aa84435295d7013748115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Slettvold?= Date: Mon, 23 Oct 2023 20:46:29 +0200 Subject: [PATCH 4/6] Remove env variable for async client. --- src/onepasswordconnectsdk/client.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/onepasswordconnectsdk/client.py b/src/onepasswordconnectsdk/client.py index 623ef5c..61b00fa 100644 --- a/src/onepasswordconnectsdk/client.py +++ b/src/onepasswordconnectsdk/client.py @@ -16,7 +16,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: From 79bdeb562723eec6300e273ef781738a1ee467a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Slettvold?= Date: Mon, 23 Oct 2023 20:57:01 +0200 Subject: [PATCH 5/6] Update tests to use the new constructors. --- src/tests/test_client_items.py | 4 ++-- src/tests/test_client_vaults.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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): From fb13bede9eaa5693ce71a735f76dfeb178fd1a77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Slettvold?= Date: Mon, 23 Oct 2023 21:03:01 +0200 Subject: [PATCH 6/6] Brush up a couple of inconsistencies in the docstrings. --- src/onepasswordconnectsdk/async_client.py | 4 ++-- src/onepasswordconnectsdk/client.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/onepasswordconnectsdk/async_client.py b/src/onepasswordconnectsdk/async_client.py index 3222ab1..560e1c7 100644 --- a/src/onepasswordconnectsdk/async_client.py +++ b/src/onepasswordconnectsdk/async_client.py @@ -392,13 +392,13 @@ def new_async_client(url: str, token: str) -> AsyncClient: def new_async_client_from_environment(url: str = None) -> AsyncClient: """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 Returns: - Client: The 1Password Connect client + AsyncClient: The 1Password Connect client """ token = os.environ.get(ENV_SERVICE_ACCOUNT_JWT_VARIABLE) diff --git a/src/onepasswordconnectsdk/client.py b/src/onepasswordconnectsdk/client.py index 61b00fa..529e45a 100644 --- a/src/onepasswordconnectsdk/client.py +++ b/src/onepasswordconnectsdk/client.py @@ -392,7 +392,7 @@ def new_client(url: str, token: str) -> 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