# HG changeset patch # User Paul Fisher # Date 1579236750 18000 # Node ID 9daa281d996b8b2f843eadb15e18e0c6a90a560a # Parent 39c0686e67658cf3baf08ab32b92cd12a1d68fa2 Add a HTTP timeout, and verify the server response. diff -r 39c0686e6765 -r 9daa281d996b setup.py --- a/setup.py Thu Oct 17 22:28:12 2019 -0400 +++ b/setup.py Thu Jan 16 23:52:30 2020 -0500 @@ -2,7 +2,7 @@ setuptools.setup( name='weatherlog', - version='0.2.0', + version='0.2.1', packages=setuptools.find_packages(), python_requires='>=3.7', install_requires=[ diff -r 39c0686e6765 -r 9daa281d996b weatherlog/http_writer.py --- a/weatherlog/http_writer.py Thu Oct 17 22:28:12 2019 -0400 +++ b/weatherlog/http_writer.py Thu Jan 16 23:52:30 2020 -0500 @@ -6,6 +6,8 @@ from . import logger +TIMEOUT_SECS = 60 + class HTTPWriter(logger.RemoteWriter): @@ -18,7 +20,14 @@ def write(self, readings: t.Iterable[t.Dict[str, object]]) -> None: try: data = b''.join(map(logger.bson_encode, readings)) - response = self._session.post(self._url, data=self._preamble + data) + response = self._session.post( + self._url, data=self._preamble + data, timeout=TIMEOUT_SECS) response.raise_for_status() + contents = response.json() + if contents.get('status') != 'OK': + raise logger.RemoteWriteError( + f'Invalid server response: {contents!r}') except requests.exceptions.RequestException as rex: raise logger.RemoteWriteError("Coulndn't write values") from rex + except ValueError as ve: + raise logger.RemoteWriteError('Invalid JSON response') from ve