Utils
opengate_data.utils.expressions
Expressions Objects
Expressions
The filter conditions a search accepts, as static methods returning plain
dicts: eq, neq, like, gt, lt, gte, lte, in_, nin and
exists. FilterBuilder extends this class, so in practice you reach them
through client.new_filter_builder() and combine them with and_ / or_
before calling build().
Example:
eq
Adds an equality condition to the filter.
Arguments:
fieldstr - The field to compare.valueany - The value to compare against.
Returns:
dict- The equality condition.
Example:
eq(“device.operationalStatus”, “NORMAL”)
neq
Adds a not-equal condition to the filter.
Arguments:
fieldstr - The field to compare.valueany - The value to compare against.
Returns:
dict- The not-equal condition.
Example:
neq(“device.operationalStatus”, “ERROR”)
like
Adds a like condition to the filter.
Arguments:
fieldstr - The field to compare.valuestr - The regex pattern to match.
Returns:
dict- The like condition.
Example:
like(“device.name”, “device_.*”)
gt
Adds a greater-than condition to the filter.
Arguments:
fieldstr - The field to compare.valueany - The value to compare against.
Returns:
dict- The greater-than condition.
Example:
gt(“device.batteryLevel”, 50)
lt
Adds a less-than condition to the filter.
Arguments:
fieldstr - The field to compare.valueany - The value to compare against.
Returns:
dict- The less-than condition.
Example:
lt(“device.batteryLevel”, 20)
gte
Adds a greater-than-or-equal condition to the filter.
Arguments:
fieldstr - The field to compare.valueany - The value to compare against.
Returns:
dict- The greater-than-or-equal condition.
Example:
gte(“device.batteryLevel”, 80)
lte
Adds a less-than-or-equal condition to the filter.
Arguments:
fieldstr - The field to compare.valueany - The value to compare against.
Returns:
dict- The less-than-or-equal condition.
Example:
lte(“device.batteryLevel”, 30)
in_
Adds an in condition to the filter.
Arguments:
fieldstr - The field to compare.valueslist - The list of values to compare against.
Returns:
dict- The in condition.
Example:
in_(“device.name”, [“device_1”, “device_2”])
nin
Adds a not-in condition to the filter.
Arguments:
fieldstr - The field to compare.valueslist - The list of values to compare against.
Returns:
dict- The not-in condition.
Example:
nin(“device.name”, [“device_3”, “device_4”])
exists
Adds an existence condition to the filter.
Arguments:
fieldstr - The field to check for existence.valuebool - True to match the entities that have the field, False to match the ones that do not.
Returns:
dict- The existence condition.
Example:
opengate_data.utils.utils
validate_type
Validates that the given variable is of the expected type or types.
This function checks if the variable matches the expected type or any type in a tuple of expected types. It raises a TypeError if the variable does not match the expected type(s).
Arguments:
variableAny - The variable to be checked.expected_typeAny - The expected type or a tuple of expected types.variable_namestr - The name of the variable, used in the error message to identify the variable.
Raises:
TypeError- If the variable is not of the expected type(s).
Returns:
None- This function does not return a value; it raises an exception if the type check fails.
set_method_call
Decorates a method to ensure it is properly registered and tracked within the builder’s workflow.
This decorator adds the method’s name to a set that tracks method calls
Arguments:
methodfunction - The method to be decorated.
Returns:
function- The wrapped method with added functionality to register its call.
Raises:
None- This decorator does not raise exceptions by itself but ensures the method call is registered.
parse_json
Attempts to convert a string into a Python object by interpreting it as JSON.
Arguments:
valuestr | Any - The value to attempt to convert. If the value is not a string, it is returned directly without attempting conversion.
Returns:
Any- The Python object resulting from the JSON conversion ifvalueis a valid JSON string. If the conversion fails due to a formatting error (ValueError), the original value is returned. Ifvalueis not a string, it is returned as is.
send_request
Helper function to make HTTP requests using the requests library.
Arguments:
methodstr - HTTP method (e.g., ‘GET’, ‘POST’, ‘PUT’, ‘DELETE’, ‘PATCH’).headersdict, optional - Request headers.urlstr - The URL for the request.datadict | str, optional - Payload for the request body.filesAny, optional - Files for multipart encoding upload.json_payloaddict, optional - JSON payload for the request body.paramsdict | Any, optional - URL parameters.verifybool - Whether to verify SSL certificates. Defaults to False.timeoutint - Request timeout in seconds. Defaults to 3000.streambool - Whether to keep the response body unread until it is consumed, which is what a file download needs. Defaults to False.
Returns:
Response | dict[str, Any]: The response object if successful, or an error dictionary.
handle_basic_response
Handle basic HTTP response.
This function processes the HTTP response and returns a dictionary containing the status code. If the response indicates an error, it also includes the error message.
Arguments:
responserequests.Response - The response object to process.
Returns:
dict[str, Any]: A dictionary containing the status code and, if applicable, the error message.
handle_error_response
Turn a failed HTTP response into the dictionary the builders return.
Nothing is raised: the caller gets the status code and, when the platform sent one, the body as the error.
Arguments:
responserequests.Response - The response object to process.
Returns:
dict[str, Any]: The status code and, if the response carried a body, the error.
handle_exception
Turn a transport failure into the dictionary the builders return.
A request that never reached the platform has no status code, so the result names the kind of failure instead: connection, timeout, request or unexpected.
Arguments:
eException - The exception raised while sending the request.
Returns:
dict[str, str]: The kind of failure and its details.
validate_build_method_calls_execute
Check that a chain was closed properly before execute() runs.
Every builder has to be closed with build() or with build_execute(),
and build() has to be the last call before execute() — otherwise a
setter called after it would never be validated.
Arguments:
method_callslist[str] - The methods called on the builder, in order.
Raises:
Exception- Ifbuild()was called more than once, if it was not the last call beforeexecute(), or if neitherbuild()norbuild_execute()was called.
validate_build
Generic builder validator with messages intended for the end user.
The two alias maps exist so the errors name what the caller wrote, not the
internals: without them a missing with_destiny_path() would be reported
as a missing ‘path’.
Arguments:
methodstr - The operation selected in the chain, which picks its entry in the spec.statedict - The current value of each field of the builder, by internal name.specdict - The rules per operation: ‘required’ and ‘forbidden’ field names, ‘choices’ mapping a field to its allowed values, and ‘custom’ holding extra checks called with the state.used_methodslist[str] | None - The methods called on the builder, in order. Needed to catch two operations in the same chain.allowed_method_callsset[str] | None - The method names that select an operation, of which a chain may use exactly one, once.field_aliasesdict[str, str] | None - Maps internal fields to public setter names (e.g., ‘path’ → ‘with_path’).method_aliasesdict[str, str] | None - Maps logical method names to public method names (e.g., ’list_one’ → ’list_one()’).
Raises:
RuntimeError- If the chain combines several operations, or repeats one.ValueError- If no operation was selected, if it is not in the spec, or if the state misses a required field, carries a forbidden one, or holds a value outside its choices.
build_headers
Build request headers starting from client headers (auth preserved) and optionally overriding Accept / Content-Type.
This function NEVER mutates client headers.
Arguments:
client_headersdict | None - The headers of the client, holding the authentication. They are copied, never modified.acceptstr | None - The Accept header to set. Left untouched if None.content_typestr | None - The Content-Type header to set. Left untouched if None.
Returns:
dict- A new set of headers.