Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webhook component - pass headers to webhook handler #17091

Merged
merged 5 commits into from
Oct 4, 2018

Conversation

kirichkov
Copy link
Contributor

@kirichkov kirichkov commented Oct 3, 2018

Description:

I'm suggesting to pass the headers (or the whole request object) to the webhook handler, besides the data and the webhook_id.

As I'm working on switching OwnTracks HTTP to use the webhooks component, I found out that the webhook component will pass to the handler only the data that is POST-ed by the OwnTracks app. The app, however, posts some necessary data as the username and the device ID as part of the headers, and the username and device ID are needed to match the device with the known_devices.yaml entry.

If I have access to the headers I can extract the necessary data automatically, and not require the user to create separate webhook IDs for each device they want to add.

If this suggestion is accepted, I'll need to update the IFTTT component too.

Related issue (if applicable): home-assistant/architecture/issues/80, /pull/16817

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

@ghost ghost added the in progress label Oct 3, 2018
@@ -85,7 +85,7 @@ class WebhookView(HomeAssistantView):
return Response(status=200)

try:
response = await handler(hass, webhook_id, data)
response = await handler(hass, webhook_id, data, headers)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'headers'

@@ -85,7 +85,7 @@ class WebhookView(HomeAssistantView):
return Response(status=200)

try:
response = await handler(hass, webhook_id, data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that we drop data and just pass request

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I'll clean up the rest of webhook and make the necessary changes in ifttt and update the tests (if needed)

Copy link
Contributor Author

@kirichkov kirichkov Oct 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just looked at the code. If we drop data altogether, each component that depends on webhook will have to implement the JSON validation on its own. Upside is that the webhook component can be used to accept calls that submit non-JSON data.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think that is fine . Gives most flexibility.

@kirichkov kirichkov changed the title [WIP] Webhook component - pass headers to webhook handler Webhook component - pass headers to webhook handler Oct 3, 2018
assert hooks[0][2] == {
'data': True
}
# assert await hooks[0][2].text() == '{\'data\':true}' # This line raises PayloadAccessError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (96 > 79 characters)

except ValueError:
_LOGGER.warning(
'Received webhook %s with invalid JSON', webhook_id)
return Response(status=200)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just return None. webhook defaults to this.

'data': True
}
# The line below causes PayloadAccessError
# assert await hooks[0][2].text() == '{\'data\':true}'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't just comment it out 😉 Instead, just extract the info inside handle and store it in hooks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously. My intention was to put a comment here, and ask for advice, but Travis is taking longer than usual :-)

import logging
from urllib.parse import urlparse

from aiohttp.web import Response

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'aiohttp.web.Response' imported but unused

@balloob balloob merged commit b92b24e into home-assistant:dev Oct 4, 2018
@ghost ghost removed the in progress label Oct 4, 2018
@balloob
Copy link
Member

balloob commented Oct 4, 2018

Nice! 🐬 🎉

@kirichkov kirichkov deleted the webhook-pass-request branch October 4, 2018 15:30
@balloob balloob mentioned this pull request Oct 12, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Feb 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants