sfq 0.0.7__tar.gz → 0.0.9__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ 3.9
@@ -1,20 +1,17 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: sfq
3
- Version: 0.0.7
3
+ Version: 0.0.9
4
4
  Summary: Python wrapper for the Salesforce's Query API.
5
5
  Author-email: David Moruzzi <sfq.pypi@dmoruzi.com>
6
6
  Keywords: salesforce,salesforce query
7
7
  Classifier: Development Status :: 3 - Alpha
8
8
  Classifier: Intended Audience :: Developers
9
- Classifier: Programming Language :: Python :: 3.7
10
- Classifier: Programming Language :: Python :: 3.8
11
9
  Classifier: Programming Language :: Python :: 3.9
12
10
  Classifier: Programming Language :: Python :: 3.10
13
11
  Classifier: Programming Language :: Python :: 3.12
14
12
  Classifier: Programming Language :: Python :: 3.13
15
13
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
16
- Requires-Python: >=3.7
17
- Requires-Dist: prompt-toolkit>=3.0.3
14
+ Requires-Python: >=3.9
18
15
  Description-Content-Type: text/markdown
19
16
 
20
17
  # sfq (Salesforce Query)
@@ -39,24 +36,6 @@ pip install sfq
39
36
 
40
37
  ## Usage
41
38
 
42
- ### Interactive Querying
43
-
44
- ```powershell
45
- usage: python -m sfq [-a SFDXAUTHURL] [--dry-run] [--disable-fuzzy-completion]
46
-
47
- Interactively query Salesforce data with real-time autocompletion.
48
-
49
- options:
50
- -h, --help show this help message and exit
51
- -a, --sfdxAuthUrl SFDXAUTHURL
52
- Salesforce auth url
53
- --dry-run Print the query without executing it
54
- --disable-fuzzy-completion
55
- Disable fuzzy completion
56
- ```
57
-
58
- You can run the `sfq` library in interactive mode by passing the `-a` option with the `SFDX_AUTH_URL` argument or by setting the `SFDX_AUTH_URL` environment variable.
59
-
60
39
  ### Library Querying
61
40
 
62
41
  ```python
@@ -146,7 +125,9 @@ To use the `sfq` library, you'll need a **client ID** and **refresh token**. The
146
125
  pip install sfq && python -c "from sfq import SFAuth; sf = SFAuth(instance_url='$instanceUrl', client_id='$clientId', refresh_token='$refreshToken'); print(sf.query('$query'))" | jq -r '.records[].Id'
147
126
  ```
148
127
 
149
- ## Notes
128
+ ## Important Considerations
129
+
130
+ - **Security**: Safeguard your refresh token diligently, as it provides access to your Salesforce environment. Avoid sharing or exposing it in unsecured locations.
131
+ - **Efficient Data Retrieval**: The `query` function automatically handles pagination, simplifying record retrieval across large datasets. It's recommended to use the `LIMIT` clause in queries to control the volume of data returned.
132
+ - **Advanced Metadata Queries**: Utilize the `tooling=True` option within the `query` function to access the Salesforce Tooling API. This option is designed for performing complex metadata operations, enhancing your data management capabilities.
150
133
 
151
- - **Authentication**: Make sure your refresh token is kept secure, as it grants access to your Salesforce instance.
152
- - **Tooling API**: You can set the `tooling=True` argument in the `query` method to access the Salesforce Tooling API for more advanced metadata queries. This is limited to library usage only.
@@ -20,24 +20,6 @@ pip install sfq
20
20
 
21
21
  ## Usage
22
22
 
23
- ### Interactive Querying
24
-
25
- ```powershell
26
- usage: python -m sfq [-a SFDXAUTHURL] [--dry-run] [--disable-fuzzy-completion]
27
-
28
- Interactively query Salesforce data with real-time autocompletion.
29
-
30
- options:
31
- -h, --help show this help message and exit
32
- -a, --sfdxAuthUrl SFDXAUTHURL
33
- Salesforce auth url
34
- --dry-run Print the query without executing it
35
- --disable-fuzzy-completion
36
- Disable fuzzy completion
37
- ```
38
-
39
- You can run the `sfq` library in interactive mode by passing the `-a` option with the `SFDX_AUTH_URL` argument or by setting the `SFDX_AUTH_URL` environment variable.
40
-
41
23
  ### Library Querying
42
24
 
43
25
  ```python
@@ -127,7 +109,9 @@ To use the `sfq` library, you'll need a **client ID** and **refresh token**. The
127
109
  pip install sfq && python -c "from sfq import SFAuth; sf = SFAuth(instance_url='$instanceUrl', client_id='$clientId', refresh_token='$refreshToken'); print(sf.query('$query'))" | jq -r '.records[].Id'
128
110
  ```
