-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcdp.py
144 lines (122 loc) · 5.51 KB
/
cdp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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, UninitializedSDKError
class Cdp:
"""The Cdp class is a singleton responsible for configuring and managing the Coinbase API client.
Attributes:
api_key_name (Optional[str]): The API key name.
private_key (Optional[str]): The private key associated with the API key.
use_server_signer (bool): Whether to use the server signer.
debugging (bool): Whether debugging is enabled.
base_path (str): The base URL for the Platform API.
max_network_retries (int): The maximum number of network retries.
api_clients: The Platform API clients instance.
"""
_instance = None
api_key_name = None
private_key = None
use_server_signer = False
debugging = False
base_path = "https://api.cdp.coinbase.com/platform"
max_network_retries = 3
class ApiClientsWrapper:
"""Wrapper that raises a helpful error when SDK is not initialized."""
def __getattr__(self, _name):
"""Raise an error when accessing an attribute of the ApiClientsWrapper."""
raise UninitializedSDKError()
api_clients = ApiClientsWrapper()
def __new__(cls):
"""Create or return the singleton instance of the Cdp class.
This method overrides the default `__new__` behavior to implement the Singleton pattern.
It ensures that only one instance of the Cdp class exists throughout the application's lifecycle.
If an instance already exists, it returns the existing instance; otherwise, it creates a new one.
Returns:
Cdp: The singleton instance of the Cdp class.
"""
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
@classmethod
def configure(
cls,
api_key_name: str,
private_key: str,
use_server_signer: bool = False,
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.
Args:
api_key_name (str): The API key name.
private_key (str): The private key associated with the API key.
use_server_signer (bool): Whether to use the server signer. Defaults to False.
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
cls.private_key = private_key
cls.use_server_signer = use_server_signer
cls.debugging = debugging
cls.base_path = base_path
cls.max_network_retries = max_network_retries
cdp_client = CdpApiClient(
api_key_name,
private_key,
base_path,
debugging,
max_network_retries,
source,
source_version,
)
cls.api_clients = ApiClients(cdp_client)
@classmethod
def configure_from_json(
cls,
file_path: str = "~/Downloads/cdp_api_key.json",
use_server_signer: bool = False,
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.
Args:
file_path (str): The path to the JSON file. Defaults to "~/Downloads/cdp_api_key.json".
use_server_signer (bool): Whether to use the server signer. Defaults to False.
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'.
"""
with open(os.path.expanduser(file_path)) as file:
data = json.load(file)
api_key_name = data.get("name") or data.get("id")
private_key = data.get("privateKey")
if not api_key_name:
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,
)