Mercurial > personal > weatherlog
view weatherlog/http_writer.py @ 10:b1657de734d5
Add HTTPWriter, which actually writes over HTTP.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 29 Sep 2019 01:18:58 -0400 |
parents | |
children | 6dbd9825b3f5 |
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): self._url = url self._session = requests.Session() self._session.headers['User-Agent'] = 'weatherlogger/0.0.1' 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=data) response.raise_for_status() except requests.exceptions.RequestException as rex: raise logger.RemoteWriteError("Coulndn't write values") from rex