129
111
 
130
- ## Notes
112
+ ## Important Considerations
113
+
114
+ - **Security**: Safeguard your refresh token diligently, as it provides access to your Salesforce environment. Avoid sharing or exposing it in unsecured locations.
115
+ - **Efficient Data Retrieval**: The `query` function automatically handles pagination, simplifying record retrieval across large datasets. It's recommended to use the `LIMIT` clause in queries to control the volume of data returned.
116
+ - **Advanced Metadata Queries**: Utilize the `tooling=True` option within the `query` function to access the Salesforce Tooling API. This option is designed for performing complex metadata operations, enhancing your data management capabilities.
131
117
 
132
- - **Authentication**: Make sure your refresh token is kept secure, as it grants access to your Salesforce instance.
133
- - **Tooling API**: You can set the `tooling=True` argument in the `query` method to access the Salesforce Tooling API for more advanced metadata queries. This is limited to library usage only.
@@ -1,21 +1,17 @@
1
1
  [project]
2
2
  name = "sfq"
3
- version = "0.0.7"
3
+ version = "0.0.9"
4
4
  description = "Python wrapper for the Salesforce's Query API."
5
5
  readme = "README.md"
6
6
  authors = [{ name = "David Moruzzi", email = "sfq.pypi@dmoruzi.com" }]
7
7
  keywords = ["salesforce", "salesforce query"]
8
- requires-python = ">=3.7"
9
- dependencies = [
10
- "prompt-toolkit>=3.0.3",
11
- ]
8
+ requires-python = ">=3.9"
9
+ dependencies = []
12
10
 
