Create Health
Description
The create_health
function sends a device's health report to the web service. This health report is expected to be a JSON string representing the device’s current status (e.g., temperature, battery level, connectivity).
Function Signature:
def create_health(ws_config: WebServiceConfig, device_name: str, health_json: str) -> WebServiceResult:
Parameters
- ws_config (WebServiceConfig): Configuration object for the web service.
- device_name (str): The name or serial number of the device sending the health report.
- health_json (str): A JSON-formatted string containing the device's health data.
Returns
- WebServiceResult: The result of the web service request, which includes:
- http_response_code: The HTTP response code from the server (e.g., 200 for success).
- http_response_string: The raw response body returned by the server.
- json_data: The parsed JSON response data, if available.
Example Usage
ws_config = WebServiceConfig(base_url="https://api.actionstreamer.com")
device_name = "Camera-01"
health_data = {
"cpuTemp": 55.4,
"batteryLevel": 87,
"uptime": 123456
}
result = create_health(ws_config, device_name, json.dumps(health_data))
print(result.http_response_code, result.http_response_string)
Behavior
- Sends a
POST
request to thev1/devicehealth
endpoint. - The request body contains a JSON object with the device name and the health JSON string.
- If the request is successful (HTTP 200), the response will contain additional details about how the health report was processed.
- Returns a
WebServiceResult
containing the HTTP code, response string, and any parsed response data.
Error Handling
- General Exception: If any exception occurs during request construction or execution, the function logs the error, sets
code
to-1
, and updates the result description accordingly.