mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
exposed content_type in rest_command (#7101)
* exposed content_type in rest_command, which allows for manually specifying the content_type for more-strict api endpoints * fixed up column length Length was 86 chars, and it needed to be 79 * double import of HTTP_HEADER_CONTENT_TYPE Removed the accidental double-import of HTTP_HEADER_CONTENT_TYPE * moved rest_command-specific config value into component * if no content_type, default to None * unit test * newline * unused CONTENT_TYPE_TEXT_PLAIN * removed the http-agnostic abstraction hass provided in favor of aiohttps hdrs constant
This commit is contained in:
parent
43799b8fee
commit
226066eafd
@ -8,6 +8,7 @@ import asyncio
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
from aiohttp import hdrs
|
||||||
import async_timeout
|
import async_timeout
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -31,6 +32,8 @@ SUPPORT_REST_METHODS = [
|
|||||||
'delete',
|
'delete',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
CONF_CONTENT_TYPE = 'content_type'
|
||||||
|
|
||||||
COMMAND_SCHEMA = vol.Schema({
|
COMMAND_SCHEMA = vol.Schema({
|
||||||
vol.Required(CONF_URL): cv.template,
|
vol.Required(CONF_URL): cv.template,
|
||||||
vol.Optional(CONF_METHOD, default=DEFAULT_METHOD):
|
vol.Optional(CONF_METHOD, default=DEFAULT_METHOD):
|
||||||
@ -39,6 +42,7 @@ COMMAND_SCHEMA = vol.Schema({
|
|||||||
vol.Inclusive(CONF_PASSWORD, 'authentication'): cv.string,
|
vol.Inclusive(CONF_PASSWORD, 'authentication'): cv.string,
|
||||||
vol.Optional(CONF_PAYLOAD): cv.template,
|
vol.Optional(CONF_PAYLOAD): cv.template,
|
||||||
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): vol.Coerce(int),
|
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): vol.Coerce(int),
|
||||||
|
vol.Optional(CONF_CONTENT_TYPE): cv.string
|
||||||
})
|
})
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema({
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
@ -72,6 +76,11 @@ def async_setup(hass, config):
|
|||||||
template_payload = command_config[CONF_PAYLOAD]
|
template_payload = command_config[CONF_PAYLOAD]
|
||||||
template_payload.hass = hass
|
template_payload.hass = hass
|
||||||
|
|
||||||
|
headers = None
|
||||||
|
if CONF_CONTENT_TYPE in command_config:
|
||||||
|
content_type = command_config[CONF_CONTENT_TYPE]
|
||||||
|
headers = {hdrs.CONTENT_TYPE: content_type}
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_service_handler(service):
|
def async_service_handler(service):
|
||||||
"""Execute a shell command service."""
|
"""Execute a shell command service."""
|
||||||
@ -86,7 +95,8 @@ def async_setup(hass, config):
|
|||||||
request = yield from getattr(websession, method)(
|
request = yield from getattr(websession, method)(
|
||||||
template_url.async_render(variables=service.data),
|
template_url.async_render(variables=service.data),
|
||||||
data=payload,
|
data=payload,
|
||||||
auth=auth
|
auth=auth,
|
||||||
|
headers=headers
|
||||||
)
|
)
|
||||||
|
|
||||||
if request.status < 400:
|
if request.status < 400:
|
||||||
|
@ -221,3 +221,22 @@ class TestRestCommandComponent(object):
|
|||||||
|
|
||||||
assert len(aioclient_mock.mock_calls) == 1
|
assert len(aioclient_mock.mock_calls) == 1
|
||||||
assert aioclient_mock.mock_calls[0][2] == b'data'
|
assert aioclient_mock.mock_calls[0][2] == b'data'
|
||||||
|
|
||||||
|
def test_rest_command_content_type(self, aioclient_mock):
|
||||||
|
"""Call a rest command with a content type."""
|
||||||
|
data = {
|
||||||
|
'payload': 'item',
|
||||||
|
'content_type': 'text/plain'
|
||||||
|
}
|
||||||
|
self.config[rc.DOMAIN]['post_test'].update(data)
|
||||||
|
|
||||||
|
with assert_setup_component(4):
|
||||||
|
setup_component(self.hass, rc.DOMAIN, self.config)
|
||||||
|
|
||||||
|
aioclient_mock.post(self.url, content=b'success')
|
||||||
|
|
||||||
|
self.hass.services.call(rc.DOMAIN, 'post_test', {})
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
assert len(aioclient_mock.mock_calls) == 1
|
||||||
|
assert aioclient_mock.mock_calls[0][2] == b'item'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user