From 3f26fc3b065ae9141a1cbfd0b09d590efb0e2976 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 13 Feb 2015 18:27:13 -0800 Subject: [PATCH] Move more HTTP stuff into constant file --- homeassistant/components/api.py | 16 +++---- homeassistant/components/frontend/__init__.py | 13 +----- homeassistant/components/http/__init__.py | 44 ++++++++----------- homeassistant/const.py | 24 +++++++++- homeassistant/remote.py | 10 ++--- tests/test_component_api.py | 5 ++- tests/test_component_frontend.py | 5 +-- tests/test_remote.py | 3 +- 8 files changed, 59 insertions(+), 61 deletions(-) diff --git a/homeassistant/components/api.py b/homeassistant/components/api.py index 2bbb9516517..8f1ba162b46 100644 --- a/homeassistant/components/api.py +++ b/homeassistant/components/api.py @@ -11,17 +11,11 @@ import homeassistant as ha from homeassistant.helpers import TrackStates import homeassistant.remote as rem from homeassistant.const import ( - URL_API, URL_API_STATES, URL_API_EVENTS, URL_API_SERVICES, - URL_API_EVENT_FORWARD, URL_API_STATES_ENTITY, URL_API_COMPONENTS) - -HTTP_OK = 200 -HTTP_CREATED = 201 -HTTP_MOVED_PERMANENTLY = 301 -HTTP_BAD_REQUEST = 400 -HTTP_UNAUTHORIZED = 401 -HTTP_NOT_FOUND = 404 -HTTP_METHOD_NOT_ALLOWED = 405 -HTTP_UNPROCESSABLE_ENTITY = 422 + URL_API, URL_API_STATES, URL_API_EVENTS, URL_API_SERVICES, URL_API_STREAM, + URL_API_EVENT_FORWARD, URL_API_STATES_ENTITY, URL_API_COMPONENTS, + EVENT_TIME_CHANGED, EVENT_HOMEASSISTANT_STOP, MATCH_ALL, + HTTP_OK, HTTP_CREATED, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, + HTTP_UNPROCESSABLE_ENTITY) DOMAIN = 'api' diff --git a/homeassistant/components/frontend/__init__.py b/homeassistant/components/frontend/__init__.py index b43faf76952..b5446b7236c 100644 --- a/homeassistant/components/frontend/__init__.py +++ b/homeassistant/components/frontend/__init__.py @@ -10,22 +10,11 @@ import logging from . import version import homeassistant.util as util +from homeassistant.const import URL_ROOT, HTTP_OK DOMAIN = 'frontend' DEPENDENCIES = ['api'] -HTTP_OK = 200 -HTTP_CREATED = 201 -HTTP_MOVED_PERMANENTLY = 301 -HTTP_BAD_REQUEST = 400 -HTTP_UNAUTHORIZED = 401 -HTTP_NOT_FOUND = 404 -HTTP_METHOD_NOT_ALLOWED = 405 -HTTP_UNPROCESSABLE_ENTITY = 422 - - -URL_ROOT = "/" - _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/http/__init__.py b/homeassistant/components/http/__init__.py index 8e957b3be7c..ae19d97eb9a 100644 --- a/homeassistant/components/http/__init__.py +++ b/homeassistant/components/http/__init__.py @@ -82,7 +82,12 @@ from socketserver import ThreadingMixIn from urllib.parse import urlparse, parse_qs import homeassistant as ha -from homeassistant.const import SERVER_PORT, AUTH_HEADER +from homeassistant.const import ( + SERVER_PORT, CONTENT_TYPE_JSON, + HTTP_HEADER_HA_AUTH, HTTP_HEADER_CONTENT_TYPE, HTTP_HEADER_ACCEPT_ENCODING, + HTTP_HEADER_CONTENT_ENCODING, HTTP_HEADER_VARY, HTTP_HEADER_CONTENT_LENGTH, + HTTP_HEADER_CACHE_CONTROL, HTTP_HEADER_EXPIRES, HTTP_OK, HTTP_UNAUTHORIZED, + HTTP_NOT_FOUND, HTTP_METHOD_NOT_ALLOWED, HTTP_UNPROCESSABLE_ENTITY) import homeassistant.remote as rem import homeassistant.util as util import homeassistant.bootstrap as bootstrap @@ -90,19 +95,6 @@ import homeassistant.bootstrap as bootstrap DOMAIN = "http" DEPENDENCIES = [] -HTTP_OK = 200 -HTTP_CREATED = 201 -HTTP_MOVED_PERMANENTLY = 301 -HTTP_BAD_REQUEST = 400 -HTTP_UNAUTHORIZED = 401 -HTTP_NOT_FOUND = 404 -HTTP_METHOD_NOT_ALLOWED = 405 -HTTP_UNPROCESSABLE_ENTITY = 422 - -URL_ROOT = "/" - -URL_STATIC = "/static/{}" - CONF_API_PASSWORD = "api_password" CONF_SERVER_HOST = "server_host" CONF_SERVER_PORT = "server_port" @@ -217,7 +209,7 @@ class RequestHandler(SimpleHTTPRequestHandler): data[key] = data[key][-1] # Did we get post input ? - content_length = int(self.headers.get('Content-Length', 0)) + content_length = int(self.headers.get(HTTP_HEADER_CONTENT_LENGTH, 0)) if content_length: body_content = self.rfile.read(content_length).decode("UTF-8") @@ -236,7 +228,7 @@ class RequestHandler(SimpleHTTPRequestHandler): if self.server.no_password_set: api_password = self.server.api_password else: - api_password = self.headers.get(AUTH_HEADER) + api_password = self.headers.get(HTTP_HEADER_HA_AUTH) if not api_password and DATA_API_PASSWORD in data: api_password = data[DATA_API_PASSWORD] @@ -316,7 +308,7 @@ class RequestHandler(SimpleHTTPRequestHandler): def write_json(self, data=None, status_code=HTTP_OK, location=None): """ Helper method to return JSON to the caller. """ self.send_response(status_code) - self.send_header('Content-type', 'application/json') + self.send_header(HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON) if location: self.send_header('Location', location) @@ -344,10 +336,10 @@ class RequestHandler(SimpleHTTPRequestHandler): Helper function to write a file pointer to the user. Does not do error handling. """ - do_gzip = 'gzip' in self.headers.get('accept-encoding', '') + do_gzip = 'gzip' in self.headers.get(HTTP_HEADER_ACCEPT_ENCODING, '') self.send_response(HTTP_OK) - self.send_header("Content-Type", content_type) + self.send_header(HTTP_HEADER_CONTENT_TYPE, content_type) # Add cache if not development if not self.server.development: @@ -355,20 +347,22 @@ class RequestHandler(SimpleHTTPRequestHandler): cache_time = 365 * 86400 self.send_header( - "Cache-Control", "public, max-age={}".format(cache_time)) + HTTP_HEADER_CACHE_CONTROL, + "public, max-age={}".format(cache_time)) self.send_header( - "Expires", self.date_time_string(time.time()+cache_time)) + HTTP_HEADER_EXPIRES, + self.date_time_string(time.time()+cache_time)) if do_gzip: gzip_data = gzip.compress(inp.read()) - self.send_header("Content-Encoding", "gzip") - self.send_header("Vary", "Accept-Encoding") - self.send_header("Content-Length", str(len(gzip_data))) + self.send_header(HTTP_HEADER_CONTENT_ENCODING, "gzip") + self.send_header(HTTP_HEADER_VARY, HTTP_HEADER_ACCEPT_ENCODING) + self.send_header(HTTP_HEADER_CONTENT_LENGTH, str(len(gzip_data))) else: fst = os.fstat(inp.fileno()) - self.send_header("Content-Length", str(fst[6])) + self.send_header(HTTP_HEADER_CONTENT_LENGTH, str(fst[6])) self.end_headers() diff --git a/homeassistant/const.py b/homeassistant/const.py index d4ca0009d83..616baa5c42a 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -89,9 +89,9 @@ SERVICE_MEDIA_PREV_TRACK = "media_prev_track" # #### API / REMOTE #### SERVER_PORT = 8123 -AUTH_HEADER = "X-HA-access" - +URL_ROOT = "/" URL_API = "/api/" +URL_API_STREAM = "/api/stream" URL_API_STATES = "/api/states" URL_API_STATES_ENTITY = "/api/states/{}" URL_API_EVENTS = "/api/events" @@ -100,3 +100,23 @@ URL_API_SERVICES = "/api/services" URL_API_SERVICES_SERVICE = "/api/services/{}/{}" URL_API_EVENT_FORWARD = "/api/event_forwarding" URL_API_COMPONENTS = "/api/components" + +HTTP_OK = 200 +HTTP_CREATED = 201 +HTTP_MOVED_PERMANENTLY = 301 +HTTP_BAD_REQUEST = 400 +HTTP_UNAUTHORIZED = 401 +HTTP_NOT_FOUND = 404 +HTTP_METHOD_NOT_ALLOWED = 405 +HTTP_UNPROCESSABLE_ENTITY = 422 + +HTTP_HEADER_HA_AUTH = "X-HA-access" +HTTP_HEADER_ACCEPT_ENCODING = "Accept-Encoding" +HTTP_HEADER_CONTENT_TYPE = "Content-type" +HTTP_HEADER_CONTENT_ENCODING = "Content-Encoding" +HTTP_HEADER_VARY = "Vary" +HTTP_HEADER_CONTENT_LENGTH = "Content-Length" +HTTP_HEADER_CACHE_CONTROL = "Cache-Control" +HTTP_HEADER_EXPIRES = "Expires" + +CONTENT_TYPE_JSON = "application/json" diff --git a/homeassistant/remote.py b/homeassistant/remote.py index bc3a16dd210..955af0900e6 100644 --- a/homeassistant/remote.py +++ b/homeassistant/remote.py @@ -21,9 +21,9 @@ import homeassistant as ha import homeassistant.bootstrap as bootstrap from homeassistant.const import ( - SERVER_PORT, AUTH_HEADER, URL_API, URL_API_STATES, URL_API_STATES_ENTITY, - URL_API_EVENTS, URL_API_EVENTS_EVENT, URL_API_SERVICES, - URL_API_SERVICES_SERVICE, URL_API_EVENT_FORWARD) + SERVER_PORT, HTTP_HEADER_HA_AUTH, URL_API, URL_API_STATES, + URL_API_STATES_ENTITY, URL_API_EVENTS, URL_API_EVENTS_EVENT, + URL_API_SERVICES, URL_API_SERVICES_SERVICE, URL_API_EVENT_FORWARD) METHOD_GET = "get" METHOD_POST = "post" @@ -55,7 +55,7 @@ class API(object): self.api_password = api_password self.base_url = "http://{}:{}".format(host, self.port) self.status = None - self._headers = {AUTH_HEADER: api_password} + self._headers = {HTTP_HEADER_HA_AUTH: api_password} def validate_api(self, force_validate=False): """ Tests if we can communicate with the API. """ @@ -308,7 +308,7 @@ def connect_remote_events(from_api, to_api): return True else: _LOGGER.error( - "Error settign up event forwarding: %s - %s", + "Error setting up event forwarding: %s - %s", req.status_code, req.text) return False diff --git a/tests/test_component_api.py b/tests/test_component_api.py index bf843d8ceed..ff25b476d32 100644 --- a/tests/test_component_api.py +++ b/tests/test_component_api.py @@ -14,6 +14,7 @@ import homeassistant as ha import homeassistant.bootstrap as bootstrap import homeassistant.remote as remote import homeassistant.components.http as http +from homeassistant.const import HTTP_HEADER_HA_AUTH API_PASSWORD = "test1234" @@ -24,7 +25,7 @@ SERVER_PORT = 8120 HTTP_BASE_URL = "http://127.0.0.1:{}".format(SERVER_PORT) -HA_HEADERS = {remote.AUTH_HEADER: API_PASSWORD} +HA_HEADERS = {HTTP_HEADER_HA_AUTH: API_PASSWORD} hass = None @@ -71,7 +72,7 @@ class TestAPI(unittest.TestCase): def test_access_denied_with_wrong_password(self): req = requests.get( _url(remote.URL_API_STATES_ENTITY.format("test")), - headers={remote.AUTH_HEADER: 'wrongpassword'}) + headers={HTTP_HEADER_HA_AUTH: 'wrongpassword'}) self.assertEqual(401, req.status_code) diff --git a/tests/test_component_frontend.py b/tests/test_component_frontend.py index d549ac7bf68..d6431f5f5df 100644 --- a/tests/test_component_frontend.py +++ b/tests/test_component_frontend.py @@ -12,9 +12,8 @@ import requests import homeassistant as ha import homeassistant.bootstrap as bootstrap -import homeassistant.remote as remote import homeassistant.components.http as http -import homeassistant.components.frontend as frontend +from homeassistant.const import HTTP_HEADER_HA_AUTH API_PASSWORD = "test1234" @@ -25,7 +24,7 @@ SERVER_PORT = 8121 HTTP_BASE_URL = "http://127.0.0.1:{}".format(SERVER_PORT) -HA_HEADERS = {remote.AUTH_HEADER: API_PASSWORD} +HA_HEADERS = {HTTP_HEADER_HA_AUTH: API_PASSWORD} hass = None diff --git a/tests/test_remote.py b/tests/test_remote.py index cffa827ef4c..38131e10a4e 100644 --- a/tests/test_remote.py +++ b/tests/test_remote.py @@ -13,12 +13,13 @@ import homeassistant as ha import homeassistant.bootstrap as bootstrap import homeassistant.remote as remote import homeassistant.components.http as http +from homeassistant.const import HTTP_HEADER_HA_AUTH API_PASSWORD = "test1234" HTTP_BASE_URL = "http://127.0.0.1:8122" -HA_HEADERS = {remote.AUTH_HEADER: API_PASSWORD} +HA_HEADERS = {HTTP_HEADER_HA_AUTH: API_PASSWORD} hass, slave, master_api, broken_api = None, None, None, None