root / astakosclient / astakosclient / __init__.py @ 8975f6f6
History | View | Annotate | Download (22 kB)
1 |
# Copyright (C) 2012, 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
import logging |
35 |
import urlparse |
36 |
import urllib |
37 |
import hashlib |
38 |
from copy import copy |
39 |
|
40 |
import simplejson |
41 |
from astakosclient.utils import \ |
42 |
retry, scheme_to_class, parse_request, check_input |
43 |
from astakosclient.errors import \ |
44 |
AstakosClientException, Unauthorized, BadRequest, NotFound, Forbidden, \ |
45 |
NoUserName, NoUUID, BadValue, QuotaLimit, InvalidResponse |
46 |
from .keypath import get_path |
47 |
from .services import astakos_services |
48 |
|
49 |
|
50 |
# Customize astakos_services here?
|
51 |
|
52 |
|
53 |
def join_urls(a, b): |
54 |
"""join_urls from synnefo.lib"""
|
55 |
return a.rstrip("/") + "/" + b.lstrip("/") |
56 |
|
57 |
# --------------------------------------------------------------------
|
58 |
# Astakos API urls
|
59 |
UI_PREFIX = get_path(astakos_services, 'astakos_ui.prefix')
|
60 |
ACCOUNTS_PREFIX = get_path(astakos_services, 'astakos_account.prefix')
|
61 |
ACCOUNTS_PREFIX = join_urls(ACCOUNTS_PREFIX, 'v1.0')
|
62 |
API_AUTHENTICATE = join_urls(ACCOUNTS_PREFIX, "authenticate")
|
63 |
API_USERCATALOGS = join_urls(ACCOUNTS_PREFIX, "user_catalogs")
|
64 |
API_SERVICE_USERCATALOGS = join_urls(ACCOUNTS_PREFIX, "service/user_catalogs")
|
65 |
API_GETSERVICES = join_urls(UI_PREFIX, "get_services")
|
66 |
API_RESOURCES = join_urls(ACCOUNTS_PREFIX, "resources")
|
67 |
API_QUOTAS = join_urls(ACCOUNTS_PREFIX, "quotas")
|
68 |
API_SERVICE_QUOTAS = join_urls(ACCOUNTS_PREFIX, "service_quotas")
|
69 |
API_COMMISSIONS = join_urls(ACCOUNTS_PREFIX, "commissions")
|
70 |
API_COMMISSIONS_ACTION = join_urls(API_COMMISSIONS, "action")
|
71 |
API_FEEDBACK = join_urls(ACCOUNTS_PREFIX, "feedback")
|
72 |
|
73 |
# --------------------------------------------------------------------
|
74 |
# Astakos Keystone API urls
|
75 |
IDENTITY_PREFIX = get_path(astakos_services, 'astakos_identity.prefix')
|
76 |
IDENTITY_PREFIX = join_urls(IDENTITY_PREFIX, "v2.0")
|
77 |
API_TOKENS = join_urls(IDENTITY_PREFIX, "tokens")
|
78 |
|
79 |
|
80 |
# --------------------------------------------------------------------
|
81 |
# Astakos Client Class
|
82 |
|
83 |
def get_token_from_cookie(request, cookie_name): |
84 |
"""Extract token from the cookie name provided
|
85 |
|
86 |
Cookie should be in the same form as astakos
|
87 |
service sets its cookie contents:
|
88 |
<user_uniq>|<user_token>
|
89 |
|
90 |
"""
|
91 |
try:
|
92 |
cookie_content = urllib.unquote(request.COOKIE.get(cookie_name, None))
|
93 |
return cookie_content.split("|")[1] |
94 |
except:
|
95 |
return None |
96 |
|
97 |
|
98 |
class AstakosClient(): |
99 |
"""AstakosClient Class Implementation"""
|
100 |
|
101 |
# ----------------------------------
|
102 |
def __init__(self, astakos_url, retry=0, |
103 |
use_pool=False, pool_size=8, logger=None): |
104 |
"""Initialize AstakosClient Class
|
105 |
|
106 |
Keyword arguments:
|
107 |
astakos_url -- i.e https://accounts.example.com (string)
|
108 |
use_pool -- use objpool for http requests (boolean)
|
109 |
retry -- how many time to retry (integer)
|
110 |
logger -- pass a different logger
|
111 |
|
112 |
"""
|
113 |
if logger is None: |
114 |
logging.basicConfig( |
115 |
format='%(asctime)s [%(levelname)s] %(name)s %(message)s',
|
116 |
datefmt='%Y-%m-%d %H:%M:%S',
|
117 |
level=logging.INFO) |
118 |
logger = logging.getLogger("astakosclient")
|
119 |
logger.debug("Intialize AstakosClient: astakos_url = %s, "
|
120 |
"use_pool = %s" % (astakos_url, use_pool))
|
121 |
|
122 |
check_input("__init__", logger, astakos_url=astakos_url)
|
123 |
|
124 |
# Check for supported scheme
|
125 |
p = urlparse.urlparse(astakos_url) |
126 |
conn_class = scheme_to_class(p.scheme, use_pool, pool_size) |
127 |
if conn_class is None: |
128 |
m = "Unsupported scheme: %s" % p.scheme
|
129 |
logger.error(m) |
130 |
raise BadValue(m)
|
131 |
|
132 |
# Save astakos_url etc. in our class
|
133 |
self.retry = retry
|
134 |
self.logger = logger
|
135 |
self.netloc = p.netloc
|
136 |
self.scheme = p.scheme
|
137 |
self.path = p.path.rstrip('/') |
138 |
self.conn_class = conn_class
|
139 |
|
140 |
# ----------------------------------
|
141 |
@retry
|
142 |
def _call_astakos(self, token, request_path, headers=None, |
143 |
body=None, method="GET", log_body=True): |
144 |
"""Make the actual call to Astakos Service"""
|
145 |
if token is not None: |
146 |
hashed_token = hashlib.sha1() |
147 |
hashed_token.update(token) |
148 |
using_token = "using token %s" % (hashed_token.hexdigest())
|
149 |
else:
|
150 |
using_token = "without using token"
|
151 |
self.logger.debug(
|
152 |
"Make a %s request to %s %s with headers %s and body %s"
|
153 |
% (method, request_path, using_token, headers, |
154 |
body if log_body else "(not logged)")) |
155 |
|
156 |
# Check Input
|
157 |
if headers is None: |
158 |
headers = {} |
159 |
if body is None: |
160 |
body = {} |
161 |
path = self.path + "/" + request_path.strip('/') |
162 |
|
163 |
# Build request's header and body
|
164 |
kwargs = {} |
165 |
kwargs['headers'] = copy(headers)
|
166 |
if token is not None: |
167 |
kwargs['headers']['X-Auth-Token'] = token |
168 |
if body:
|
169 |
kwargs['body'] = copy(body)
|
170 |
kwargs['headers'].setdefault(
|
171 |
'content-type', 'application/octet-stream') |
172 |
kwargs['headers'].setdefault('content-length', |
173 |
len(body) if body else 0) |
174 |
|
175 |
try:
|
176 |
# Get the connection object
|
177 |
with self.conn_class(self.netloc) as conn: |
178 |
# Send request
|
179 |
(message, data, status) = \ |
180 |
_do_request(conn, method, path, **kwargs) |
181 |
except Exception as err: |
182 |
self.logger.error("Failed to send request: %s" % repr(err)) |
183 |
raise AstakosClientException(str(err)) |
184 |
|
185 |
# Return
|
186 |
self.logger.debug("Request returned with status %s" % status) |
187 |
if status == 400: |
188 |
raise BadRequest(message, data)
|
189 |
elif status == 401: |
190 |
raise Unauthorized(message, data)
|
191 |
elif status == 403: |
192 |
raise Forbidden(message, data)
|
193 |
elif status == 404: |
194 |
raise NotFound(message, data)
|
195 |
elif status < 200 or status >= 300: |
196 |
raise AstakosClientException(message, data, status)
|
197 |
|
198 |
try:
|
199 |
if data:
|
200 |
return simplejson.loads(unicode(data)) |
201 |
else:
|
202 |
return None |
203 |
except Exception as err: |
204 |
msg = "Cannot parse response \"%s\" with simplejson: %s"
|
205 |
self.logger.error(msg % (data, str(err))) |
206 |
raise InvalidResponse(str(err), data) |
207 |
|
208 |
# ------------------------
|
209 |
# do a GET to ``API_AUTHENTICATE``
|
210 |
def get_user_info(self, token, usage=False): |
211 |
"""Authenticate user and get user's info as a dictionary
|
212 |
|
213 |
Keyword arguments:
|
214 |
token -- user's token (string)
|
215 |
usage -- return usage information for user (boolean)
|
216 |
|
217 |
In case of success return user information (json parsed format).
|
218 |
Otherwise raise an AstakosClientException.
|
219 |
|
220 |
"""
|
221 |
# Send request
|
222 |
auth_path = copy(API_AUTHENTICATE) |
223 |
if usage:
|
224 |
auth_path += "?usage=1"
|
225 |
return self._call_astakos(token, auth_path) |
226 |
|
227 |
# ----------------------------------
|
228 |
# do a POST to ``API_USERCATALOGS`` (or ``API_SERVICE_USERCATALOGS``)
|
229 |
# with {'uuids': uuids}
|
230 |
def _uuid_catalog(self, token, uuids, req_path): |
231 |
req_headers = {'content-type': 'application/json'} |
232 |
req_body = parse_request({'uuids': uuids}, self.logger) |
233 |
data = self._call_astakos(
|
234 |
token, req_path, req_headers, req_body, "POST")
|
235 |
if "uuid_catalog" in data: |
236 |
return data.get("uuid_catalog") |
237 |
else:
|
238 |
m = "_uuid_catalog request returned %s. No uuid_catalog found" \
|
239 |
% data |
240 |
self.logger.error(m)
|
241 |
raise AstakosClientException(m)
|
242 |
|
243 |
def get_usernames(self, token, uuids): |
244 |
"""Return a uuid_catalog dictionary for the given uuids
|
245 |
|
246 |
Keyword arguments:
|
247 |
token -- user's token (string)
|
248 |
uuids -- list of user ids (list of strings)
|
249 |
|
250 |
The returned uuid_catalog is a dictionary with uuids as
|
251 |
keys and the corresponding user names as values
|
252 |
|
253 |
"""
|
254 |
req_path = copy(API_USERCATALOGS) |
255 |
return self._uuid_catalog(token, uuids, req_path) |
256 |
|
257 |
def get_username(self, token, uuid): |
258 |
"""Return the user name of a uuid (see get_usernames)"""
|
259 |
check_input("get_username", self.logger, uuid=uuid) |
260 |
uuid_dict = self.get_usernames(token, [uuid])
|
261 |
if uuid in uuid_dict: |
262 |
return uuid_dict.get(uuid)
|
263 |
else:
|
264 |
raise NoUserName(uuid)
|
265 |
|
266 |
def service_get_usernames(self, token, uuids): |
267 |
"""Return a uuid_catalog dict using a service's token"""
|
268 |
req_path = copy(API_SERVICE_USERCATALOGS) |
269 |
return self._uuid_catalog(token, uuids, req_path) |
270 |
|
271 |
def service_get_username(self, token, uuid): |
272 |
"""Return the displayName of a uuid using a service's token"""
|
273 |
check_input("service_get_username", self.logger, uuid=uuid) |
274 |
uuid_dict = self.service_get_usernames(token, [uuid])
|
275 |
if uuid in uuid_dict: |
276 |
return uuid_dict.get(uuid)
|
277 |
else:
|
278 |
raise NoUserName(uuid)
|
279 |
|
280 |
# ----------------------------------
|
281 |
# do a POST to ``API_USERCATALOGS`` (or ``API_SERVICE_USERCATALOGS``)
|
282 |
# with {'displaynames': display_names}
|
283 |
def _displayname_catalog(self, token, display_names, req_path): |
284 |
req_headers = {'content-type': 'application/json'} |
285 |
req_body = parse_request({'displaynames': display_names}, self.logger) |
286 |
data = self._call_astakos(
|
287 |
token, req_path, req_headers, req_body, "POST")
|
288 |
if "displayname_catalog" in data: |
289 |
return data.get("displayname_catalog") |
290 |
else:
|
291 |
m = "_displayname_catalog request returned %s. " \
|
292 |
"No displayname_catalog found" % data
|
293 |
self.logger.error(m)
|
294 |
raise AstakosClientException(m)
|
295 |
|
296 |
def get_uuids(self, token, display_names): |
297 |
"""Return a displayname_catalog for the given names
|
298 |
|
299 |
Keyword arguments:
|
300 |
token -- user's token (string)
|
301 |
display_names -- list of user names (list of strings)
|
302 |
|
303 |
The returned displayname_catalog is a dictionary with
|
304 |
the names as keys and the corresponding uuids as values
|
305 |
|
306 |
"""
|
307 |
req_path = copy(API_USERCATALOGS) |
308 |
return self._displayname_catalog(token, display_names, req_path) |
309 |
|
310 |
def get_uuid(self, token, display_name): |
311 |
"""Return the uuid of a name (see getUUIDs)"""
|
312 |
check_input("get_uuid", self.logger, display_name=display_name) |
313 |
name_dict = self.get_uuids(token, [display_name])
|
314 |
if display_name in name_dict: |
315 |
return name_dict.get(display_name)
|
316 |
else:
|
317 |
raise NoUUID(display_name)
|
318 |
|
319 |
def service_get_uuids(self, token, display_names): |
320 |
"""Return a display_name catalog using a service's token"""
|
321 |
req_path = copy(API_SERVICE_USERCATALOGS) |
322 |
return self._displayname_catalog(token, display_names, req_path) |
323 |
|
324 |
def service_get_uuid(self, token, display_name): |
325 |
"""Return the uuid of a name using a service's token"""
|
326 |
check_input("service_get_uuid", self.logger, display_name=display_name) |
327 |
name_dict = self.service_get_uuids(token, [display_name])
|
328 |
if display_name in name_dict: |
329 |
return name_dict.get(display_name)
|
330 |
else:
|
331 |
raise NoUUID(display_name)
|
332 |
|
333 |
# ----------------------------------
|
334 |
# do a GET to ``API_GETSERVICES``
|
335 |
def get_services(self): |
336 |
"""Return a list of dicts with the registered services"""
|
337 |
return self._call_astakos(None, copy(API_GETSERVICES)) |
338 |
|
339 |
# ----------------------------------
|
340 |
# do a GET to ``API_RESOURCES``
|
341 |
def get_resources(self): |
342 |
"""Return a dict of dicts with the available resources"""
|
343 |
return self._call_astakos(None, copy(API_RESOURCES)) |
344 |
|
345 |
# ----------------------------------
|
346 |
# do a POST to ``API_FEEDBACK``
|
347 |
def send_feedback(self, token, message, data): |
348 |
"""Send feedback to astakos service
|
349 |
|
350 |
keyword arguments:
|
351 |
token -- user's token (string)
|
352 |
message -- Feedback message
|
353 |
data -- Additional information about service client status
|
354 |
|
355 |
In case of success return nothing.
|
356 |
Otherwise raise an AstakosClientException
|
357 |
|
358 |
"""
|
359 |
check_input("send_feedback", self.logger, message=message, data=data) |
360 |
path = copy(API_FEEDBACK) |
361 |
req_body = urllib.urlencode( |
362 |
{'feedback_msg': message, 'feedback_data': data}) |
363 |
self._call_astakos(token, path, None, req_body, "POST") |
364 |
|
365 |
# ----------------------------------
|
366 |
# do a POST to ``API_TOKENS``
|
367 |
def get_endpoints(self, token, uuid=None): |
368 |
""" Fallback call for authenticate
|
369 |
|
370 |
Keyword arguments:
|
371 |
token -- user's token (string)
|
372 |
uuid -- user's uniq id
|
373 |
|
374 |
It returns back the token as well as information about the token
|
375 |
holder and the services he/she can acess (in json format).
|
376 |
In case of error raise an AstakosClientException.
|
377 |
|
378 |
"""
|
379 |
req_path = copy(API_TOKENS) |
380 |
req_headers = {'content-type': 'application/json'} |
381 |
body = {'auth': {'token': {'id': token}}} |
382 |
if uuid is not None: |
383 |
body['auth']['tenantName'] = uuid |
384 |
req_body = parse_request(body, self.logger)
|
385 |
return self._call_astakos(token, req_path, req_headers, |
386 |
req_body, "POST", False) |
387 |
|
388 |
# ----------------------------------
|
389 |
# do a GET to ``API_QUOTAS``
|
390 |
def get_quotas(self, token): |
391 |
"""Get user's quotas
|
392 |
|
393 |
Keyword arguments:
|
394 |
token -- user's token (string)
|
395 |
|
396 |
In case of success return a dict of dicts with user's current quotas.
|
397 |
Otherwise raise an AstakosClientException
|
398 |
|
399 |
"""
|
400 |
return self._call_astakos(token, copy(API_QUOTAS)) |
401 |
|
402 |
# ----------------------------------
|
403 |
# do a GET to ``API_SERVICE_QUOTAS``
|
404 |
def service_get_quotas(self, token, user=None): |
405 |
"""Get all quotas for resources associated with the service
|
406 |
|
407 |
Keyword arguments:
|
408 |
token -- service's token (string)
|
409 |
user -- optionally, the uuid of a specific user
|
410 |
|
411 |
In case of success return a dict of dicts of dicts with current quotas
|
412 |
for all users, or of a specified user, if user argument is set.
|
413 |
Otherwise raise an AstakosClientException
|
414 |
|
415 |
"""
|
416 |
query = copy(API_SERVICE_QUOTAS) |
417 |
if user is not None: |
418 |
query += "?user=" + user
|
419 |
return self._call_astakos(token, query) |
420 |
|
421 |
# ----------------------------------
|
422 |
# do a POST to ``API_COMMISSIONS``
|
423 |
def issue_commission(self, token, request): |
424 |
"""Issue a commission
|
425 |
|
426 |
Keyword arguments:
|
427 |
token -- service's token (string)
|
428 |
request -- commision request (dict)
|
429 |
|
430 |
In case of success return commission's id (int).
|
431 |
Otherwise raise an AstakosClientException.
|
432 |
|
433 |
"""
|
434 |
req_headers = {'content-type': 'application/json'} |
435 |
req_body = parse_request(request, self.logger)
|
436 |
try:
|
437 |
response = self._call_astakos(token, copy(API_COMMISSIONS),
|
438 |
req_headers, req_body, "POST")
|
439 |
except AstakosClientException as err: |
440 |
if err.status == 413: |
441 |
raise QuotaLimit(err.message, err.details)
|
442 |
else:
|
443 |
raise
|
444 |
|
445 |
if "serial" in response: |
446 |
return response['serial'] |
447 |
else:
|
448 |
m = "issue_commission_core request returned %s. No serial found" \
|
449 |
% response |
450 |
self.logger.error(m)
|
451 |
raise AstakosClientException(m)
|
452 |
|
453 |
def issue_one_commission(self, token, holder, source, provisions, |
454 |
name="", force=False, auto_accept=False): |
455 |
"""Issue one commission (with specific holder and source)
|
456 |
|
457 |
keyword arguments:
|
458 |
token -- service's token (string)
|
459 |
holder -- user's id (string)
|
460 |
source -- commission's source (ex system) (string)
|
461 |
provisions -- resources with their quantity (dict from string to int)
|
462 |
name -- description of the commission (string)
|
463 |
force -- force this commission (boolean)
|
464 |
auto_accept -- auto accept this commission (boolean)
|
465 |
|
466 |
In case of success return commission's id (int).
|
467 |
Otherwise raise an AstakosClientException.
|
468 |
(See also issue_commission)
|
469 |
|
470 |
"""
|
471 |
check_input("issue_one_commission", self.logger, |
472 |
holder=holder, source=source, |
473 |
provisions=provisions) |
474 |
|
475 |
request = {} |
476 |
request["force"] = force
|
477 |
request["auto_accept"] = auto_accept
|
478 |
request["name"] = name
|
479 |
try:
|
480 |
request["provisions"] = []
|
481 |
for resource, quantity in provisions.iteritems(): |
482 |
t = {"holder": holder, "source": source, |
483 |
"resource": resource, "quantity": quantity} |
484 |
request["provisions"].append(t)
|
485 |
except Exception as err: |
486 |
self.logger.error(str(err)) |
487 |
raise BadValue(str(err)) |
488 |
|
489 |
return self.issue_commission(token, request) |
490 |
|
491 |
# ----------------------------------
|
492 |
# do a GET to ``API_COMMISSIONS``
|
493 |
def get_pending_commissions(self, token): |
494 |
"""Get Pending Commissions
|
495 |
|
496 |
Keyword arguments:
|
497 |
token -- service's token (string)
|
498 |
|
499 |
In case of success return a list of pending commissions' ids
|
500 |
(list of integers)
|
501 |
|
502 |
"""
|
503 |
return self._call_astakos(token, copy(API_COMMISSIONS)) |
504 |
|
505 |
# ----------------------------------
|
506 |
# do a GET to ``API_COMMISSIONS``/<serial>
|
507 |
def get_commission_info(self, token, serial): |
508 |
"""Get Description of a Commission
|
509 |
|
510 |
Keyword arguments:
|
511 |
token -- service's token (string)
|
512 |
serial -- commission's id (int)
|
513 |
|
514 |
In case of success return a dict of dicts containing
|
515 |
informations (details) about the requested commission
|
516 |
|
517 |
"""
|
518 |
check_input("get_commission_info", self.logger, serial=serial) |
519 |
|
520 |
path = API_COMMISSIONS + "/" + str(serial) |
521 |
return self._call_astakos(token, path) |
522 |
|
523 |
# ----------------------------------
|
524 |
# do a POST to ``API_COMMISSIONS``/<serial>/action"
|
525 |
def commission_action(self, token, serial, action): |
526 |
"""Perform a commission action
|
527 |
|
528 |
Keyword arguments:
|
529 |
token -- service's token (string)
|
530 |
serial -- commission's id (int)
|
531 |
action -- action to perform, currently accept/reject (string)
|
532 |
|
533 |
In case of success return nothing.
|
534 |
|
535 |
"""
|
536 |
check_input("commission_action", self.logger, |
537 |
serial=serial, action=action) |
538 |
|
539 |
path = API_COMMISSIONS + "/" + str(serial) + "/action" |
540 |
req_headers = {'content-type': 'application/json'} |
541 |
req_body = parse_request({str(action): ""}, self.logger) |
542 |
self._call_astakos(token, path, req_headers, req_body, "POST") |
543 |
|
544 |
def accept_commission(self, token, serial): |
545 |
"""Accept a commission (see commission_action)"""
|
546 |
self.commission_action(token, serial, "accept") |
547 |
|
548 |
def reject_commission(self, token, serial): |
549 |
"""Reject a commission (see commission_action)"""
|
550 |
self.commission_action(token, serial, "reject") |
551 |
|
552 |
# ----------------------------------
|
553 |
# do a POST to ``API_COMMISSIONS_ACTION``
|
554 |
def resolve_commissions(self, token, accept_serials, reject_serials): |
555 |
"""Resolve multiple commissions at once
|
556 |
|
557 |
Keyword arguments:
|
558 |
token -- service's token (string)
|
559 |
accept_serials -- commissions to accept (list of ints)
|
560 |
reject_serials -- commissions to reject (list of ints)
|
561 |
|
562 |
In case of success return a dict of dicts describing which
|
563 |
commissions accepted, which rejected and which failed to
|
564 |
resolved.
|
565 |
|
566 |
"""
|
567 |
check_input("resolve_commissions", self.logger, |
568 |
accept_serials=accept_serials, |
569 |
reject_serials=reject_serials) |
570 |
|
571 |
path = copy(API_COMMISSIONS_ACTION) |
572 |
req_headers = {'content-type': 'application/json'} |
573 |
req_body = parse_request({"accept": accept_serials,
|
574 |
"reject": reject_serials},
|
575 |
self.logger)
|
576 |
return self._call_astakos(token, path, req_headers, req_body, "POST") |
577 |
|
578 |
|
579 |
# --------------------------------------------------------------------
|
580 |
# Private functions
|
581 |
# We want _doRequest to be a distinct function
|
582 |
# so that we can replace it during unit tests.
|
583 |
def _do_request(conn, method, url, **kwargs): |
584 |
"""The actual request. This function can easily be mocked"""
|
585 |
conn.request(method, url, **kwargs) |
586 |
response = conn.getresponse() |
587 |
length = response.getheader('content-length', None) |
588 |
data = response.read(length) |
589 |
status = int(response.status)
|
590 |
message = response.reason |
591 |
return (message, data, status)
|