Skip to content

Commit 16e5478

Browse files
committed
Introduce dataspace awareness
1 parent 2c00d4a commit 16e5478

4 files changed

Lines changed: 38 additions & 4 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ Options:
180180
- `--client-id TEXT`: Connected App Client ID
181181
- `--client-secret TEXT`: Connected App Client Secret
182182
- `--login-url TEXT`: Salesforce login URL
183+
- `--dataspace TEXT`: Dataspace name (optional, for non-default dataspaces)
183184

184185

185186
#### `datacustomcode init`
@@ -308,6 +309,16 @@ You can read more about Jupyter Notebooks here: https://jupyter.org/
308309

309310
You now have all fields necessary for the `datacustomcode configure` command.
310311

312+
### Working with Dataspaces
313+
314+
If you're working with a non-default dataspace in Salesforce Data Cloud, you can specify the dataspace during configuration:
315+
316+
```bash
317+
datacustomcode configure --dataspace my-dataspace
318+
```
319+
320+
**For default dataspaces**, you can omit the `--dataspace` parameter entirely - the SDK will connect to the default dataspace automatically.
321+
311322
## Other docs
312323

313324
- [Troubleshooting](./docs/troubleshooting.md)

src/datacustomcode/cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ def version():
5050
@click.option("--client-id", prompt=True)
5151
@click.option("--client-secret", prompt=True)
5252
@click.option("--login-url", prompt=True)
53+
@click.option("--dataspace", default="default", help="Dataspace name (optional, for non-default dataspaces)")
5354
def configure(
5455
username: str,
5556
password: str,
5657
client_id: str,
5758
client_secret: str,
5859
login_url: str,
5960
profile: str,
61+
dataspace: str | None,
6062
) -> None:
6163
from datacustomcode.credentials import Credentials
6264

@@ -66,6 +68,7 @@ def configure(
6668
client_id=client_id,
6769
client_secret=client_secret,
6870
login_url=login_url,
71+
dataspace=dataspace,
6972
).update_ini(profile=profile)
7073

7174

src/datacustomcode/credentials.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"client_id": "SFDC_CLIENT_ID",
2727
"client_secret": "SFDC_CLIENT_SECRET",
2828
"login_url": "SFDC_LOGIN_URL",
29+
"dataspace": "SFDC_DATASPACE",
2930
}
3031
INI_FILE = os.path.expanduser("~/.datacustomcode/credentials.ini")
3132

@@ -37,6 +38,7 @@ class Credentials:
3738
client_id: str
3839
client_secret: str
3940
login_url: str
41+
dataspace: str | None = None
4042

4143
@classmethod
4244
def from_ini(
@@ -47,21 +49,30 @@ def from_ini(
4749
config = configparser.ConfigParser()
4850
logger.debug(f"Reading {ini_file} for profile {profile}")
4951
config.read(ini_file)
52+
dataspace = config[profile].get("dataspace")
5053
return cls(
5154
username=config[profile]["username"],
5255
password=config[profile]["password"],
5356
client_id=config[profile]["client_id"],
5457
client_secret=config[profile]["client_secret"],
5558
login_url=config[profile]["login_url"],
59+
dataspace=dataspace,
5660
)
5761

5862
@classmethod
5963
def from_env(cls) -> Credentials:
6064
try:
61-
return cls(**{k: os.environ[v] for k, v in ENV_CREDENTIALS.items()})
65+
credentials_data = {}
66+
for k, v in ENV_CREDENTIALS.items():
67+
if k == "dataspace":
68+
credentials_data[k] = os.environ.get(v)
69+
else:
70+
credentials_data[k] = os.environ[v]
71+
return cls(**credentials_data)
6272
except KeyError as exc:
73+
required_vars = [v for k, v in ENV_CREDENTIALS.items() if k != "dataspace"]
6374
raise ValueError(
64-
f"All of {ENV_CREDENTIALS.values()} must be set in environment."
75+
f"All of {required_vars} must be set in environment. "
6576
) from exc
6677

6778
@classmethod
@@ -92,6 +103,9 @@ def update_ini(self, profile: str = "default", ini_file: str = INI_FILE):
92103
config[profile]["client_id"] = self.client_id
93104
config[profile]["client_secret"] = self.client_secret
94105
config[profile]["login_url"] = self.login_url
106+
107+
if self.dataspace is not None:
108+
config[profile]["dataspace"] = self.dataspace
95109

96110
with open(expanded_ini_file, "w") as f:
97111
config.write(f)

src/datacustomcode/io/reader/query_api.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,19 @@ def __init__(
8181
self.spark = spark
8282
credentials = Credentials.from_available(profile=credentials_profile)
8383

84-
self._conn = SalesforceCDPConnection(
84+
connection_args = [
8585
credentials.login_url,
8686
credentials.username,
8787
credentials.password,
8888
credentials.client_id,
8989
credentials.client_secret,
90-
)
90+
]
91+
92+
connection_kwargs = {}
93+
if credentials.dataspace is not None:
94+
connection_kwargs["dataspace"] = credentials.dataspace
95+
96+
self._conn = SalesforceCDPConnection(*connection_args, **connection_kwargs)
9197

9298
def read_dlo(
9399
self,

0 commit comments

Comments
 (0)