-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathquery_api.py
More file actions
245 lines (207 loc) · 9.12 KB
/
query_api.py
File metadata and controls
245 lines (207 loc) · 9.12 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Copyright (c) 2025, Salesforce, Inc.
# SPDX-License-Identifier: Apache-2
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import logging
from typing import (
TYPE_CHECKING,
Final,
Optional,
Union,
)
from salesforcecdpconnector.connection import SalesforceCDPConnection
from datacustomcode.credentials import AuthType, Credentials
from datacustomcode.io.reader.base import BaseDataCloudReader
from datacustomcode.io.reader.sf_cli import SFCLIDataCloudReader
from datacustomcode.io.reader.utils import _pandas_to_spark_schema
if TYPE_CHECKING:
from pyspark.sql import DataFrame as PySparkDataFrame, SparkSession
from pyspark.sql.types import AtomicType, StructType
logger = logging.getLogger(__name__)
SQL_QUERY_TEMPLATE: Final = "SELECT * FROM {} LIMIT {}"
SQL_QUERY_TEMPLATE_NO_LIMIT: Final = "SELECT * FROM {}"
def create_cdp_connection(
credentials: Credentials,
dataspace: Optional[str] = None,
) -> SalesforceCDPConnection:
"""Create a SalesforceCDPConnection based on the credentials auth type.
This factory function creates the appropriate connection based on the
authentication method configured in the credentials.
Args:
credentials: Credentials instance with authentication details.
dataspace: Optional dataspace identifier for multi-tenant queries.
If None or "default", the dataspace argument is not passed to
the connection constructor.
Returns:
SalesforceCDPConnection configured for the specified auth method.
Raises:
ValueError: If the auth type is not supported.
"""
effective_dataspace = dataspace if dataspace and dataspace != "default" else None
if credentials.auth_type == AuthType.OAUTH_TOKENS:
logger.debug("Creating CDP connection with OAuth Tokens authentication")
if effective_dataspace is not None:
return SalesforceCDPConnection(
credentials.login_url,
client_id=credentials.client_id,
client_secret=credentials.client_secret,
refresh_token=credentials.refresh_token,
dataspace=effective_dataspace,
)
else:
return SalesforceCDPConnection(
credentials.login_url,
client_id=credentials.client_id,
client_secret=credentials.client_secret,
refresh_token=credentials.refresh_token,
)
elif credentials.auth_type == AuthType.CLIENT_CREDENTIALS:
logger.debug("Creating CDP connection with Client Credentials authentication")
if effective_dataspace is not None:
return SalesforceCDPConnection(
credentials.login_url,
client_id=credentials.client_id,
client_secret=credentials.client_secret,
dataspace=effective_dataspace,
)
else:
return SalesforceCDPConnection(
credentials.login_url,
client_id=credentials.client_id,
client_secret=credentials.client_secret,
)
else:
raise ValueError(f"Unsupported authentication type: {credentials.auth_type}")
class QueryAPIDataCloudReader(BaseDataCloudReader):
"""DataCloud reader using Query API.
This reader emulates data access within Data Cloud by calling the Query API.
Supports multiple authentication methods:
- OAuth Tokens (default, needs client_id/secret with refresh_token)
- Client Credentials (server-to-server, needs client_id/secret only)
- SF CLI (uses ``sf org display`` access token via the REST API directly)
Supports dataspace configuration for querying data within specific dataspaces.
When a dataspace is provided (and not "default"), queries are executed within
that dataspace context.
"""
CONFIG_NAME = "QueryAPIDataCloudReader"
def __init__(
self,
spark: SparkSession,
credentials_profile: str = "default",
dataspace: Optional[str] = None,
sf_cli_org: Optional[str] = None,
default_row_limit: Optional[int] = None,
) -> None:
"""Initialize QueryAPIDataCloudReader.
Args:
spark: SparkSession instance for creating DataFrames.
credentials_profile: Credentials profile name (default: "default").
The profile determines which credentials to load from the
~/.datacustomcode/credentials.ini file or environment variables.
dataspace: Optional dataspace identifier. If provided and not "default",
the connection will be configured for the specified dataspace.
When None or "default", uses the default dataspace.
sf_cli_org: Optional SF CLI org alias or username. When set, the
reader delegates to :class:`SFCLIDataCloudReader` which calls
the Data Cloud REST API directly using the token obtained from
``sf org display``, bypassing the CDP token-exchange flow.
default_row_limit: Maximum number of rows to fetch automatically.
When ``None``, no limit is applied (all rows are returned).
Set via ``default_row_limit`` in ``config.yaml`` reader options.
"""
self.spark = spark
self._default_row_limit = default_row_limit
if sf_cli_org:
logger.debug(
f"Initializing QueryAPIDataCloudReader with SF CLI org '{sf_cli_org}'"
)
self._sf_cli_reader: Optional[SFCLIDataCloudReader] = SFCLIDataCloudReader(
spark=spark,
sf_cli_org=sf_cli_org,
dataspace=dataspace,
default_row_limit=default_row_limit,
)
self._conn = None
else:
self._sf_cli_reader = None
credentials = Credentials.from_available(profile=credentials_profile)
logger.debug(
"Initializing QueryAPIDataCloudReader with "
f"auth_type={credentials.auth_type.value}"
)
self._conn = create_cdp_connection(credentials, dataspace)
def _build_query(self, name: str) -> str:
"""Build a SQL query, applying the configured default row limit.
Args:
name: Object name to query.
Returns:
SQL query string.
"""
if self._default_row_limit is not None:
return SQL_QUERY_TEMPLATE.format(name, self._default_row_limit)
return SQL_QUERY_TEMPLATE_NO_LIMIT.format(name)
def read_dlo(
self,
name: str,
schema: Union[AtomicType, StructType, str, None] = None,
) -> PySparkDataFrame:
"""
Read a Data Lake Object (DLO) from the Data Cloud.
Args:
name (str): The name of the DLO.
schema (Optional[Union[AtomicType, StructType, str]]): Schema of the DLO.
Returns:
PySparkDataFrame: The PySpark DataFrame.
"""
sf_cli_reader: Optional[SFCLIDataCloudReader] = getattr(
self, "_sf_cli_reader", None
)
if sf_cli_reader is not None:
return sf_cli_reader.read_dlo(name, schema) # type: ignore[no-any-return]
query = self._build_query(name)
assert self._conn is not None
pandas_df = self._conn.get_pandas_dataframe(query)
# Convert pandas DataFrame to Spark DataFrame
if not schema:
# auto infer schema
schema = _pandas_to_spark_schema(pandas_df)
spark_dataframe = self.spark.createDataFrame(pandas_df, schema)
return spark_dataframe
def read_dmo(
self,
name: str,
schema: Union[AtomicType, StructType, str, None] = None,
) -> PySparkDataFrame:
"""
Read a Data Model Object (DMO) from the Data Cloud.
Args:
name (str): The name of the DMO.
schema (Optional[Union[AtomicType, StructType, str]]): Schema of the DMO.
Returns:
PySparkDataFrame: The PySpark DataFrame.
"""
sf_cli_reader: Optional[SFCLIDataCloudReader] = getattr(
self, "_sf_cli_reader", None
)
if sf_cli_reader is not None:
return sf_cli_reader.read_dmo(name, schema) # type: ignore[no-any-return]
query = self._build_query(name)
assert self._conn is not None
pandas_df = self._conn.get_pandas_dataframe(query)
# Convert pandas DataFrame to Spark DataFrame
if not schema:
# auto infer schema
schema = _pandas_to_spark_schema(pandas_df)
spark_dataframe = self.spark.createDataFrame(pandas_df, schema)
return spark_dataframe