-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataGatewayClient.py
More file actions
52 lines (40 loc) · 1.47 KB
/
DataGatewayClient.py
File metadata and controls
52 lines (40 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import json
import datetime
from libhttp import *
class AggressiveEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, datetime.datetime):
return o.isoformat(' ')
elif isinstance(o, datetime.time) or isinstance(o, datetime.date):
return o.isoformat()
elif hasattr(o, '__dict__'):
return o.__dict__
return json.JSONEncoder.default(self, o)
class DataGatewayJSONRequest(object):
def __init__(self, key, data, storage):
self.key = key
self.data = data
self.storage = storage
def getstr(self):
return json.dumps(self, cls=AggressiveEncoder, encoding='utf-8')
class DataGatewayClient(object):
def __init__(self, address):
self._address = address
self._ios = HTTPIOStream(addr=address)
def reconnect(self):
self.close()
self._ios.open(self._address)
def close(self):
self._ios.close()
def _compose_message(self, key, data, storage):
req = HTTPRequest(method='PUT')
req.add(("Content-Type", "application/json"))
req.body = DataGatewayJSONRequest(key, data, storage).getstr()
req.add(("Content-Length", len(req.body)))
return req
def push(self, key, data, storage):
self._ios.write_message(self._compose_message(key, data, storage))
resp = self._ios.read_response()
return resp.code, resp.body
def is_connected(self):
return self._ios.is_open()