Skip to content
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

Adds source and source type for better usage monitoring #43

Merged
merged 4 commits into from
Nov 7, 2024
Merged
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
21 changes: 19 additions & 2 deletions cdp/cdp.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import os

from cdp import __version__
from cdp.api_clients import ApiClients
from cdp.cdp_api_client import CdpApiClient
from cdp.constants import SDK_DEFAULT_SOURCE
from cdp.errors import InvalidConfigurationError


Expand Down Expand Up @@ -54,6 +56,8 @@ def configure(
debugging: bool = False,
base_path: str = "https://api.cdp.coinbase.com/platform",
max_network_retries: int = 3,
source: str = SDK_DEFAULT_SOURCE,
source_version: str = __version__,
) -> None:
"""Configure the CDP SDK.

Expand All @@ -64,6 +68,8 @@ def configure(
debugging (bool): Whether debugging is enabled. Defaults to False.
base_path (str): The base URL for the CDP API. Defaults to "https://api.cdp.coinbase.com/platform".
max_network_retries (int): The maximum number of network retries. Defaults to 3.
source (Optional[str]): Specifies whether the sdk is being used directly or if it's an Agentkit extension.
source_version (Optional[str]): The version of the source package.

"""
cls.api_key_name = api_key_name
Expand All @@ -74,7 +80,13 @@ def configure(
cls.max_network_retries = max_network_retries

cdp_client = CdpApiClient(
api_key_name, private_key, base_path, debugging, max_network_retries
api_key_name,
private_key,
base_path,
debugging,
max_network_retries,
source,
source_version,
)
cls.api_clients = ApiClients(cdp_client)

Expand All @@ -86,6 +98,8 @@ def configure_from_json(
debugging: bool = False,
base_path: str = "https://api.cdp.coinbase.com/platform",
max_network_retries: int = 3,
source: str = SDK_DEFAULT_SOURCE,
source_version: str = __version__,
) -> None:
"""Configure the CDP SDK from a JSON file.

Expand All @@ -95,6 +109,8 @@ def configure_from_json(
debugging (bool): Whether debugging is enabled. Defaults to False.
base_path (str): The base URL for the CDP API. Defaults to "https://api.cdp.coinbase.com/platform".
max_network_retries (int): The maximum number of network retries. Defaults to 3.
source (Optional[str]): Specifies whether the sdk is being used directly or if it's an Agentkit extension.
source_version (Optional[str]): The version of the source package.

Raises:
InvalidConfigurationError: If the JSON file is missing the 'api_key_name' or 'private_key'.
Expand All @@ -108,12 +124,13 @@ def configure_from_json(
raise InvalidConfigurationError("Invalid JSON format: Missing 'api_key_name'")
if not private_key:
raise InvalidConfigurationError("Invalid JSON format: Missing 'private_key'")

cls.configure(
api_key_name,
private_key,
use_server_signer,
debugging,
base_path,
max_network_retries,
source,
source_version,
)
11 changes: 10 additions & 1 deletion cdp/cdp_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from cdp.client.api_response import T as ApiResponseT # noqa: N811
from cdp.client.configuration import Configuration
from cdp.client.exceptions import ApiException
from cdp.constants import SDK_DEFAULT_SOURCE
from cdp.errors import ApiError, InvalidAPIKeyFormatError


Expand All @@ -27,6 +28,8 @@ def __init__(
host: str = "https://api.cdp.coinbase.com/platform",
debugging: bool = False,
max_network_retries: int = 3,
source: str = SDK_DEFAULT_SOURCE,
source_version: str = __version__,
):
"""Initialize the CDP API Client.

Expand All @@ -36,6 +39,8 @@ def __init__(
host (str, optional): The base URL for the API. Defaults to "https://api.cdp.coinbase.com/platform".
debugging (bool): Whether debugging is enabled.
max_network_retries (int): The maximum number of network retries. Defaults to 3.
source (str): Specifies whether the sdk is being used directly or if it's an Agentkit extension.
source_version (str): The version of the source package.

"""
retry_strategy = self._get_retry_strategy(max_network_retries)
Expand All @@ -44,6 +49,8 @@ def __init__(
self._api_key = api_key
self._private_key = private_key
self._debugging = debugging
self._source = source
self._source_version = source_version

@property
def api_key(self) -> str:
Expand Down Expand Up @@ -208,7 +215,7 @@ def _nonce(self) -> str:
return "".join(random.choices("0123456789", k=16))

def _get_correlation_data(self) -> str:
"""Return encoded correlation data including the SDK version and language.
"""Return encoded correlation data including the SDK version, language, and source.

Returns:
str: The correlation data.
Expand All @@ -217,6 +224,8 @@ def _get_correlation_data(self) -> str:
data = {
"sdk_version": __version__,
"sdk_language": "python",
"source": self._source,
"source_version": self._source_version,
}
return ",".join(f"{key}={value}" for key, value in data.items())

Expand Down
4 changes: 4 additions & 0 deletions cdp/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Specifies package level constants used throughout the package."""

# SDK_DEFAULT_SOURCE (str): Denotes the default source for the Python SDK.
SDK_DEFAULT_SOURCE = "sdk"
34 changes: 34 additions & 0 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from cdp import __version__
from cdp.cdp import Cdp
from cdp.cdp_api_client import CdpApiClient
from cdp.constants import SDK_DEFAULT_SOURCE


def test_api_client_get_correlation_data():
"""Tests _get_correlation_data from the CdpApiClient."""
cdp_api_client = CdpApiClient(
api_key="test",
private_key="test",
)
expected_result = f"""sdk_version={__version__},sdk_language=python,source={SDK_DEFAULT_SOURCE},source_version={__version__}"""
assert cdp_api_client._get_correlation_data() == expected_result

cdp_api_client2 = CdpApiClient(
api_key="test",
private_key="test",
host="https://api.cdp.coinbase.com/platform",
debugging=False,
max_network_retries=3,
source="test",
source_version="test_ver",
)
expected_result2 = (
f"""sdk_version={__version__},sdk_language=python,source=test,source_version=test_ver"""
)
assert cdp_api_client2._get_correlation_data() == expected_result2

Cdp.configure(api_key_name="test", private_key="test")
assert Cdp.api_clients._cdp_client._get_correlation_data() == expected_result

Cdp.configure(api_key_name="test", private_key="test", source="test", source_version="test_ver")
assert Cdp.api_clients._cdp_client._get_correlation_data() == expected_result2
Loading