IoT Edge Validator Library for Python
Project description
iot-edge-validator
This package is a wrapper around the azure-iot-device SDK to provide standardized exception handling and direct method request validation.
Table of Contents
- Versioning
- Getting Started
- Compare Dictionary Method
- Format Exception Error Method
- Generate Error Response Method
Versioning
This repository adheres to Semantic Versioning. It will be maintained through the CHANGELOG.md
and in GitHub Releases. It's important to note that you must maintain the version with your releases in iot/edge/validator/_version.py
, otherwise a new package version will fail to get published.
Getting Started
This section provides basic examples with the iot-edge-validator
.
Prerequisites
-
Python 3.7 or later is required to use this package.
-
You must have an Azure subscription and Azure IoT Edge Device to use this package.
Basic Examples
-
Install via pip:
pip install iot-edge-validator
-
Import and say hello:
from iot.edge.validator import __version__ print(f"hello world from iot-edge-validator version: {__version__}")
-
Basic usage:
from typing import Any, Dict, Union from azure.iot.device.iothub.models.methods import MethodRequest, MethodResponse from iot.edge.validator import ( format_exception_error, generate_error_response, compare_dictionary, ) EXPECTED_METHOD_NAME: str = "some_method_name" EXPECTED_METHOD_PAYLOAD: Dict[str, Any] = { "some": {}, "expected": {}, "payload": {}, } def validate_method_requests(method_request: MethodRequest) -> Union[MethodResponse, None]: """validation handler for some_method_name listener""" if method_request.name == EXPECTED_METHOD_NAME: pass else: return generate_error_response(method_request, f"received unknown method request for {method_request.name}", 400, ) def validate_some_method_name_request(method_request: MethodRequest) -> Union[MethodResponse, None]: """validation for expected payload of some_method_name direct method request""" # top level basic format validation error_msg = compare_dictionary( d1=method_request.payload, d2=EXPECTED_METHOD_PAYLOAD, value_match=False, recurse=False, ) if error_msg: return generate_error_response( method_request, f"error parsing payload: {error_msg}", 400 ) return None
Compare Dictionary Method
Compare two dictionaries for keys, optionally compare recursively and optionally compare for exact values to match.
compare_dictionary(d1, d2, value_match=False, recurse=True)
Parameters
-
d1
Dict[str, Any]Dictionary to compare.
-
d2
Dict[str, Any]Dictionary to compare.
-
value_match
Optional[bool]Compare for exact values to match. Default is False.
-
recurse
Optional[bool]Recurse through sub dictionaries of the given dictionary. Default is True.
Returns
Returns a string - either empty or containing an error message.
Format Exception Error Method
Format exceptions using traceback.
format_exception_error(context, exception)
Parameters
-
context
strThe context of where your program was when the execption occurred.
-
exception
ExceptionThe caught exception.
Returns
Returns a string containing the formatted exception error and tracing.
Generate Error Response Method
Given a method request, generate an error response.
generate_error_response(request, message, status=500)
Parameters
-
request
azure.iot.device.iothub.models.method.MethodRequestThe method request to use when generating the error response.
-
message
strThe custom error message.
-
status
Optional[int]Error status code. Default is 500.
Returns
Returns a MethodResponse of type azure.iot.device.iothub.models.methods.MethodResponse
.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for iot_edge_validator-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7c9c4e922aeb79b773b772732f6217b93c30fcdb89193550aecce8d85e154d3 |
|
MD5 | 309e46af6377bf050d60745bc68387bc |
|
BLAKE2b-256 | b346e8fcfa871038e17f3234796a3a69560e752317505a9f3c59d777e9432014 |