Mercurial > personal > weatherlog
comparison 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 |
comparison
equal
deleted
inserted
replaced
13:4c81182eaa6b | 14:c01f9929ae38 |
---|---|
1 """RemoteWriter which writes BSON to the given HTTP destination.""" | 1 """RemoteWriter which writes BSON to the given HTTP destination.""" |
2 | 2 |
3 import typing as t | 3 import typing as t |
4 | 4 |
5 import bson | |
6 import requests | 5 import requests |
7 | 6 |
8 from . import logger | 7 from . import logger |
9 from . import types | |
10 | 8 |
11 | 9 |
12 class HTTPWriter(logger.RemoteWriter): | 10 class HTTPWriter(logger.RemoteWriter): |
13 | 11 |
14 def __init__(self, url: str, preamble: t.Dict[str, t.Any]): | 12 def __init__(self, url: str, preamble: t.Dict[str, object]): |
15 self._url = url | 13 self._url = url |
16 self._session = requests.Session() | 14 self._session = requests.Session() |
17 self._session.headers['User-Agent'] = 'weatherlogger/0.0.1' | 15 self._session.headers['User-Agent'] = 'weatherlogger/0.0.1' |
18 self._preamble = bson.BSON.encode(preamble) | 16 self._preamble = logger.bson_encode(preamble) |
19 | 17 |
20 def write(self, readings: t.Sequence[types.Reading]) -> None: | 18 def write(self, readings: t.Iterable[t.Dict[str, object]]) -> None: |
21 try: | 19 try: |
22 data = b''.join(bson.BSON.encode(r.as_dict()) for r in readings) | 20 data = b''.join(map(logger.bson_encode, readings)) |
23 response = self._session.post(self._url, data=self._preamble + data) | 21 response = self._session.post(self._url, data=self._preamble + data) |
24 response.raise_for_status() | 22 response.raise_for_status() |
25 except requests.exceptions.RequestException as rex: | 23 except requests.exceptions.RequestException as rex: |
26 raise logger.RemoteWriteError("Coulndn't write values") from rex | 24 raise logger.RemoteWriteError("Coulndn't write values") from rex |