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
|
# Copyright (C) 2018 Lars Wirzenius
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import logging
import requests
class HttpError(Exception):
pass
class HttpAPI:
# Make requests to an HTTP API.
json_type = 'application/json'
def __init__(self):
self._session = requests.Session()
self._token = None
def set_session(self, session):
self._session = session
def set_token(self, token):
self._token = token
def get_dict(self, url, headers=None):
r = self._request(self._session.get, url, headers=headers)
ct = r.headers.get('Content-Type')
if ct != self.json_type:
raise HttpError('Not JSON response')
try:
return r.json()
except json.decoder.JSONDecodeError:
raise HttpError('JSON parsing error')
def get_blob(self, url, headers=None):
r = self._request(self._session.get, url, headers=headers)
return r.content
def post(self, url, headers=None, body=None):
self._send_request(self._session.post, url, headers=headers, body=body)
return None
def put(self, url, headers=None, body=None):
self._send_request(self._session.put, url, headers=headers, body=body)
return None
def _send_request(self, func, url, headers=None, body=None):
if headers is None:
headers = {}
headers = dict(headers)
h, body = self._get_content_type_header(body)
headers.update(h)
self._request(func, url, headers=headers, data=body)
return None
def _get_content_type_header(self, body):
if isinstance(body, dict):
header = {
'Content-Type': 'application/json',
}
body = json.dumps(body)
return header, body
return {}, body
def _get_authorization_headers(self):
return {
'Authorization': 'Bearer {}'.format(self._token),
}
def _request(self, func, url, headers=None, **kwargs):
if headers is None:
headers = {}
headers.update(self._get_authorization_headers())
logging.debug('request: func=%r', func)
logging.debug('request: url=%r', url)
for h in headers:
logging.debug('request: %s: %s', h, headers[h])
logging.debug('request: kwargs=%r', kwargs)
r = func(url, headers=headers, verify=False, **kwargs)
logging.debug('response: status_code=%r', r.status_code)
logging.debug('response: content=%r', r.content)
if not r.ok:
raise HttpError(r.status_code)
return r
class ControllerClient:
def __init__(self):
self._name = None
self._api = HttpAPI()
self._url = None
def set_client_name(self, name):
self._name = name
def set_http_api(self, api):
self._api = api
def set_controller_url(self, url):
self._url = url
def set_token(self, token):
self._api.set_token(token)
def url(self, path):
return '{}{}'.format(self._url, path)
def get_blob_service_url(self):
url = self.url('/version')
version = self._api.get_dict(url)
return version.get('blob_service')
def register(self):
assert self._url is not None
url = self.url('/workers')
logging.info('Registering worker %s to %s', self._name, url)
body = {
'worker': self._name,
}
try:
self._api.post(url, body=body)
except HttpError:
pass
def get_work(self):
url = self.url('/work/{}'.format(self._name))
work = self._api.get_dict(url)
logging.info('Requested work, got: %r', work)
return work
def report_work(self, work):
logging.info('Reporting work: %r', work)
url = self.url('/work')
headers = {
'Content-Type': self._api.json_type,
}
body = json.dumps(work)
self._api.post(url, headers=headers, body=body)
|