Mercurial > personal > weatherlog
changeset 18:9daa281d996b
Add a HTTP timeout, and verify the server response.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Thu, 16 Jan 2020 23:52:30 -0500 |
parents | 39c0686e6765 |
children | 7117db65715e |
files | setup.py weatherlog/http_writer.py |
diffstat | 2 files changed, 11 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- 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=[
--- 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