13
11
  classifiers = [
14
12
  "Development Status :: 3 - Alpha",
15
13
  "Intended Audience :: Developers",
16
14
  "Topic :: Software Development :: Libraries :: Python Modules",
17
- "Programming Language :: Python :: 3.7",
18
- "Programming Language :: Python :: 3.8",
19
15
  "Programming Language :: Python :: 3.9",
20
16
  "Programming Language :: Python :: 3.10",
21
17
  "Programming Language :: Python :: 3.12",
@@ -0,0 +1,398 @@
1
+ import http.client
2
+ import json
3
+ import logging
4
+ import os
5
+ import time
6
+ from typing import Any, Dict, Optional
7
+ from urllib.parse import quote, urlparse
8
+
9
+ TRACE = 5
10
+ logging.addLevelName(TRACE, "TRACE")
11
+
12
+
13
+ def trace(self: logging.Logger, message: str, *args: Any, **kwargs: Any) -> None:
14
+ """Custom TRACE level logging function with redaction."""
15
+
16
+ def _redact_sensitive(data: Any) -> Any:
17
+ """Redacts sensitive keys from a dictionary or query string."""
18
+ REDACT_VALUE = "*" * 8
19
+ if isinstance(data, dict):
20
+ return {
21
+ k: (
22
+ REDACT_VALUE
23
+ if k.lower() in ["access_token", "authorization", "refresh_token"]
24
+ else v
25
+ )
26
+ for k, v in data.items()
27
+ }
28
+ elif isinstance(data, str):
29
+ parts = data.split("&")
30
+ for i, part in enumerate(parts):
31
+ if "=" in part:
32
+ key, value = part.split("=", 1)
33
+ if key.lower() in [
34
+ "access_token",
35
+ "authorization",
36
+ "refresh_token",
37
+ ]:
38
+ parts[i] = f"{key}={REDACT_VALUE}"
39
+ return "&".join(parts)
40
+ return data
41
+
42
+ redacted_args = args
43
+ if args:
44
+ first = args[0]
45
+ if isinstance(first, str):
46
+ try:
47
+ loaded = json.loads(first)
48
+ first = loaded
49
+ except (json.JSONDecodeError, TypeError):
50
+ pass
51
+ redacted_first = _redact_sensitive(first)
52
+ redacted_args = (redacted_first,) + args[1:]
53
+
54
+ if self.isEnabledFor(TRACE):
55
+ self._log(TRACE, message, redacted_args, **kwargs)
56
+
57
+
58
+ logging.Logger.trace = trace
59
+ logger = logging.getLogger("sfq")
60
+
61
+
62
+ class SFAuth:
63
+ def __init__(
64
+ self,
65
+ instance_url: str,
66
+ client_id: str,
67
+ refresh_token: str,
68
+ api_version: str = "v63.0",
69
+ token_endpoint: str = "/services/oauth2/token",
70
+ access_token: Optional[str] = None,
71
+ token_expiration_time: Optional[float] = None,
72
+ token_lifetime: int = 15 * 60,
73
+ user_agent: str = "sfq/0.0.9",
74
+ proxy: str = "auto",
75
+ ) -> None:
76
+ """
77
+ Initializes the SFAuth with necessary parameters.
78
+
79
+ :param instance_url: The Salesforce instance URL.
80
+ :param client_id: The client ID for OAuth.
81
+ :param refresh_token: The refresh token for OAuth.
82
+ :param api_version: The Salesforce API version (default is "v63.0").
83
+ :param token_endpoint: The token endpoint (default is "/services/oauth2/token").
84
+ :param access_token: The access token for the current session (default is None).
85
+ :param token_expiration_time: The expiration time of the access token (default is None).
86
+ :param token_lifetime: The lifetime of the access token in seconds (default is 15 minutes).
87
+ :param user_agent: Custom User-Agent string (default is "sfq/0.0.9").
88
+ :param proxy: The proxy configuration, "auto" to use environment (default is "auto").
89
+ """
90
+ self.instance_url = instance_url
91
+ self.client_id = client_id
92
+ self.refresh_token = refresh_token
93
+ self.api_version = api_version
94
+ self.token_endpoint = token_endpoint
95
+ self.access_token = access_token
96
+ self.token_expiration_time = token_expiration_time
97
+ self.token_lifetime = token_lifetime
98
+ self.user_agent = user_agent
99
+ self._auto_configure_proxy(proxy)
100
+ self._high_api_usage_threshold = 80
101
+
102
+ def _auto_configure_proxy(self, proxy: str) -> None:
103
+ """
104
+ Automatically configure the proxy based on the environment or provided value.
105
+ """
106
+ if proxy == "auto":
107
+ self.proxy = os.environ.get("https_proxy")
108
+ if self.proxy:
109
+ logger.debug("Auto-configured proxy: %s", self.proxy)
110
+ else:
111
+ self.proxy = proxy
112
+ logger.debug("Using configured proxy: %s", self.proxy)
113
+
114
+ def _prepare_payload(self) -> Dict[str, str]:
115
+ """
116
+ Prepare the payload for the token request.
117
+ """
118
+ return {
119
+ "grant_type": "refresh_token",
120
+ "client_id": self.client_id,
121
+ "refresh_token": self.refresh_token,
122
+ }
123
+
124
+ def _create_connection(self, netloc: str) -> http.client.HTTPConnection:
125
+ """
126
+ Create a connection using HTTP or HTTPS, with optional proxy support.
127
+
128
+ :param netloc: The target host and port from the parsed instance URL.
129
+ :return: An HTTP(S)Connection object.
130
+ """
131
+ if self.proxy:
132
+ proxy_url = urlparse(self.proxy)
133
+ logger.trace("Using proxy: %s", self.proxy)
134
+ conn = http.client.HTTPSConnection(proxy_url.hostname, proxy_url.port)
135
+ conn.set_tunnel(netloc)
136
+ logger.trace("Using proxy tunnel to %s", netloc)
137
+ else:
138
+ conn = http.client.HTTPSConnection(netloc)
139
+ logger.trace("Direct connection to %s", netloc)
140
+ return conn
141
+
142
+ def _post_token_request(self, payload: Dict[str, str]) -> Optional[Dict[str, Any]]:
143
+ """
144
+ Send a POST request to the Salesforce token endpoint using http.client.
145
+
146
+ :param payload: Dictionary of form-encoded OAuth parameters.
147
+ :return: Parsed JSON response if successful, otherwise None.
148
+ """
149
+ parsed_url = urlparse(self.instance_url)
150
+ conn = self._create_connection(parsed_url.netloc)
151
+ headers = {
152
+ "Accept": "application/json",
153
+ "Content-Type": "application/x-www-form-urlencoded",
154
+ "User-Agent": self.user_agent,
155
+ }
156
+ body = "&".join(f"{key}={quote(str(value))}" for key, value in payload.items())
157
+
158
+ try:
159
+ logger.trace("Request endpoint: %s", self.token_endpoint)
160
+ logger.trace("Request body: %s", body)
161
+ logger.trace("Request headers: %s", headers)
162
+ conn.request("POST", self.token_endpoint, body, headers)
163
+ response = conn.getresponse()
164
+ data = response.read().decode("utf-8")
165
+ self._http_resp_header_logic(response)
166
+
167
+ if response.status == 200:
168
+ logger.trace("Token refresh successful.")
169
+ logger.trace("Response body: %s", data)
170
+ return json.loads(data)
171
+
172
+ logger.error(
173
+ "Token refresh failed: %s %s", response.status, response.reason
174
+ )
175
+ logger.debug("Response body: %s", data)
176
+
177
+ except Exception as err:
178
+ logger.exception("Error during token request: %s", err)
179
+
180
+ finally:
181
+ conn.close()
182
+
183
+ return None
184
+
185
+ def _http_resp_header_logic(self, response: http.client.HTTPResponse) -> None:
186
+ """
187
+ Perform additional logic based on the HTTP response headers.
188
+
189
+ :param response: The HTTP response object.
190
+ :return: None
191
+ """
192
+ logger.trace(
193
+ "Response status: %s, reason: %s", response.status, response.reason
194
+ )
195
+ headers = response.getheaders()
196
+ headers_list = [(k, v) for k, v in headers if not v.startswith("BrowserId=")]
197
+ logger.trace("Response headers: %s", headers_list)
198
+ for key, value in headers_list:
199
+ if key.startswith("Sforce-"):
200
+ if key == "Sforce-Limit-Info":
201
+ current_api_calls = int(value.split("=")[1].split("/")[0])
202
+ maximum_api_calls = int(value.split("=")[1].split("/")[1])
203
+ usage_percentage = round(
204
+ current_api_calls / maximum_api_calls * 100, 2
205
+ )
206
+ if usage_percentage > self._high_api_usage_threshold:
207
+ logger.warning(
208
+ "High API usage: %s/%s (%s%%)",
209
+ current_api_calls,
210
+ maximum_api_calls,
211
+ usage_percentage,
212
+ )
213
+ else:
214
+ logger.debug(
215
+ "API usage: %s/%s (%s%%)",
216
+ current_api_calls,
217
+ maximum_api_calls,
218
+ usage_percentage,
219
+ )
220
+
221
+ def _refresh_token_if_needed(self) -> Optional[str]:
222
+ """
223
+ Automatically refresh the access token if it has expired or is missing.
224
+
225
+ :return: A valid access token or None if refresh failed.
226
+ """
227
+ if self.access_token and not self._is_token_expired():
228
+ return self.access_token
229
+
230
+ logger.trace("Access token expired or missing, refreshing...")
231
+ payload = self._prepare_payload()
232
+ token_data = self._post_token_request(payload)
233
+
234
+ if token_data:
235
+ self.access_token = token_data.get("access_token")
236
+ issued_at = token_data.get("issued_at")
237
+
238
+ try:
239
+ self.org_id = token_data.get("id").split("/")[4]
240
+ self.user_id = token_data.get("id").split("/")[5]
241
+ logger.trace(
242
+ "Authenticated as user %s in org %s", self.user_id, self.org_id
243
+ )
244
+ except (IndexError, KeyError):
245
+ logger.error("Failed to extract org/user IDs from token response.")
246
+
247
+ if self.access_token and issued_at:
248
+ self.token_expiration_time = int(issued_at) + self.token_lifetime
249
+ logger.trace("New token expires at %s", self.token_expiration_time)
250
+ return self.access_token
251
+
252
+ logger.error("Failed to obtain access token.")
253
+ return None
254
+
255
+ def _is_token_expired(self) -> bool:
256
+ """
257
+ Check if the access token has expired.
258
+
259
+ :return: True if token is expired or missing, False otherwise.
260
+ """
261
+ try:
262
+ return time.time() >= float(self.token_expiration_time)
263
+ except (TypeError, ValueError):
264
+ logger.warning("Token expiration check failed. Treating token as expired.")
265
+ return True
266
+
267
+ def tooling_query(self, query: str) -> Optional[Dict[str, Any]]:
268
+ """
269
+ Execute a SOQL query using the Tooling API.
270
+
271
+ :param query: The SOQL query string.
272
+ :return: Parsed JSON response or None on failure.
273
+ """
274
+ return self.query(query, tooling=True)
275
+
276
+ def limits(self) -> Optional[Dict[str, Any]]:
277
+ self._refresh_token_if_needed()
278
+
279
+ if not self.access_token:
280
+ logger.error("No access token available for limits.")
281
+ return None
282
+
283
+ endpoint = f"/services/data/{self.api_version}/limits"
284
+ headers = {
285
+ "Authorization": f"Bearer {self.access_token}",
286
+ "User-Agent": self.user_agent,
287
+ "Content-Type": "application/x-www-form-urlencoded",
288
+ "Accept": "application/json",
289
+ }
290
+
291
+ parsed_url = urlparse(self.instance_url)
292
+ conn = self._create_connection(parsed_url.netloc)
293
+
294
+ try:
295
+ logger.trace("Request endpoint: %s", endpoint)
296
+ logger.trace("Request headers: %s", headers)
297
+ conn.request("GET", endpoint, headers=headers)
298
+ response = conn.getresponse()
299
+ data = response.read().decode("utf-8")
300
+ self._http_resp_header_logic(response)
301
+
302
+ if response.status == 200:
303
+ logger.debug("Limits API request successful.")
304
+ logger.trace("Response body: %s", data)
305
+ return json.loads(data)
306
+
307
+ logger.error("Limits API request failed: %s %s", response.status, response.reason)
308
+ logger.debug("Response body: %s", data)
309
+
310
+ except Exception as err:
311
+ logger.exception("Error during limits request: %s", err)
312
+
313
+ finally:
314
+ conn.close()
315
+
316
+ return None
317
+
318
+ def query(self, query: str, tooling: bool = False) -> Optional[Dict[str, Any]]:
319
+ """
320
+ Execute a SOQL query using the REST or Tooling API.
321
+
322
+ :param query: The SOQL query string.
323
+ :param tooling: If True, use the Tooling API endpoint.
324
+ :return: Parsed JSON response or None on failure.
325
+ """
326
+ self._refresh_token_if_needed()
327
+
328
+ if not self.access_token:
329
+ logger.error("No access token available for query.")
330
+ return None
331
+
332
+ endpoint = f"/services/data/{self.api_version}/"
333
+ endpoint += "tooling/query" if tooling else "query"
334
+ query_string = f"?q={quote(query)}"
335
+
336
+ endpoint += query_string
337
+
338
+ headers = {
339
+ "Authorization": f"Bearer {self.access_token}",
340
+ "User-Agent": self.user_agent,
341
+ "Content-Type": "application/x-www-form-urlencoded",
342
+ "Accept": "application/json",
343
+ }
344
+
345
+ parsed_url = urlparse(self.instance_url)
346
+ conn = self._create_connection(parsed_url.netloc)
347
+
348
+ try:
349
+ paginated_results = {"totalSize": 0, "done": False, "records": []}
350
+ while True:
351
+ logger.trace("Request endpoint: %s", endpoint)
352
+ logger.trace("Request headers: %s", headers)
353
+ conn.request("GET", endpoint, headers=headers)
354
+ response = conn.getresponse()
355
+ data = response.read().decode("utf-8")
356
+ self._http_resp_header_logic(response)
357
+
358
+ if response.status == 200:
359
+ current_results = json.loads(data)
360
+ paginated_results["records"].extend(current_results["records"])
361
+ query_done = current_results.get("done")
362
+ if query_done:
363
+ total_size = current_results.get("totalSize")
364
+ paginated_results = {
365
+ "totalSize": total_size,
366
+ "done": query_done,
367
+ "records": paginated_results["records"],
368
+ }
369
+ logger.debug(
370
+ "Query successful, returned %s records: %r",
371
+ total_size,
372
+ query,
373
+ )
374
+ logger.trace("Query full response: %s", data)
375
+ break
376
+ endpoint = current_results.get("nextRecordsUrl")
377
+ logger.debug(
378
+ "Query batch successful, getting next batch: %s", endpoint
379
+ )
380
+ else:
381
+ logger.debug("Query failed: %r", query)
382
+ logger.error(
383
+ "Query failed with HTTP status %s (%s)",
384
+ response.status,
385
+ response.reason,
386
+ )
387
+ logger.debug("Query response: %s", data)
388
+ break
389
+
390
+ return paginated_results
391
+
392
+ except Exception as err:
393
+ logger.exception("Exception during query: %s", err)
394
+
395
+ finally:
396
+ conn.close()
397
+
398
+ return None
sfq-0.0.9/uv.lock ADDED
@@ -0,0 +1,7 @@
1
+ version = 1
2
+ requires-python = ">=3.9"
3
+
4
+ [[package]]
5
+ name = "sfq"
6
+ version = "0.0.9"
7
+ source = { editable = "." }
sfq-0.0.7/.python-version DELETED
@@ -1 +0,0 @@
1
- 3.7
@@ -1,174 +0,0 @@
1
- import http.client
2
- import logging
3
- import time
4
- import os
5
- import json
6
- from urllib.parse import urlparse, quote
7
-
8
- logging.basicConfig(level=logging.INFO, format='[sfq:%(lineno)d - %(levelname)s] %(message)s')
9
-
10
- class SFAuth:
11
- def __init__(
12
- self,
13
- instance_url,
14
- client_id,
15
- refresh_token,
16
- api_version="v63.0",
17
- token_endpoint="/services/oauth2/token",
18
- access_token=None,
19
- token_expiration_time=None,
20
- token_lifetime=15 * 60,
21
- proxy="auto",
22
- user_agent="sfq/0.0.7",
23
- ):
24
- """
25
- Initializes the SFAuth with necessary parameters.
26
-
27
- :param instance_url: The Salesforce instance URL.
28
- :param client_id: The client ID for OAuth.
29
- :param refresh_token: The refresh token for OAuth.
30
- :param api_version: The Salesforce API version (default is "v62.0").
31
- :param token_endpoint: The token endpoint (default is "/services/oauth2/token").
32
- :param access_token: The access token for the current session (default is None).
33
- :param token_expiration_time: The expiration time of the access token (default is None).
34
- :param token_lifetime: The lifetime of the access token (default is 15 minutes).
35
- :param proxy: The proxy configuration (default is "auto").
36
- """
37
- self.instance_url = instance_url
38
- self.client_id = client_id
39
- self.refresh_token = refresh_token
40
- self.api_version = api_version
41
- self.token_endpoint = token_endpoint
42
- self.access_token = access_token
43
- self.token_expiration_time = token_expiration_time
44
- self.token_lifetime = token_lifetime
45
- self._auto_configure_proxy(proxy)
46
- self.user_agent = user_agent
47
-
48
- def _auto_configure_proxy(self, proxy):
49
- """
50
- Automatically configure the proxy based on the environment.
51
- """
52
- if proxy == "auto":
53
- if "https_proxy" in os.environ:
54
- self.proxy = os.environ["https_proxy"]
55
- else:
56
- self.proxy = None
57
- else:
58
- self.proxy = proxy
59
-
60
- def _prepare_payload(self):
61
- """Prepare the payload for the token request."""
62
- return {
63
- "grant_type": "refresh_token",
64
- "client_id": self.client_id,
65
- "refresh_token": self.refresh_token,
66
- }
67
-
68
- def _create_connection(self, netloc):
69
- """
70
- Create a connection using HTTP or HTTPS, depending on the proxy configuration.
71
- """
72
- if self.proxy:
73
- proxy_url = urlparse(self.proxy)
74
- if proxy_url.scheme == "http://":
75
- conn = http.client.HTTPConnection(proxy_url.hostname, proxy_url.port)
76
- else:
77
- conn = http.client.HTTPSConnection(proxy_url.hostname, proxy_url.port)
78
- conn.set_tunnel(netloc)
79
- else:
80
- conn = http.client.HTTPSConnection(netloc)
81
- return conn
82
-
83
- def _send_post_request(self, payload):
84
- """Send a POST request to the Salesforce token endpoint using http.client."""
85
- parsed_url = urlparse(self.instance_url)
86
- conn = self._create_connection(parsed_url.netloc)
87
-
88
- headers = {"Content-Type": "application/x-www-form-urlencoded", "User-Agent": self.user_agent}
89
- body = "&".join([f"{key}={quote(str(value))}" for key, value in payload.items()])
90
-
91
- try:
92
- conn.request("POST", self.token_endpoint, body, headers)
93
- response = conn.getresponse()
94
- data = response.read().decode("utf-8")
95
- if response.status == 200:
96
- return json.loads(data)
97
- else:
98
- logging.error(
99
- f"HTTP error occurred: {response.status} {response.reason}"
100
- )
101
- logging.error(f"Response content: {data}")
102
- except Exception as err:
103
- logging.error(f"Other error occurred: {err}")
104
- finally:
105
- conn.close()
106
-
107
- return None
108
-
109
- def _refresh_token_if_needed(self):
110
- """Automatically refresh the token if it has expired or is missing."""
111
- _token_expiration = self._is_token_expired()
112
- if self.access_token and not _token_expiration:
113
- return self.access_token
114
-
115
- if not self.access_token:
116
- logging.debug("No access token available. Requesting a new one.")
117
- elif _token_expiration:
118
- logging.debug("Access token has expired. Requesting a new one.")
119
-
120
- payload = self._prepare_payload()
121
- token_data = self._send_post_request(payload)
122
- if token_data:
123
- self.access_token = token_data["access_token"]
124
- self.token_expiration_time = int(token_data["issued_at"]) + int(self.token_lifetime)
125
- logging.debug("Access token refreshed successfully.")
126
- else:
127
- logging.error("Failed to refresh access token.")
128
- return self.access_token
129
-
130
-
131
- def _is_token_expired(self):
132
- """Check if the access token has expired."""
133
- try:
134
- return time.time() >= float(self.token_expiration_time)
135
- except (TypeError, ValueError):
136
- return True
137
-
138
- def query(self, query, tooling=False):
139
- """Query Salesforce using SOQL or Tooling API, depending on the `tooling` parameter."""
140
- self._refresh_token_if_needed()
141
-
142
- if not self.access_token:
143
- logging.error("No access token available to make the query.")
144
- return None
145
-
146
- if tooling:
147
- query_endpoint = f"/services/data/{self.api_version}/tooling/query"
148
- else:
149
- query_endpoint = f"/services/data/{self.api_version}/query"
150
-
151
- headers = {"Authorization": f"Bearer {self.access_token}", "User-Agent": self.user_agent}
152
-
153
- # Handle special characters in the query
154
- encoded_query = quote(query)
155
- params = f"?q={encoded_query}"
156
-
157
- parsed_url = urlparse(self.instance_url)
158
- conn = self._create_connection(parsed_url.netloc)
159
-
160
- try:
161
- conn.request("GET", query_endpoint + params, headers=headers)
162
- response = conn.getresponse()
163
- data = response.read().decode("utf-8")
164
- if response.status == 200:
165
- return json.loads(data)
166
- else:
167
- logging.error(f"HTTP error occurred during query: {response.status} {response.reason}")
168
- logging.error(f"Response content: {data}")
169
- except Exception as err:
170
- logging.error(f"Other error occurred during query: {err}")
171
- finally:
172
- conn.close()
173
-
174
- return None
@@ -1,151 +0,0 @@
1
- import difflib
2
- import http.client
3
- import json
4
-
5
- from sfq import SFAuth
6
-
7
- from prompt_toolkit import prompt
8
- from prompt_toolkit.completion import Completer, Completion
9
-
10
- def _interactive_shell(sf: SFAuth, dry_run: bool, disable_fuzzy_completion: bool):
11
- """Runs an interactive REPL for querying Salesforce data with real-time autocompletion."""
12
-
13
- sobject = None
14
- fields = None
15
-
16
- class DynamicSeparatorCompleter(Completer):
17
- """Custom completer that adapts to different separators."""
18
-
19
- def __init__(self, words: list[str], separators: list[str] = [","]):
20
- self.words = words
21
- self.separators = separators
22
-
23
- def get_completions(self, document, complete_event):
24
- text_before_cursor = document.text_before_cursor
25
-
26
- for separator in self.separators:
27
- if separator in text_before_cursor:
28
- last_token = text_before_cursor.split(separator)[-1].strip()
29
- break
30
- else:
31
- last_token = text_before_cursor.strip()
32
-
33
-
34
- matches_difflib = difflib.get_close_matches(last_token, self.words, n=20, cutoff=0.6)
35
- matches_starting = [word for word in self.words if word.lower().startswith(last_token.lower())]
36
-
37
- if not disable_fuzzy_completion and (len(last_token) > 6 or not(matches_starting)):
38
- matches = matches_difflib
39
- else:
40
- matches = matches_starting
41
-
42
- for word in matches:
43
- yield Completion(word, start_position=-len(last_token))
44
-
45
- def _get_objects(sf: SFAuth):
46
- """Retrieve available Salesforce objects."""
47
- host = sf.instance_url.split("://")[1].split("/")[0]
48
- conn = http.client.HTTPSConnection(host)
49
- uri = f"/services/data/{sf.api_version}/sobjects/"
50
- headers = {'Authorization': f'Bearer {sf._refresh_token_if_needed()}'}
51
- conn.request("GET", uri, headers=headers)
52
- response = conn.getresponse()
53
-
54
- if response.status != 200:
55
- print(f'Error: {response.status} {response.reason}')
56
- return []
57
-
58
- data = json.loads(response.read())
59
- return [sobject['name'] for sobject in data['sobjects']]
60
-
61
- def _get_fields(sobject: str, sf: SFAuth):
62
- """Retrieve available fields for a given Salesforce object."""
63
- host = sf.instance_url.split("://")[1].split("/")[0]
64
- conn = http.client.HTTPSConnection(host)
65
- uri = f"/services/data/{sf.api_version}/sobjects/{sobject}/describe/"
66
- headers = {'Authorization': f'Bearer {sf._refresh_token_if_needed()}'}
67
- conn.request("GET", uri, headers=headers)
68
- response = conn.getresponse()
69
-
70
- if response.status != 200:
71
- print(f'Error: {response.status} {response.reason}')
72
- raise ValueError(f'Unable to fetch fields for sObject "{sobject}": {response.status}, {response.reason}')
73
-
74
- data = json.loads(response.read())
75
- return [f['name'] for f in data['fields']]
76
-
77
- available_objects = _get_objects(sf)
78
-
79
- object_completer = DynamicSeparatorCompleter(available_objects)
80
- while not sobject:
81
- sobject = prompt('FROM ', completer=object_completer).strip()
82
-
83
-
84
- available_fields = _get_fields(sobject, sf)
85
- field_completer = DynamicSeparatorCompleter(available_fields, separators=[","])
86
- while not fields:
87
- fields = prompt('SELECT ', completer=field_completer).strip()
88
-
89
- where_completer = DynamicSeparatorCompleter(available_fields, separators=[" AND ", " OR "])
90
- where = prompt("WHERE ", completer=where_completer).strip()
91
- where_clause = f"WHERE {where}" if where else ""
92
-
93
- limit = prompt("LIMIT ", default="200").strip()
94
- limit_clause = f"LIMIT {limit}" if limit else ""
95
-
96
- query = f"SELECT {fields} FROM {sobject} {where_clause} {limit_clause}".replace(' ', ' ')
97
-
98
- if dry_run:
99
- print('\nDry-run, skipping execution...')
100
- print(f'\nQuery: {query}\n')
101
- return query
102
-
103
- print('\nExecuting query...\n')
104
- data = sf.query(query)
105
- print(json.dumps(data, indent=4))
106
- print(f'\nQuery: {query}\n')
107
- return data
108
-
109
- if __name__ == "__main__":
110
- import argparse
111
- import os
112
-
113
- parser = argparse.ArgumentParser(
114
- description='Interactively query Salesforce data with real-time autocompletion.'
115
- )
116
- parser.add_argument(
117
- '-a', '--sfdxAuthUrl', type=str, help='Salesforce auth url', default=os.environ.get('SFDX_AUTH_URL')
118
- )
119
- parser.add_argument(
120
- '--dry-run', action='store_true', help='Print the query without executing it', default=str(os.environ.get('SFQ_DRY_RUN')),
121
- )
122
- parser.add_argument(
123
- '--disable-fuzzy-completion', action='store_true', help='Disable fuzzy completion', default=str(os.environ.get('SFQ_DISABLE_FUZZY_COMPLETION')),
124
- )
125
- args = parser.parse_args()
126
-
127
- if not args.sfdxAuthUrl:
128
- raise ValueError('SFDX_AUTH_URL environment variable is not set nor provided as an argument')
129
-
130
- try:
131
- if args.dry_run.lower() not in ['true', '1']:
132
- args.dry_run = False
133
- except AttributeError:
134
- pass
135
-
136
- try:
137
- if args.disable_fuzzy_completion.lower() not in ['true', '1']:
138
- args.disable_fuzzy_completion = False
139
- except AttributeError:
140
- pass
141
-
142
-
143
- _interactive_shell(
144
- SFAuth(
145
- instance_url=f"https://{str(args.sfdxAuthUrl).split('@')[1]}",
146
- client_id=str(args.sfdxAuthUrl).split('//')[1].split('::')[0],
147
- refresh_token=str(args.sfdxAuthUrl).split('::')[1].split('@')[0],
148
- ),
149
- args.dry_run,
150
- args.disable_fuzzy_completion
151
- )
sfq-0.0.7/uv.lock DELETED
@@ -1,35 +0,0 @@
1
- version = 1
2
- revision = 1
3
- requires-python = ">=3.7"
4
-
5
- [[package]]
6
- name = "prompt-toolkit"
7
- version = "3.0.3"
8
- source = { registry = "https://pypi.org/simple" }
9
- dependencies = [
10
- { name = "wcwidth" },
11
- ]
12
- sdist = { url = "https://files.pythonhosted.org/packages/8f/bc/58ba47a2a864d8e3d968d03b577c85fbdf52c8d324a030df71ac9c06c1b5/prompt_toolkit-3.0.3.tar.gz", hash = "sha256:a402e9bf468b63314e37460b68ba68243d55b2f8c4d0192f85a019af3945050e", size = 2997855 }
13
- wheels = [
14
- { url = "https://files.pythonhosted.org/packages/f5/22/f00412fafc68169054cc623a35c32773f22b403ddbe516c8adfdecf25341/prompt_toolkit-3.0.3-py3-none-any.whl", hash = "sha256:c93e53af97f630f12f5f62a3274e79527936ed466f038953dfa379d4941f651a", size = 348445 },
15
- ]
16
-
17
- [[package]]
18
- name = "sfq"
19
- version = "0.0.7"
20
- source = { editable = "." }
21
- dependencies = [
22
- { name = "prompt-toolkit" },
23
- ]
24
-
25
- [package.metadata]
26
- requires-dist = [{ name = "prompt-toolkit", specifier = ">=3.0.3" }]
27
-
28
- [[package]]
29
- name = "wcwidth"
30
- version = "0.2.13"
31
- source = { registry = "https://pypi.org/simple" }
32
- sdist = { url = "https://files.pythonhosted.org/packages/6c/63/53559446a878410fc5a5974feb13d31d78d752eb18aeba59c7fef1af7598/wcwidth-0.2.13.tar.gz", hash = "sha256:72ea0c06399eb286d978fdedb6923a9eb47e1c486ce63e9b4e64fc18303972b5", size = 101301 }
33
- wheels = [
34
- { url = "https://files.pythonhosted.org/packages/fd/84/fd2ba7aafacbad3c4201d395674fc6348826569da3c0937e75505ead3528/wcwidth-0.2.13-py2.py3-none-any.whl", hash = "sha256:3da69048e4540d84af32131829ff948f1e022c1c6bdb8d6102117aac784f6859", size = 34166 },
35
- ]
File without changes
File without changes
File without changes