sfq 0.0.12__py3-none-any.whl → 0.0.13__py3-none-any.whl
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.
- sfq/__init__.py +183 -29
- sfq-0.0.13.dist-info/METADATA +179 -0
- sfq-0.0.13.dist-info/RECORD +5 -0
- sfq-0.0.12.dist-info/METADATA +0 -133
- sfq-0.0.12.dist-info/RECORD +0 -5
- {sfq-0.0.12.dist-info → sfq-0.0.13.dist-info}/WHEEL +0 -0
sfq/__init__.py
CHANGED
@@ -8,7 +8,7 @@ import warnings
|
|
8
8
|
from collections import OrderedDict
|
9
9
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
10
10
|
from queue import Empty, Queue
|
11
|
-
from typing import Any, Dict, Optional
|
11
|
+
from typing import Any, Dict, Literal, Optional
|
12
12
|
from urllib.parse import quote, urlparse
|
13
13
|
|
14
14
|
TRACE = 5
|
@@ -81,13 +81,14 @@ class SFAuth:
|
|
81
81
|
self,
|
82
82
|
instance_url: str,
|
83
83
|
client_id: str,
|
84
|
-
refresh_token: str,
|
84
|
+
refresh_token: str, # client_secret & refresh_token will swap positions 2025-AUG-1
|
85
|
+
client_secret: str = "_deprecation_warning", # mandatory after 2025-AUG-1
|
85
86
|
api_version: str = "v63.0",
|
86
87
|
token_endpoint: str = "/services/oauth2/token",
|
87
88
|
access_token: Optional[str] = None,
|
88
89
|
token_expiration_time: Optional[float] = None,
|
89
90
|
token_lifetime: int = 15 * 60,
|
90
|
-
user_agent: str = "sfq/0.0.
|
91
|
+
user_agent: str = "sfq/0.0.13",
|
91
92
|
proxy: str = "auto",
|
92
93
|
) -> None:
|
93
94
|
"""
|
@@ -96,16 +97,18 @@ class SFAuth:
|
|
96
97
|
:param instance_url: The Salesforce instance URL.
|
97
98
|
:param client_id: The client ID for OAuth.
|
98
99
|
:param refresh_token: The refresh token for OAuth.
|
100
|
+
:param client_secret: The client secret for OAuth (default is "_deprecation_warning").
|
99
101
|
:param api_version: The Salesforce API version (default is "v63.0").
|
100
102
|
:param token_endpoint: The token endpoint (default is "/services/oauth2/token").
|
101
103
|
:param access_token: The access token for the current session (default is None).
|
102
104
|
:param token_expiration_time: The expiration time of the access token (default is None).
|
103
105
|
:param token_lifetime: The lifetime of the access token in seconds (default is 15 minutes).
|
104
|
-
:param user_agent: Custom User-Agent string (default is "sfq/0.0.
|
106
|
+
:param user_agent: Custom User-Agent string (default is "sfq/0.0.13").
|
105
107
|
:param proxy: The proxy configuration, "auto" to use environment (default is "auto").
|
106
108
|
"""
|
107
|
-
self.instance_url = instance_url
|
109
|
+
self.instance_url = self._format_instance_url(instance_url)
|
108
110
|
self.client_id = client_id
|
111
|
+
self.client_secret = client_secret
|
109
112
|
self.refresh_token = refresh_token
|
110
113
|
self.api_version = api_version
|
111
114
|
self.token_endpoint = token_endpoint
|
@@ -116,6 +119,26 @@ class SFAuth:
|
|
116
119
|
self._auto_configure_proxy(proxy)
|
117
120
|
self._high_api_usage_threshold = 80
|
118
121
|
|
122
|
+
if self.client_secret == "_deprecation_warning":
|
123
|
+
warnings.warn(
|
124
|
+
"The 'client_secret' parameter will be mandatory and positional arguments will change after 1 August 2025. "
|
125
|
+
"Please ensure explicit argument assignment and 'client_secret' inclusion when initializing the SFAuth object.",
|
126
|
+
DeprecationWarning,
|
127
|
+
stacklevel=2,
|
128
|
+
)
|
129
|
+
|
130
|
+
logger.debug(
|
131
|
+
"Will be SFAuth(instance_url, client_id, client_secret, refresh_token) starting 1 August 2025... but please just use named arguments.."
|
132
|
+
)
|
133
|
+
|
134
|
+
def _format_instance_url(self, instance_url) -> str:
|
135
|
+
# check if it begins with https://
|
136
|
+
if instance_url.startswith("https://"):
|
137
|
+
return instance_url
|
138
|
+
if instance_url.startswith("http://"):
|
139
|
+
return instance_url.replace("http://", "https://")
|
140
|
+
return f"https://{instance_url}"
|
141
|
+
|
119
142
|
def _auto_configure_proxy(self, proxy: str) -> None:
|
120
143
|
"""
|
121
144
|
Automatically configure the proxy based on the environment or provided value.
|
@@ -128,16 +151,36 @@ class SFAuth:
|
|
128
151
|
self.proxy = proxy
|
129
152
|
logger.debug("Using configured proxy: %s", self.proxy)
|
130
153
|
|
131
|
-
def _prepare_payload(self) -> Dict[str, str]:
|
154
|
+
def _prepare_payload(self) -> Dict[str, Optional[str]]:
|
132
155
|
"""
|
133
156
|
Prepare the payload for the token request.
|
157
|
+
|
158
|
+
This method constructs a dictionary containing the necessary parameters
|
159
|
+
for a token request using the refresh token grant type. It includes
|
160
|
+
the client ID, client secret, and refresh token if they are available.
|
161
|
+
|
162
|
+
Returns:
|
163
|
+
Dict[str, Optional[str]]: A dictionary containing the payload for the token request.
|
134
164
|
"""
|
135
|
-
|
165
|
+
payload = {
|
136
166
|
"grant_type": "refresh_token",
|
137
167
|
"client_id": self.client_id,
|
168
|
+
"client_secret": self.client_secret,
|
138
169
|
"refresh_token": self.refresh_token,
|
139
170
|
}
|
140
171
|
|
172
|
+
if self.client_secret == "_deprecation_warning":
|
173
|
+
logger.warning(
|
174
|
+
"The SFQ library is making a breaking change (2025-AUG-1) to require the 'client_secret' parameter to be assigned when initializing the SFAuth object. "
|
175
|
+
"In addition, positional arguments will change. Please ensure explicit argument assignment and 'client_secret' inclusion when initializing the SFAuth object to avoid impact."
|
176
|
+
)
|
177
|
+
payload.pop("client_secret")
|
178
|
+
|
179
|
+
if not self.client_secret:
|
180
|
+
payload.pop("client_secret")
|
181
|
+
|
182
|
+
return payload
|
183
|
+
|
141
184
|
def _create_connection(self, netloc: str) -> http.client.HTTPConnection:
|
142
185
|
"""
|
143
186
|
Create a connection using HTTP or HTTPS, with optional proxy support.
|
@@ -214,27 +257,24 @@ class SFAuth:
|
|
214
257
|
headers_list = [(k, v) for k, v in headers if not v.startswith("BrowserId=")]
|
215
258
|
logger.trace("Response headers: %s", headers_list)
|
216
259
|
for key, value in headers_list:
|
217
|
-
if key
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
260
|
+
if key == "Sforce-Limit-Info":
|
261
|
+
current_api_calls = int(value.split("=")[1].split("/")[0])
|
262
|
+
maximum_api_calls = int(value.split("=")[1].split("/")[1])
|
263
|
+
usage_percentage = round(current_api_calls / maximum_api_calls * 100, 2)
|
264
|
+
if usage_percentage > self._high_api_usage_threshold:
|
265
|
+
logger.warning(
|
266
|
+
"High API usage: %s/%s (%s%%)",
|
267
|
+
current_api_calls,
|
268
|
+
maximum_api_calls,
|
269
|
+
usage_percentage,
|
270
|
+
)
|
271
|
+
else:
|
272
|
+
logger.debug(
|
273
|
+
"API usage: %s/%s (%s%%)",
|
274
|
+
current_api_calls,
|
275
|
+
maximum_api_calls,
|
276
|
+
usage_percentage,
|
223
277
|
)
|
224
|
-
if usage_percentage > self._high_api_usage_threshold:
|
225
|
-
logger.warning(
|
226
|
-
"High API usage: %s/%s (%s%%)",
|
227
|
-
current_api_calls,
|
228
|
-
maximum_api_calls,
|
229
|
-
usage_percentage,
|
230
|
-
)
|
231
|
-
else:
|
232
|
-
logger.debug(
|
233
|
-
"API usage: %s/%s (%s%%)",
|
234
|
-
current_api_calls,
|
235
|
-
maximum_api_calls,
|
236
|
-
usage_percentage,
|
237
|
-
)
|
238
278
|
|
239
279
|
def _refresh_token_if_needed(self) -> Optional[str]:
|
240
280
|
"""
|
@@ -298,6 +338,7 @@ class SFAuth:
|
|
298
338
|
_safe_resource_name = quote(resource_name, safe="")
|
299
339
|
query = f"SELECT Id FROM StaticResource WHERE Name = '{_safe_resource_name}'"
|
300
340
|
if namespace:
|
341
|
+
namespace = quote(namespace, safe="")
|
301
342
|
query += f" AND NamespacePrefix = '{namespace}'"
|
302
343
|
query += " LIMIT 1"
|
303
344
|
_static_resource_id_response = self.query(query)
|
@@ -382,6 +423,7 @@ class SFAuth:
|
|
382
423
|
safe_resource_name = quote(resource_name, safe="")
|
383
424
|
query = f"SELECT Id FROM StaticResource WHERE Name = '{safe_resource_name}'"
|
384
425
|
if namespace:
|
426
|
+
namespace = quote(namespace, safe="")
|
385
427
|
query += f" AND NamespacePrefix = '{namespace}'"
|
386
428
|
query += " LIMIT 1"
|
387
429
|
|
@@ -607,7 +649,85 @@ class SFAuth:
|
|
607
649
|
"""
|
608
650
|
return self.query(query, tooling=True)
|
609
651
|
|
610
|
-
def
|
652
|
+
def get_sobject_prefixes(
|
653
|
+
self, key_type: Literal["id", "name"] = "id"
|
654
|
+
) -> Optional[Dict[str, str]]:
|
655
|
+
"""
|
656
|
+
Fetch all key prefixes from the Salesforce instance and map them to sObject names or vice versa.
|
657
|
+
|
658
|
+
:param key_type: The type of key to return. Either 'id' (prefix) or 'name' (sObject).
|
659
|
+
:return: A dictionary mapping key prefixes to sObject names or None on failure.
|
660
|
+
"""
|
661
|
+
valid_key_types = {"id", "name"}
|
662
|
+
if key_type not in valid_key_types:
|
663
|
+
logger.error(
|
664
|
+
"Invalid key type: %s, must be one of: %s",
|
665
|
+
key_type,
|
666
|
+
", ".join(valid_key_types),
|
667
|
+
)
|
668
|
+
return None
|
669
|
+
|
670
|
+
self._refresh_token_if_needed()
|
671
|
+
|
672
|
+
if not self.access_token:
|
673
|
+
logger.error("No access token available for key prefixes.")
|
674
|
+
return None
|
675
|
+
|
676
|
+
endpoint = f"/services/data/{self.api_version}/sobjects/"
|
677
|
+
headers = {
|
678
|
+
"Authorization": f"Bearer {self.access_token}",
|
679
|
+
"User-Agent": self.user_agent,
|
680
|
+
"Accept": "application/json",
|
681
|
+
}
|
682
|
+
|
683
|
+
parsed_url = urlparse(self.instance_url)
|
684
|
+
conn = self._create_connection(parsed_url.netloc)
|
685
|
+
prefixes = {}
|
686
|
+
|
687
|
+
try:
|
688
|
+
logger.trace("Request endpoint: %s", endpoint)
|
689
|
+
logger.trace("Request headers: %s", headers)
|
690
|
+
conn.request("GET", endpoint, headers=headers)
|
691
|
+
response = conn.getresponse()
|
692
|
+
data = response.read().decode("utf-8")
|
693
|
+
self._http_resp_header_logic(response)
|
694
|
+
|
695
|
+
if response.status == 200:
|
696
|
+
logger.debug("Key prefixes API request successful.")
|
697
|
+
logger.trace("Response body: %s", data)
|
698
|
+
for sobject in json.loads(data)["sobjects"]:
|
699
|
+
key_prefix = sobject.get("keyPrefix")
|
700
|
+
name = sobject.get("name")
|
701
|
+
if not key_prefix or not name:
|
702
|
+
continue
|
703
|
+
|
704
|
+
if key_type == "id":
|
705
|
+
prefixes[key_prefix] = name
|
706
|
+
elif key_type == "name":
|
707
|
+
prefixes[name] = key_prefix
|
708
|
+
|
709
|
+
logger.debug("Key prefixes: %s", prefixes)
|
710
|
+
return prefixes
|
711
|
+
|
712
|
+
logger.error(
|
713
|
+
"Key prefixes API request failed: %s %s",
|
714
|
+
response.status,
|
715
|
+
response.reason,
|
716
|
+
)
|
717
|
+
logger.debug("Response body: %s", data)
|
718
|
+
|
719
|
+
except Exception as err:
|
720
|
+
logger.exception("Exception during key prefixes API request: %s", err)
|
721
|
+
|
722
|
+
finally:
|
723
|
+
logger.trace("Closing connection...")
|
724
|
+
conn.close()
|
725
|
+
|
726
|
+
return None
|
727
|
+
|
728
|
+
def cquery(
|
729
|
+
self, query_dict: dict[str, str], max_workers: int = 10
|
730
|
+
) -> Optional[Dict[str, Any]]:
|
611
731
|
"""
|
612
732
|
Execute multiple SOQL queries using the Composite Batch API with threading to reduce network overhead.
|
613
733
|
The function returns a dictionary mapping the original keys to their corresponding batch response.
|
@@ -668,7 +788,34 @@ class SFAuth:
|
|
668
788
|
logger.trace("Composite query full response: %s", data)
|
669
789
|
results = json.loads(data).get("results", [])
|
670
790
|
for i, result in enumerate(results):
|
671
|
-
|
791
|
+
records = []
|
792
|
+
if "result" in result and "records" in result["result"]:
|
793
|
+
records.extend(result["result"]["records"])
|
794
|
+
# Handle pagination
|
795
|
+
while not result["result"].get("done", True):
|
796
|
+
next_url = result["result"].get("nextRecordsUrl")
|
797
|
+
if next_url:
|
798
|
+
conn.request("GET", next_url, headers=headers)
|
799
|
+
response = conn.getresponse()
|
800
|
+
data = response.read().decode("utf-8")
|
801
|
+
self._http_resp_header_logic(response)
|
802
|
+
if response.status == 200:
|
803
|
+
next_results = json.loads(data)
|
804
|
+
records.extend(next_results.get("records", []))
|
805
|
+
result["result"]["done"] = next_results.get("done")
|
806
|
+
else:
|
807
|
+
logger.error(
|
808
|
+
"Failed to fetch next records: %s",
|
809
|
+
response.reason,
|
810
|
+
)
|
811
|
+
break
|
812
|
+
else:
|
813
|
+
result["result"]["done"] = True
|
814
|
+
paginated_results = result["result"]
|
815
|
+
paginated_results["records"] = records
|
816
|
+
if "nextRecordsUrl" in paginated_results:
|
817
|
+
del paginated_results["nextRecordsUrl"]
|
818
|
+
batch_results[keys[i]] = paginated_results
|
672
819
|
if result.get("statusCode") != 200:
|
673
820
|
logger.error("Query failed for key %s: %s", keys[i], result)
|
674
821
|
logger.error(
|
@@ -709,6 +856,13 @@ class SFAuth:
|
|
709
856
|
logger.trace("Composite query results: %s", results_dict)
|
710
857
|
return results_dict
|
711
858
|
|
859
|
+
def _reconnect_with_backoff(self, attempt: int) -> None:
|
860
|
+
wait_time = min(2**attempt, 60)
|
861
|
+
logger.warning(
|
862
|
+
f"Reconnecting after failure, backoff {wait_time}s (attempt {attempt})"
|
863
|
+
)
|
864
|
+
time.sleep(wait_time)
|
865
|
+
|
712
866
|
def _subscribe_topic(
|
713
867
|
self,
|
714
868
|
topic: str,
|
@@ -0,0 +1,179 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: sfq
|
3
|
+
Version: 0.0.13
|
4
|
+
Summary: Python wrapper for the Salesforce's Query API.
|
5
|
+
Author-email: David Moruzzi <sfq.pypi@dmoruzi.com>
|
6
|
+
Keywords: salesforce,salesforce query
|
7
|
+
Classifier: Development Status :: 3 - Alpha
|
8
|
+
Classifier: Intended Audience :: Developers
|
9
|
+
Classifier: Programming Language :: Python :: 3.9
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
13
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
14
|
+
Requires-Python: >=3.9
|
15
|
+
Description-Content-Type: text/markdown
|
16
|
+
|
17
|
+
# sfq (Salesforce Query)
|
18
|
+
|
19
|
+
sfq is a lightweight Python wrapper library designed to simplify querying Salesforce, reducing repetitive code for accessing Salesforce data.
|
20
|
+
|
21
|
+
For more varied workflows, consider using an alternative like [Simple Salesforce](https://simple-salesforce.readthedocs.io/en/stable/). This library was even referenced on the [Salesforce Developers Blog](https://developer.salesforce.com/blogs/2021/09/how-to-automate-data-extraction-from-salesforce-using-python).
|
22
|
+
|
23
|
+
## Features
|
24
|
+
|
25
|
+
- Simplified query execution for Salesforce instances.
|
26
|
+
- Integration with Salesforce authentication via refresh tokens.
|
27
|
+
- Option to interact with Salesforce Tooling API for more advanced queries.
|
28
|
+
|
29
|
+
## Installation
|
30
|
+
|
31
|
+
You can install the `sfq` library using `pip`:
|
32
|
+
|
33
|
+
```bash
|
34
|
+
pip install sfq
|
35
|
+
```
|
36
|
+
|
37
|
+
## Usage
|
38
|
+
|
39
|
+
### Library Querying
|
40
|
+
|
41
|
+
```python
|
42
|
+
from sfq import SFAuth
|
43
|
+
|
44
|
+
# Initialize the SFAuth class with authentication details
|
45
|
+
sf = SFAuth(
|
46
|
+
instance_url="https://example-dev-ed.trailblaze.my.salesforce.com",
|
47
|
+
client_id="your-client-id-here",
|
48
|
+
client_secret="your-client-secret-here",
|
49
|
+
refresh_token="your-refresh-token-here"
|
50
|
+
)
|
51
|
+
|
52
|
+
# Execute a query to fetch account records
|
53
|
+
print(sf.query("SELECT Id FROM Account LIMIT 5"))
|
54
|
+
|
55
|
+
# Execute a query to fetch Tooling API data
|
56
|
+
print(sf.tooling_query("SELECT Id, FullName, Metadata FROM SandboxSettings LIMIT 5"))
|
57
|
+
```
|
58
|
+
|
59
|
+
### sObject Key Prefixes
|
60
|
+
|
61
|
+
```python
|
62
|
+
# Key prefix via IDs
|
63
|
+
print(sf.get_sobject_prefixes())
|
64
|
+
>>> {'0Pp': 'AIApplication', '6S9': 'AIApplicationConfig', '9qd': 'AIInsightAction', '9bq': 'AIInsightFeedback', '0T2': 'AIInsightReason', '9qc': 'AIInsightValue', ...}
|
65
|
+
|
66
|
+
# Key prefix via names
|
67
|
+
print(sf.get_sobject_prefixes(key_type="name"))
|
68
|
+
>>> {'AIApplication': '0Pp', 'AIApplicationConfig': '6S9', 'AIInsightAction': '9qd', 'AIInsightFeedback': '9bq', 'AIInsightReason': '0T2', 'AIInsightValue': '9qc', ...}
|
69
|
+
```
|
70
|
+
|
71
|
+
### Composite Batch Queries
|
72
|
+
|
73
|
+
```python
|
74
|
+
multiple_queries = {
|
75
|
+
"Recent Users": """
|
76
|
+
SELECT Id, Name,CreatedDate
|
77
|
+
FROM User
|
78
|
+
ORDER BY CreatedDate DESC
|
79
|
+
LIMIT 10""",
|
80
|
+
"Recent Accounts": "SELECT Id, Name, CreatedDate FROM Account ORDER BY CreatedDate DESC LIMIT 10",
|
81
|
+
"Frozen Users": "SELECT Id, UserId FROM UserLogin WHERE IsFrozen = true", # If exceeds 2000 records, will paginate
|
82
|
+
}
|
83
|
+
|
84
|
+
batched_response = sf.cquery(multiple_queries)
|
85
|
+
|
86
|
+
for subrequest_identifer, subrequest_response in batched_response.items():
|
87
|
+
print(f'"{subrequest_identifer}" returned {subrequest_response["totalSize"]} records')
|
88
|
+
>>> "Recent Users" returned 10 records
|
89
|
+
>>> "Recent Accounts" returned 10 records
|
90
|
+
>>> "Frozen Users" returned 4082 records
|
91
|
+
```
|
92
|
+
|
93
|
+
### Static Resources
|
94
|
+
|
95
|
+
```python
|
96
|
+
page = sf.read_static_resource_id('081aj000009jUMXAA2')
|
97
|
+
print(f'Initial resource: {page}')
|
98
|
+
>>> Initial resource: <h1>It works!</h1>
|
99
|
+
sf.update_static_resource_name('HelloWorld', '<h1>Hello World</h1>')
|
100
|
+
page = sf.read_static_resource_name('HelloWorld')
|
101
|
+
print(f'Updated resource: {page}')
|
102
|
+
>>> Updated resource: <h1>Hello World</h1>
|
103
|
+
sf.update_static_resource_id('081aj000009jUMXAA2', '<h1>It works!</h1>')
|
104
|
+
```
|
105
|
+
|
106
|
+
## How to Obtain Salesforce Tokens
|
107
|
+
|
108
|
+
To use the `sfq` library, you'll need a **client ID** and **refresh token**. The easiest way to obtain these is by using the Salesforce CLI:
|
109
|
+
|
110
|
+
### Steps to Get Tokens
|
111
|
+
|
112
|
+
1. **Install the Salesforce CLI**:
|
113
|
+
Follow the instructions on the [Salesforce CLI installation page](https://developer.salesforce.com/tools/salesforcecli).
|
114
|
+
|
115
|
+
2. **Authenticate with Salesforce**:
|
116
|
+
Login to your Salesforce org using the following command:
|
117
|
+
|
118
|
+
```bash
|
119
|
+
sf org login web --alias int --instance-url https://corpa--int.sandbox.my.salesforce.com
|
120
|
+
```
|
121
|
+
|
122
|
+
3. **Display Org Details**:
|
123
|
+
To get the client ID, client secret, refresh token, and instance URL, run:
|
124
|
+
|
125
|
+
```bash
|
126
|
+
sf org display --target-org int --verbose --json
|
127
|
+
```
|
128
|
+
|
129
|
+
The output will look like this:
|
130
|
+
|
131
|
+
```json
|
132
|
+
{
|
133
|
+
"status": 0,
|
134
|
+
"result": {
|
135
|
+
"id": "00Daa0000000000000",
|
136
|
+
"apiVersion": "63.0",
|
137
|
+
"accessToken": "00Daa0000000000000!evaU3fYZEWGUrqI5rMtaS8KYbHfeqA7YWzMgKToOB43Jk0kj7LtiWCbJaj4owPFQ7CqpXPAGX1RDCHblfW9t8cNOCNRFng3o",
|
138
|
+
"instanceUrl": "https://example-dev-ed.trailblaze.my.salesforce.com",
|
139
|
+
"username": "user@example.com",
|
140
|
+
"clientId": "PlatformCLI",
|
141
|
+
"connectedStatus": "Connected",
|
142
|
+
"sfdxAuthUrl": "force://PlatformCLI::nwAeSuiRqvRHrkbMmCKvLHasS0vRbh3Cf2RF41WZzmjtThnCwOuDvn9HObcUjKuTExJPqPegIwnLB5aH6GNWYhU@example-dev-ed.trailblaze.my.salesforce.com",
|
143
|
+
"alias": "int"
|
144
|
+
}
|
145
|
+
}
|
146
|
+
```
|
147
|
+
|
148
|
+
4. **Extract and Use the Tokens**:
|
149
|
+
The `sfdxAuthUrl` is structured as:
|
150
|
+
|
151
|
+
```
|
152
|
+
force://<client_id>:<client_secret>:<refresh_token>@<instance_url>
|
153
|
+
```
|
154
|
+
|
155
|
+
This means with the above output sample, you would use the following information:
|
156
|
+
|
157
|
+
```python
|
158
|
+
# This is for illustrative purposes; use environment variables instead
|
159
|
+
client_id = "PlatformCLI"
|
160
|
+
client_secret = ""
|
161
|
+
refresh_token = "nwAeSuiRqvRHrkbMmCKvLHasS0vRbh3Cf2RF41WZzmjtThnCwOuDvn9HObcUjKuTExJPqPegIwnLB5aH6GNWYhU"
|
162
|
+
instance_url = "https://example-dev-ed.trailblaze.my.salesforce.com"
|
163
|
+
|
164
|
+
from sfq import SFAuth
|
165
|
+
sf = SFAuth(
|
166
|
+
instance_url=instance_url,
|
167
|
+
client_id=client_id,
|
168
|
+
client_secret=client_secret,
|
169
|
+
refresh_token=refresh_token,
|
170
|
+
)
|
171
|
+
|
172
|
+
```
|
173
|
+
|
174
|
+
## Important Considerations
|
175
|
+
|
176
|
+
- **Security**: Safeguard your client_id, client_secret, and refresh_token diligently, as they provide access to your Salesforce environment. Avoid sharing or exposing them in unsecured locations.
|
177
|
+
- **Efficient Data Retrieval**: The `query` and `cquery` 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.
|
178
|
+
- **Advanced Tooling Queries**: Utilize the `tooling_query` function to access the Salesforce Tooling API. This option is designed for performing complex operations, enhancing your data management capabilities.
|
179
|
+
|
@@ -0,0 +1,5 @@
|
|
1
|
+
sfq/__init__.py,sha256=Lb3nk6IyyC5_qNy6xOWwfp36Zybtfs8jeGgeiUAbESA,42921
|
2
|
+
sfq/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
+
sfq-0.0.13.dist-info/METADATA,sha256=dBV2044TZgirMKp6VLg9uIZ0dqKq3eL0G1p8w94sGJs,6598
|
4
|
+
sfq-0.0.13.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
5
|
+
sfq-0.0.13.dist-info/RECORD,,
|
sfq-0.0.12.dist-info/METADATA
DELETED
@@ -1,133 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.4
|
2
|
-
Name: sfq
|
3
|
-
Version: 0.0.12
|
4
|
-
Summary: Python wrapper for the Salesforce's Query API.
|
5
|
-
Author-email: David Moruzzi <sfq.pypi@dmoruzi.com>
|
6
|
-
Keywords: salesforce,salesforce query
|
7
|
-
Classifier: Development Status :: 3 - Alpha
|
8
|
-
Classifier: Intended Audience :: Developers
|
9
|
-
Classifier: Programming Language :: Python :: 3.9
|
10
|
-
Classifier: Programming Language :: Python :: 3.10
|
11
|
-
Classifier: Programming Language :: Python :: 3.12
|
12
|
-
Classifier: Programming Language :: Python :: 3.13
|
13
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
14
|
-
Requires-Python: >=3.9
|
15
|
-
Description-Content-Type: text/markdown
|
16
|
-
|
17
|
-
# sfq (Salesforce Query)
|
18
|
-
|
19
|
-
sfq is a lightweight Python wrapper library designed to simplify querying Salesforce, reducing repetitive code for accessing Salesforce data.
|
20
|
-
|
21
|
-
For more varied workflows, consider using an alternative like [Simple Salesforce](https://simple-salesforce.readthedocs.io/en/stable/). This library was even referenced on the [Salesforce Developers Blog](https://developer.salesforce.com/blogs/2021/09/how-to-automate-data-extraction-from-salesforce-using-python).
|
22
|
-
|
23
|
-
## Features
|
24
|
-
|
25
|
-
- Simplified query execution for Salesforce instances.
|
26
|
-
- Integration with Salesforce authentication via refresh tokens.
|
27
|
-
- Option to interact with Salesforce Tooling API for more advanced queries.
|
28
|
-
|
29
|
-
## Installation
|
30
|
-
|
31
|
-
You can install the `sfq` library using `pip`:
|
32
|
-
|
33
|
-
```bash
|
34
|
-
pip install sfq
|
35
|
-
```
|
36
|
-
|
37
|
-
## Usage
|
38
|
-
|
39
|
-
### Library Querying
|
40
|
-
|
41
|
-
```python
|
42
|
-
from sfq import SFAuth
|
43
|
-
|
44
|
-
# Initialize the SFAuth class with authentication details
|
45
|
-
sf = SFAuth(
|
46
|
-
instance_url="https://example-dev-ed.trailblaze.my.salesforce.com",
|
47
|
-
client_id="PlatformCLI",
|
48
|
-
refresh_token="your-refresh-token-here"
|
49
|
-
)
|
50
|
-
|
51
|
-
# Execute a query to fetch account records
|
52
|
-
print(sf.query("SELECT Id FROM Account LIMIT 5"))
|
53
|
-
|
54
|
-
# Execute a query to fetch Tooling API data
|
55
|
-
print(sf.query("SELECT Id, FullName, Metadata FROM SandboxSettings LIMIT 5", tooling=True))
|
56
|
-
```
|
57
|
-
|
58
|
-
### Bash Querying
|
59
|
-
|
60
|
-
You can easily incorporate this into ad-hoc bash scripts or commands:
|
61
|
-
|
62
|
-
```bash
|
63
|
-
python -c "from sfq import SFAuth; sf = SFAuth(instance_url='$instance_url', client_id='$client_id', refresh_token='$refresh_token'); print(sf.query('$query'))" | jq -r '.records[].Id'
|
64
|
-
```
|
65
|
-
|
66
|
-
## How to Obtain Salesforce Tokens
|
67
|
-
|
68
|
-
To use the `sfq` library, you'll need a **client ID** and **refresh token**. The easiest way to obtain these is by using the Salesforce CLI:
|
69
|
-
|
70
|
-
### Steps to Get Tokens
|
71
|
-
|
72
|
-
1. **Install the Salesforce CLI**:
|
73
|
-
Follow the instructions on the [Salesforce CLI installation page](https://developer.salesforce.com/tools/salesforcecli).
|
74
|
-
|
75
|
-
2. **Authenticate with Salesforce**:
|
76
|
-
Login to your Salesforce org using the following command:
|
77
|
-
|
78
|
-
```bash
|
79
|
-
sf org login web --alias int --instance-url https://corpa--int.sandbox.my.salesforce.com
|
80
|
-
```
|
81
|
-
|
82
|
-
3. **Display Org Details**:
|
83
|
-
To get the client ID, refresh token, and instance URL, run:
|
84
|
-
|
85
|
-
```bash
|
86
|
-
sf org display --target-org int --verbose --json
|
87
|
-
```
|
88
|
-
|
89
|
-
The output will look like this:
|
90
|
-
|
91
|
-
```json
|
92
|
-
{
|
93
|
-
"status": 0,
|
94
|
-
"result": {
|
95
|
-
"id": "00Daa0000000000000",
|
96
|
-
"apiVersion": "63.0",
|
97
|
-
"accessToken": "your-access-token-here",
|
98
|
-
"instanceUrl": "https://example-dev-ed.trailblaze.my.salesforce.com",
|
99
|
-
"username": "user@example.com",
|
100
|
-
"clientId": "PlatformCLI",
|
101
|
-
"connectedStatus": "Connected",
|
102
|
-
"sfdxAuthUrl": "force://PlatformCLI::your-refresh-token-here::https://example-dev-ed.trailblaze.my.salesforce.com",
|
103
|
-
"alias": "int"
|
104
|
-
}
|
105
|
-
}
|
106
|
-
```
|
107
|
-
|
108
|
-
4. **Extract and Use the Tokens**:
|
109
|
-
The `sfdxAuthUrl` is structured as:
|
110
|
-
|
111
|
-
```
|
112
|
-
force://<client_id>::<refresh_token>::<instance_url>
|
113
|
-
```
|
114
|
-
|
115
|
-
You can extract and use the tokens in a bash script like this:
|
116
|
-
|
117
|
-
```bash
|
118
|
-
query="SELECT Id FROM User WHERE IsActive = true AND Profile.Name = 'System Administrator'"
|
119
|
-
|
120
|
-
sfdxAuthUrl=$(sf org display --target-org int --verbose --json | jq -r '.result.sfdxAuthUrl' | sed 's/force:\/\///')
|
121
|
-
clientId=$(echo "$sfdxAuthUrl" | sed 's/::/\n/g' | sed -n '1p')
|
122
|
-
refreshToken=$(echo "$sfdxAuthUrl" | sed 's/::/\n/g' | sed -n '2p')
|
123
|
-
instanceUrl=$(echo "$sfdxAuthUrl" | sed 's/::/\n/g' | sed -n '3p')
|
124
|
-
|
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'
|
126
|
-
```
|
127
|
-
|
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.
|
133
|
-
|
sfq-0.0.12.dist-info/RECORD
DELETED
@@ -1,5 +0,0 @@
|
|
1
|
-
sfq/__init__.py,sha256=XEKDLgvHBwGfIU0TDQjyxgLnfiFyCtd0a_MW3DnqFxQ,36032
|
2
|
-
sfq/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
-
sfq-0.0.12.dist-info/METADATA,sha256=glq9BPwFsvXAk1yaK30GrbdT3oDdl2Urd3lpdc3JW0s,5067
|
4
|
-
sfq-0.0.12.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
5
|
-
sfq-0.0.12.dist-info/RECORD,,
|
File without changes
|