Mercurial > personal > weatherlog
view weatherlog/http_writer.py @ 14:c01f9929ae38
Make logger and HTTP writer more general and resilient.
This makes the logger and HTTP writer more general, by removing
any dependency upon the exact data type they are writing.
They can now handle any type of BSON-serializable dict,
and track what they have sent by keeping track of the last *byte*,
not the last timestamp.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Tue, 15 Oct 2019 22:40:24 -0400 |
parents | 6dbd9825b3f5 |
children | 9daa281d996b |
line wrap: on
line source
"""RemoteWriter which writes BSON to the given HTTP destination.""" import typing as t import requests from . import logger class HTTPWriter(logger.RemoteWriter): def __init__(self, url: str, preamble: t.Dict[str, object]): self._url = url self._session = requests.Session() self._session.headers['User-Agent'] = 'weatherlogger/0.0.1' self._preamble = logger.bson_encode(preamble) 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.raise_for_status() except requests.exceptions.RequestException as rex: raise logger.RemoteWriteError("Coulndn't write values") from rex