Beispiel #1
0
    async def async_webhook_message(self, payload: dict[Any, Any]) -> dict[Any, Any]:
        """Process cloud webhook message to client."""
        cloudhook_id = payload["cloudhook_id"]

        found = None
        for cloudhook in self._prefs.cloudhooks.values():
            if cloudhook["cloudhook_id"] == cloudhook_id:
                found = cloudhook
                break

        if found is None:
            return {"status": HTTPStatus.OK}

        request = MockRequest(
            content=payload["body"].encode("utf-8"),
            headers=payload["headers"],
            method=payload["method"],
            query_string=payload["query"],
            mock_source=DOMAIN,
        )

        response = await webhook.async_handle_webhook(
            self._hass, found["webhook_id"], request
        )

        response_dict = serialize_response(response)
        body = response_dict.get("body")

        return {
            "body": body,
            "status": response_dict["status"],
            "headers": {"Content-Type": response.content_type},
        }
Beispiel #2
0
async def websocket_handle(hass, connection, msg):
    """Handle an incoming webhook via the WS API."""
    request = MockRequest(
        content=msg["body"].encode("utf-8"),
        headers=msg["headers"],
        method=msg["method"],
        query_string=msg["query"],
        mock_source=f"{DOMAIN}/ws",
    )

    response = await async_handle_webhook(hass, msg["webhook_id"], request)

    response_dict = serialize_response(response)
    body = response_dict.get("body")

    connection.send_result(
        msg["id"],
        {
            "body": body,
            "status": response_dict["status"],
            "headers": {
                "Content-Type": response.content_type
            },
        },
    )
Beispiel #3
0
async def async_handle_webhook(hass, cloud, payload):
    """Handle an incoming IoT message for cloud webhooks."""
    cloudhook_id = payload['cloudhook_id']

    found = None
    for cloudhook in cloud.prefs.cloudhooks.values():
        if cloudhook['cloudhook_id'] == cloudhook_id:
            found = cloudhook
            break

    if found is None:
        return {'status': 200}

    request = MockRequest(
        content=payload['body'].encode('utf-8'),
        headers=payload['headers'],
        method=payload['method'],
        query_string=payload['query'],
    )

    response = await hass.components.webhook.async_handle_webhook(
        found['webhook_id'], request)

    response_dict = serialize_response(response)
    body = response_dict.get('body')
    if body:
        body = body.decode('utf-8')

    return {
        'body': body,
        'status': response_dict['status'],
        'headers': {
            'Content-Type': response.content_type
        }
    }
def test_serialize_json():
    """Test serializing a JSON response."""
    response = web.json_response({"how": "what"})
    assert aiohttp.serialize_response(response) == {
        'status': 200,
        'body': b'{"how": "what"}',
        'headers': {'Content-Type': 'application/json; charset=utf-8'},
    }
def test_serialize_text():
    """Test serializing a text response."""
    response = web.Response(status=201, text='Hello')
    assert aiohttp.serialize_response(response) == {
        'status': 201,
        'body': b'Hello',
        'headers': {'Content-Type': 'text/plain; charset=utf-8'},
    }