forked from loopbio/python-motifapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
324 lines (263 loc) · 11.6 KB
/
api.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from __future__ import print_function
import re
import json
import ssl
import socket
import os.path
import subprocess
import logging
from six.moves import urllib, http_client
DEFAULT_HTTP_TIMEOUT = 10 # seconds
# http://code.activestate.com/recipes/577548-https-httplib-client-connection-with-certificate-v/
# http://stackoverflow.com/questions/1875052/using-paired-certificates-with-urllib2
class HTTPSClientAuthHandler(urllib.request.HTTPSHandler):
"""
Allows sending a client certificate with the HTTPS connection.
This version also validates the peer (server) certificate since, well...
"""
def __init__(self, key=None, cert=None, ca_certs=None, ssl_version=None):
urllib.request.HTTPSHandler.__init__(self)
self.key = key
self.cert = cert
self.ca_certs = ca_certs
self.ssl_version = ssl_version
def https_open(self, req):
# Rather than pass in a reference to a connection class, we pass in
# a reference to a function which, for all intents and purposes,
# will behave as a constructor
return self.do_open(self.get_connection, req)
def get_connection(self, host, timeout=DEFAULT_HTTP_TIMEOUT):
return HTTPSConnection(host,
key_file=self.key,
cert_file=self.cert,
timeout=timeout,
ca_certs=self.ca_certs,
ssl_version=self.ssl_version)
class HTTPSConnection(http_client.HTTPSConnection):
"""
Overridden to allow peer certificate validation, configuration
of SSL/ TLS version. See:
http://hg.python.org/cpython/file/c1c45755397b/Lib/httplib.py#l1144
and `ssl.wrap_socket()`
"""
def __init__(self, host, **kwargs):
self.ca_certs = kwargs.pop('ca_certs', None)
self.ssl_version = kwargs.pop('ssl_version', ssl.PROTOCOL_SSLv23)
http_client.HTTPSConnection.__init__(self, host, **kwargs)
def connect(self):
sock = socket.create_connection((self.host, self.port), self.timeout)
if self._tunnel_host:
self.sock = sock
self._tunnel()
self.sock = ssl.wrap_socket(sock,
keyfile=self.key_file,
certfile=self.cert_file,
ca_certs=self.ca_certs,
cert_reqs=ssl.CERT_REQUIRED if self.ca_certs else ssl.CERT_NONE,
ssl_version=self.ssl_version)
class MethodRequest(urllib.request.Request):
# See: https://gist.github.com/logic/2715756
def __init__(self, *args, **kwargs):
if 'method' in kwargs:
self._method = kwargs['method']
del kwargs['method']
else:
self._method = None
return urllib.request.Request.__init__(self, *args, **kwargs)
def get_method(self, *args, **kwargs):
return self._method if self._method is not None else urllib.request.Request.get_method(self, *args, **kwargs)
class MotifError(Exception):
pass
class MotifApiError(MotifError):
def __init__(self, message, code):
self.code = code
Exception.__init__(self, message)
class MotifApi(object):
STREAM_TYPE_IMAGE = 1
STREAM_TYPE_STATE = 2
API = {'version$': 'GET',
'cameras$': 'GET',
'cameras/configure$': 'PATCH',
'cameras/read/(?P<name>[^\s /]+)$': 'GET',
'camera/(?P<serial>[^\s /]+)$': 'GET',
'camera/(?P<serial>[^\s /]+)/configure$': 'PATCH',
'camera/(?P<serial>[^\s /]+)/read/(?P<name>[^\s /]+)$': 'GET',
'camera/(?P<serial>[^\s /]+)/recording/start$': 'POST',
'camera/(?P<serial>[^\s /]+)/recording/stop$': 'POST',
'camera/(?P<serial>[^\s /]+)/recordings$': 'GET',
'camera/(?P<serial>[^\s /]+)/recordings/copy_all$': 'POST',
'camera/(?P<serial>[^\s /]+)/recordings/export_all$': 'POST',
'camera/(?P<serial>[^\s /]+)/io/(?P<name>[^\s /]+)/set': 'POST',
'camera/(?P<serial>[^\s /]+)/io/log': 'POST',
'camera/(?P<serial>[^\s /]+)/io/read': 'GET',
'recording/start$': 'POST',
'recording/stop$': 'POST',
'recordings$': 'GET',
'recordings/copy_all$': 'POST',
'recordings/export_all$': 'POST',
'schedule$': 'GET',
'schedule/clear$': 'POST',
'schedule/(?P<identifier>[^\s /]+)/clear$': 'DELETE',
'schedule/recording/start$': 'POST',
'schedule/camera/(?P<serial>[^\s /]+)/recording/start$': 'POST',
'schedule/recordings/copy_all': 'POST',
'schedule/camera/(?P<serial>[^\s /]+)/recordings/copy_all$': 'POST',
'schedule/recordings/export_all': 'POST',
'schedule/camera/(?P<serial>[^\s /]+)/recordings/export_all$': 'POST',
'schedule/io/(?P<name>[^\s /]+)/set': 'POST',
'schedule/camera/(?P<serial>[^\s /]+)/io/(?P<name>[^\s /]+)/set': 'POST',
'schedule/cameras/configure/(?P<name>[^\s /]+)': 'POST',
'schedule/camera/(?P<serial>[^\s /]+)/configure/(?P<name>[^\s /]+)': 'POST',
'io/(?P<io_serial>[^\s /]+)/(?P<io_port>[^\s /]+)/set': 'POST',
'io/(?P<name>[^\s /]+)/set': 'POST',
'io/log': 'POST',
'io/read': 'GET',
}
def __init__(self, host=None, api_key=None, port=6083, ca_cert=None, api_version=1):
self._log = logging.getLogger('motifapi')
try:
port = int(os.environ.get('MOTIF_PORT', port))
except:
pass
if host is None:
try:
host = os.environ['MOTIF_HOST']
self._log.debug('took host from environment')
except KeyError:
pass
if not host:
host = '127.0.0.1'
if api_key is None:
try:
api_key = os.environ['MOTIF_API_KEY']
self._log.debug('took api_key from environment')
except KeyError:
pass
if not api_key:
try:
api_key = subprocess.check_output(['recnode-apikey']).strip()
self._log.debug('took api-key from recnode-apikey subprocess')
except OSError:
pass
if not api_key:
raise ValueError('API key must be specified')
if ca_cert is None:
ca_cert = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'server.crt')
if not os.path.exists(ca_cert):
raise ValueError('could not find certificate: %s' % ca_cert)
self._prefix = 'api/%d/' % api_version
client_cert_key = None
client_cert_pem = None # file path
handler = HTTPSClientAuthHandler(
key=client_cert_key,
cert=client_cert_pem,
ca_certs=ca_cert,
ssl_version=ssl.PROTOCOL_SSLv23)
self._http = urllib.request.build_opener(handler)
self._host = host
self._api_key = api_key
self._port = port
def _build_request(self, endpoint, data=None, method='GET'):
if endpoint[0] == '/':
endpoint = endpoint[1:]
if endpoint != 'version':
endpoint = self._prefix + endpoint
if data is not None:
try:
data = json.dumps(data).encode("utf-8")
except TypeError:
raise ValueError('Arguments must be JSON serializable (they were %r)' % (data,))
url = 'https://%s:%s/%s' % (self._host, self._port, endpoint)
req = MethodRequest(url, data, method=method)
req.add_header('X-Api-Key', self._api_key)
req.add_header('Content-Type', 'application/json')
self._log.debug('%s %s (%d bytes %s)' % (method,
url,
0 if data is None else len(data),
type(data)))
return req
def _call(self, req):
try:
resp = self._http.open(req)
data = resp.read()
resp.close()
return data
except urllib.error.HTTPError as e:
try:
raw = e.read()
err = json.loads(raw.decode('utf-8'))
exc = MotifApiError(err['error'], err['status_code'])
except Exception:
raise ValueError('unknown API error')
raise exc
except urllib.error.URLError:
raise MotifError('motif not running or reachable')
def call(self, endpoint, method=None, data=None, **kwargs):
meth = ep = None
for _ep, _meth in self.API.items():
if re.match(_ep, endpoint):
meth = _meth
ep = _ep
break
if meth is None:
raise ValueError("unknown endpoint '%s' (are you missing/adding '/')" % endpoint)
req = self._build_request(endpoint,
data=kwargs or None,
method=meth)
out = self._call(req)
if out:
try:
return json.loads(out.decode('utf-8'))
except ValueError as e:
raise ValueError('Invalid JSON response: %s' % e.message)
else:
return {}
def is_recording(self, serial):
r = self.call('camera/%s' % serial)
if r['camera_info'].get('filename'):
return True
elif r['camera_info'].get('status') == 'pending':
return True
return False
def is_copying(self, serial):
r = self.call('camera/%s' % serial)
return r['playback_info']['status'] == 'copying'
def is_exporting(self, serial):
r = self.call('camera/%s' % serial)
status = r['playback_info']['status']
return status.startswith('export') and ('finished' not in status)
def get_stream(self, serial=None, stream_type=STREAM_TYPE_IMAGE, force_host=None):
from .stream import ImageStreamer, StateStreamer
if serial is None:
try:
# get the first camera
for c in self.call('cameras').get('cameras', []):
serial = c['serial']
break
except (urllib.error.URLError, MotifApiError):
raise MotifError('motif not running or reachable')
if serial is None:
raise MotifError('no cameras connected or running')
def _get_host_port(_stream_name):
_stat = self.call('camera/%s' % serial)
_host = _stat['camera_info']['stream'][_stream_name]['host']
_port = int(_stat['camera_info']['stream'][_stream_name]['port'])
if force_host is not None:
_host = force_host
elif (_host == '0.0.0.0') and (self._host != '0.0.0.0'):
_host = self._host
return _host, _port
if stream_type in (MotifApi.STREAM_TYPE_IMAGE, MotifApi.STREAM_TYPE_STATE):
try:
if stream_type == MotifApi.STREAM_TYPE_IMAGE:
host, port = _get_host_port('image')
return ImageStreamer(host, port)
else:
host, port = _get_host_port('state')
return StateStreamer(host, port)
except (urllib.error.URLError, MotifApiError):
raise MotifError('camera with serial %s not found or running' % serial)
except KeyError:
raise MotifError('realtime stream not enabled on camera')
else:
raise ValueError('unknown stream type')