Mercurial > personal > weatherlog
view weatherlog/http_writer.py @ 11:6dbd9825b3f5
http_writer: switch to "preamble" based location/password.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 29 Sep 2019 11:52:23 -0400 |
parents | b1657de734d5 |
children | c01f9929ae38 |
line wrap: on
line source
"""RemoteWriter which writes BSON to the given HTTP destination.""" import typing as t import bson import requests from . import logger from . import types class HTTPWriter(logger.RemoteWriter): def __init__(self, url: str, preamble: t.Dict[str, t.Any]): self._url = url self._session = requests.Session() self._session.headers['User-Agent'] = 'weatherlogger/0.0.1' self._preamble = bson.BSON.encode(preamble) def write(self, readings: t.Sequence[types.Reading]) -> None: try: data = b''.join(bson.BSON.encode(r.as_dict()) for r in 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