annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
1 """RemoteWriter which writes BSON to the given HTTP destination."""
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
2
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
3 import typing as t
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
4
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
5 import bson
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
6 import requests
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
7
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
8 from . import logger
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
9 from . import types
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
12 class HTTPWriter(logger.RemoteWriter):
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
13
11
6dbd9825b3f5 http_writer: switch to "preamble" based location/password.
Paul Fisher <paul@pfish.zone>
parents: 10
diff changeset
14 def __init__(self, url: str, preamble: t.Dict[str, t.Any]):
10
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15 self._url = url
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 self._session = requests.Session()
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17 self._session.headers['User-Agent'] = 'weatherlogger/0.0.1'
11
6dbd9825b3f5 http_writer: switch to "preamble" based location/password.
Paul Fisher <paul@pfish.zone>
parents: 10
diff changeset
18 self._preamble = bson.BSON.encode(preamble)
10
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
20 def write(self, readings: t.Sequence[types.Reading]) -> None:
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
21 try:
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
22 data = b''.join(bson.BSON.encode(r.as_dict()) for r in readings)
11
6dbd9825b3f5 http_writer: switch to "preamble" based location/password.
Paul Fisher <paul@pfish.zone>
parents: 10
diff changeset
23 response = self._session.post(self._url, data=self._preamble + data)
10
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
24 response.raise_for_status()
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
25 except requests.exceptions.RequestException as rex:
b1657de734d5 Add HTTPWriter, which actually writes over HTTP.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26 raise logger.RemoteWriteError("Coulndn't write values") from rex