Creating a client and authenticating
dealcloud_sdk
's main entrypoint is the DealCloud
class. When it is instantiated, the object is scoped to a given DealCloud environment, by passing the site URL and API credentials as arguments:
from dealcloud_sdk import DealCloud
dc = DealCloud(
site_url="client.dealcloud.com",
client_id="12345",
client_secret="your_client_secret",
)
Whilst suitable for local development, the above implementation is insecure if you keep code in online repositories such as GitHub. Please see the below examples for more secure implementations.
Securely loading credentials
Using Environment Variables
Where the environment variables contain the relevant values. These can be loaded from a .env file using the python-dotenv
PyPi package (opens in a new tab).
The default environment variable names are, these can be overridden if desired:
DC_SDK_SITE_URL
DC_SDK_CLIENT_ID
DC_SDK_CLIENT_SECRET
from dealcloud_sdk import DealCloud
dc = DealCloud().from_env()
To override the default environment variable names:
from dealcloud_sdk import DealCloud
dc = DealCloud().from_env(
site_url_env_name="OVERRIDDEN_SITE_KEY",
client_id_env_name="OVERRIDDEN_CLIENT_ID_KEY",
client_secret_env_name="OVERRIDDEN_CLIENT_SECRET_KEY",
)
Using JSON Config File
Given a JSON file in the below format:
{
"site_url": "client.dealcloud.com",
"client_id": 12345,
"client_secret": "your_client_secret"
}
The DealCloud object can be created as below:
from dealcloud_sdk import DealCloud
dc = DealCloud.from_json("path/to/json_config_file.json")
Similarly, if the credentials are stored as part of a larger JSON config file, a key path can be passed as the second argument, directing DealCloud
to the path of the credentials in the wider JSON. For example:
{
"other": [1, 2, 3],
"creds": {
"dc": {
"site_url": "client.dealcloud.com",
"client_id": 12345,
"client_secret": "your_client_secret"
}
}
}
from dealcloud_sdk import DealCloud
dc = DealCloud.from_json("path/to/json_config_file.json", "creds.dc")
Using YAML Config File
Given a YAML file in the below format:
site_url: "client.dealcloud.com"
client_id: 12345
client_secret: "your_client_secret"
The DealCloud object can be created as below:
from dealcloud_sdk import DealCloud
dc = DealCloud.from_yaml("path/to/yaml_config_file.json")
Similarly, if the credentials are stored as part of a larger YAML config file, a key path can be passed as the second argument, directing DealCloud
to the path of the credentials in the wider YAML. For example:
other:
- 1
- 2
- 3
creds:
dc:
site_url: "client.dealcloud.com"
client_id: 12345
client_secret: "your_client_secret"
from dealcloud_sdk import DealCloud
dc = DealCloud.from_yaml("path/to/yaml_config_file.json", "creds.dc")