From b92b24e8a47d4bc244fa1da3dc9e965161427ee4 Mon Sep 17 00:00:00 2001 From: Georgi Kirichkov Date: Thu, 4 Oct 2018 16:54:51 +0300 Subject: [PATCH] Webhook component - pass headers to webhook handler (#17091) * Pass headers to webhook handler * Refactors webhook component to post Request object instead of data * Update webhook tests * Cleanup webhook test and fix a bug in ifttt * Address code review comments --- homeassistant/components/ifttt/__init__.py | 9 ++++++++- homeassistant/components/webhook.py | 11 +---------- tests/components/test_webhook.py | 8 +++----- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/ifttt/__init__.py b/homeassistant/components/ifttt/__init__.py index 534217a7ba2..60748d6ff13 100644 --- a/homeassistant/components/ifttt/__init__.py +++ b/homeassistant/components/ifttt/__init__.py @@ -5,6 +5,7 @@ For more details about this component, please refer to the documentation at https://home-assistant.io/components/ifttt/ """ from ipaddress import ip_address +import json import logging from urllib.parse import urlparse @@ -74,8 +75,14 @@ async def async_setup(hass, config): return True -async def handle_webhook(hass, webhook_id, data): +async def handle_webhook(hass, webhook_id, request): """Handle webhook callback.""" + body = await request.text() + try: + data = json.loads(body) if body else {} + except ValueError: + return None + if isinstance(data, dict): data['webhook_id'] = webhook_id hass.bus.async_fire(EVENT_RECEIVED, data) diff --git a/homeassistant/components/webhook.py b/homeassistant/components/webhook.py index 0e44ffbab25..2a4c3f973f2 100644 --- a/homeassistant/components/webhook.py +++ b/homeassistant/components/webhook.py @@ -3,7 +3,6 @@ For more details about this component, please refer to the documentation at https://home-assistant.io/components/webhook/ """ -import json import logging from aiohttp.web import Response @@ -76,16 +75,8 @@ class WebhookView(HomeAssistantView): 'Received message for unregistered webhook %s', webhook_id) return Response(status=200) - body = await request.text() try: - data = json.loads(body) if body else {} - except ValueError: - _LOGGER.warning( - 'Received webhook %s with invalid JSON', webhook_id) - return Response(status=200) - - try: - response = await handler(hass, webhook_id, data) + response = await handler(hass, webhook_id, request) if response is None: response = Response(status=200) return response diff --git a/tests/components/test_webhook.py b/tests/components/test_webhook.py index c87687292a8..9434c3d98d5 100644 --- a/tests/components/test_webhook.py +++ b/tests/components/test_webhook.py @@ -63,7 +63,7 @@ async def test_posting_webhook_json(hass, mock_client): async def handle(*args): """Handle webhook.""" - hooks.append(args) + hooks.append((args[0], args[1], await args[2].text())) hass.components.webhook.async_register(webhook_id, handle) @@ -74,9 +74,7 @@ async def test_posting_webhook_json(hass, mock_client): assert len(hooks) == 1 assert hooks[0][0] is hass assert hooks[0][1] == webhook_id - assert hooks[0][2] == { - 'data': True - } + assert hooks[0][2] == '{"data": true}' async def test_posting_webhook_no_data(hass, mock_client): @@ -95,4 +93,4 @@ async def test_posting_webhook_no_data(hass, mock_client): assert len(hooks) == 1 assert hooks[0][0] is hass assert hooks[0][1] == webhook_id - assert hooks[0][2] == {} + assert await hooks[0][2].text() == ''