Skip to content

Cleaner client constructors #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/onepasswordconnectsdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
45 changes: 45 additions & 0 deletions src/onepasswordconnectsdk/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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)
15 changes: 4 additions & 11 deletions src/onepasswordconnectsdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)
4 changes: 2 additions & 2 deletions src/tests/test_client_items.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test_client_vaults.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down