Utilities
json_dumps(data)
¶
Serialize data to a JSON formatted bytes object.
Utility function used to allow users to pass an already serialized JSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
str | bytes | bytearray | Any
|
Data to be serialized. |
required |
Returns:
Type | Description |
---|---|
bytes
|
JSON formatted bytes object. |
Source code in src/aiosalesforce/utils.py
json_loads(data)
¶
Deserialize JSON formatted data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
str | bytes | bytearray
|
JSON formatted data. |
required |
Returns:
Type | Description |
---|---|
Any
|
Deserialized data. |
Source code in src/aiosalesforce/utils.py
format_soql(query, *args, **kwargs)
¶
Format SOQL query template with dynamic parameters.
While SOQL queries are safe by design (cannot create/update/delete records), it is still possible to expose sensitive information via SOQL injection. It is always recommended to use this function instead of string formatting.
You should never surround your parameters with single quotes in the query template - this is done automatically by this function when necessary. E.g., "SELECT Id FROM Object WHERE Value = {value}"
The only exception to this rule is when you use the 'like' format spec. The 'like' format spec is used to escape special characters in a LIKE pattern when a portion of it is dynamic. E.g., "SELECT Id FROM Object WHERE Value LIKE '%{value:like}'"
If you don't use the 'like' format spec when formatting LIKE statements, you will get unnecessary quotes and special query characters (% and _) will not be escaped. This would make you vulnerable to SOQL injection.
Examples:
>>> format_soql("SELECT Id FROM Account WHERE Name = {}", "John Doe")
"SELECT Id FROM Account WHERE Name = 'John Doe'"
>>> format_soql("SELECT Id FROM Account WHERE Name = {name}", name="John Doe")
"SELECT Id FROM Account WHERE Name = 'John Doe'"
>>> format_soql("SELECT Id FROM Account WHERE Name LIKE {value}", value="John%")
"SELECT Id FROM Account WHERE Name LIKE 'John%'"
>>> format_soql(
>>> "SELECT Id FROM Record WHERE Description LIKE '% fails {pattern:like}'",
>>> pattern="50% of the time",
>>> )
"SELECT Id FROM Record WHERE Description LIKE '% fails 50\% of the time'"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
LiteralString
|
SOQL query template. |
required |
Returns:
Type | Description |
---|---|
str
|
Formatted SOQL query. |