Skip to content

Commit 06e33d5

Browse files
adriangomez24Adrian Lopez
and
Adrian Lopez
authored
Adds source and source type for better usage monitoring (#43)
* add source and source version to better monitor usage * improve docstring * address changes and add more tests * set defaults and add more tests --------- Co-authored-by: Adrian Lopez <[email protected]>
1 parent d8264a0 commit 06e33d5

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

cdp/cdp.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import json
22
import os
33

4+
from cdp import __version__
45
from cdp.api_clients import ApiClients
56
from cdp.cdp_api_client import CdpApiClient
7+
from cdp.constants import SDK_DEFAULT_SOURCE
68
from cdp.errors import InvalidConfigurationError
79

810

@@ -54,6 +56,8 @@ def configure(
5456
debugging: bool = False,
5557
base_path: str = "https://api.cdp.coinbase.com/platform",
5658
max_network_retries: int = 3,
59+
source: str = SDK_DEFAULT_SOURCE,
60+
source_version: str = __version__,
5761
) -> None:
5862
"""Configure the CDP SDK.
5963
@@ -64,6 +68,8 @@ def configure(
6468
debugging (bool): Whether debugging is enabled. Defaults to False.
6569
base_path (str): The base URL for the CDP API. Defaults to "https://api.cdp.coinbase.com/platform".
6670
max_network_retries (int): The maximum number of network retries. Defaults to 3.
71+
source (Optional[str]): Specifies whether the sdk is being used directly or if it's an Agentkit extension.
72+
source_version (Optional[str]): The version of the source package.
6773
6874
"""
6975
cls.api_key_name = api_key_name
@@ -74,7 +80,13 @@ def configure(
7480
cls.max_network_retries = max_network_retries
7581

7682
cdp_client = CdpApiClient(
77-
api_key_name, private_key, base_path, debugging, max_network_retries
83+
api_key_name,
84+
private_key,
85+
base_path,
86+
debugging,
87+
max_network_retries,
88+
source,
89+
source_version,
7890
)
7991
cls.api_clients = ApiClients(cdp_client)
8092

@@ -86,6 +98,8 @@ def configure_from_json(
8698
debugging: bool = False,
8799
base_path: str = "https://api.cdp.coinbase.com/platform",
88100
max_network_retries: int = 3,
101+
source: str = SDK_DEFAULT_SOURCE,
102+
source_version: str = __version__,
89103
) -> None:
90104
"""Configure the CDP SDK from a JSON file.
91105
@@ -95,6 +109,8 @@ def configure_from_json(
95109
debugging (bool): Whether debugging is enabled. Defaults to False.
96110
base_path (str): The base URL for the CDP API. Defaults to "https://api.cdp.coinbase.com/platform".
97111
max_network_retries (int): The maximum number of network retries. Defaults to 3.
112+
source (Optional[str]): Specifies whether the sdk is being used directly or if it's an Agentkit extension.
113+
source_version (Optional[str]): The version of the source package.
98114
99115
Raises:
100116
InvalidConfigurationError: If the JSON file is missing the 'api_key_name' or 'private_key'.
@@ -108,12 +124,13 @@ def configure_from_json(
108124
raise InvalidConfigurationError("Invalid JSON format: Missing 'api_key_name'")
109125
if not private_key:
110126
raise InvalidConfigurationError("Invalid JSON format: Missing 'private_key'")
111-
112127
cls.configure(
113128
api_key_name,
114129
private_key,
115130
use_server_signer,
116131
debugging,
117132
base_path,
118133
max_network_retries,
134+
source,
135+
source_version,
119136
)

cdp/cdp_api_client.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from cdp.client.api_response import T as ApiResponseT # noqa: N811
1515
from cdp.client.configuration import Configuration
1616
from cdp.client.exceptions import ApiException
17+
from cdp.constants import SDK_DEFAULT_SOURCE
1718
from cdp.errors import ApiError, InvalidAPIKeyFormatError
1819

1920

@@ -27,6 +28,8 @@ def __init__(
2728
host: str = "https://api.cdp.coinbase.com/platform",
2829
debugging: bool = False,
2930
max_network_retries: int = 3,
31+
source: str = SDK_DEFAULT_SOURCE,
32+
source_version: str = __version__,
3033
):
3134
"""Initialize the CDP API Client.
3235
@@ -36,6 +39,8 @@ def __init__(
3639
host (str, optional): The base URL for the API. Defaults to "https://api.cdp.coinbase.com/platform".
3740
debugging (bool): Whether debugging is enabled.
3841
max_network_retries (int): The maximum number of network retries. Defaults to 3.
42+
source (str): Specifies whether the sdk is being used directly or if it's an Agentkit extension.
43+
source_version (str): The version of the source package.
3944
4045
"""
4146
retry_strategy = self._get_retry_strategy(max_network_retries)
@@ -44,6 +49,8 @@ def __init__(
4449
self._api_key = api_key
4550
self._private_key = private_key
4651
self._debugging = debugging
52+
self._source = source
53+
self._source_version = source_version
4754

4855
@property
4956
def api_key(self) -> str:
@@ -208,7 +215,7 @@ def _nonce(self) -> str:
208215
return "".join(random.choices("0123456789", k=16))
209216

210217
def _get_correlation_data(self) -> str:
211-
"""Return encoded correlation data including the SDK version and language.
218+
"""Return encoded correlation data including the SDK version, language, and source.
212219
213220
Returns:
214221
str: The correlation data.
@@ -217,6 +224,8 @@ def _get_correlation_data(self) -> str:
217224
data = {
218225
"sdk_version": __version__,
219226
"sdk_language": "python",
227+
"source": self._source,
228+
"source_version": self._source_version,
220229
}
221230
return ",".join(f"{key}={value}" for key, value in data.items())
222231

cdp/constants.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""Specifies package level constants used throughout the package."""
2+
3+
# SDK_DEFAULT_SOURCE (str): Denotes the default source for the Python SDK.
4+
SDK_DEFAULT_SOURCE = "sdk"

tests/test_api_client.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from cdp import __version__
2+
from cdp.cdp import Cdp
3+
from cdp.cdp_api_client import CdpApiClient
4+
from cdp.constants import SDK_DEFAULT_SOURCE
5+
6+
7+
def test_api_client_get_correlation_data():
8+
"""Tests _get_correlation_data from the CdpApiClient."""
9+
cdp_api_client = CdpApiClient(
10+
api_key="test",
11+
private_key="test",
12+
)
13+
expected_result = f"""sdk_version={__version__},sdk_language=python,source={SDK_DEFAULT_SOURCE},source_version={__version__}"""
14+
assert cdp_api_client._get_correlation_data() == expected_result
15+
16+
cdp_api_client2 = CdpApiClient(
17+
api_key="test",
18+
private_key="test",
19+
host="https://api.cdp.coinbase.com/platform",
20+
debugging=False,
21+
max_network_retries=3,
22+
source="test",
23+
source_version="test_ver",
24+
)
25+
expected_result2 = (
26+
f"""sdk_version={__version__},sdk_language=python,source=test,source_version=test_ver"""
27+
)
28+
assert cdp_api_client2._get_correlation_data() == expected_result2
29+
30+
Cdp.configure(api_key_name="test", private_key="test")
31+
assert Cdp.api_clients._cdp_client._get_correlation_data() == expected_result
32+
33+
Cdp.configure(api_key_name="test", private_key="test", source="test", source_version="test_ver")
34+
assert Cdp.api_clients._cdp_client._get_correlation_data() == expected_result2

0 commit comments

Comments
 (0)