32 |
32 |
# or implied, of GRNET S.A.
|
33 |
33 |
|
34 |
34 |
from logging import getLogger
|
35 |
|
from astakosclient import AstakosClient as SynnefoAstakosClientOrig
|
36 |
|
from astakosclient import AstakosClientException as SynnefoAstakosClientError
|
|
35 |
from astakosclient import AstakosClient
|
|
36 |
from astakosclient import AstakosClientException
|
37 |
37 |
|
38 |
38 |
from kamaki.clients import Client, ClientError, RequestManager, recvlog
|
39 |
39 |
|
... | ... | |
42 |
42 |
def wrap(self, *args, **kwargs):
|
43 |
43 |
try:
|
44 |
44 |
return foo(self, *args, **kwargs)
|
45 |
|
except SynnefoAstakosClientError as sace:
|
|
45 |
except AstakosClientException as sace:
|
46 |
46 |
self._raise_for_status(sace)
|
47 |
47 |
return wrap
|
48 |
48 |
|
49 |
49 |
|
50 |
|
class SynnefoAstakosClient(SynnefoAstakosClientOrig):
|
51 |
|
"""A synnefo astakosclient.AstakosClient wrapper, that logs"""
|
|
50 |
class SynnefoAstakosClient(AstakosClient):
|
|
51 |
"""An astakosclient.AstakosClient wrapper, that logs the way of kamaki"""
|
|
52 |
|
|
53 |
LOG_TOKEN = False
|
|
54 |
LOG_DATA = False
|
52 |
55 |
|
53 |
56 |
def _dump_response(self, request, status, message, data):
|
54 |
57 |
recvlog.info('\n%d %s' % (status, message))
|
55 |
58 |
recvlog.info('data size: %s' % len(data))
|
56 |
|
token = request.headers.get('X-Auth-Token', '')
|
57 |
|
data = data.replace(token, '...') if token else data
|
58 |
|
recvlog.info(data)
|
|
59 |
if not self.LOG_TOKEN:
|
|
60 |
token = request.headers.get('X-Auth-Token', '')
|
|
61 |
if self.LOG_DATA:
|
|
62 |
data = data.replace(token, '...') if token else data
|
|
63 |
if self.LOG_DATA:
|
|
64 |
recvlog.info(data)
|
59 |
65 |
recvlog.info('- - - - - - -')
|
60 |
66 |
|
61 |
67 |
def _call_astakos(self, *args, **kwargs):
|
... | ... | |
69 |
75 |
path=log_request['path'],
|
70 |
76 |
data=log_request.get('body', None),
|
71 |
77 |
headers=log_request.get('headers', dict()))
|
|
78 |
req.LOG_TOKEN, req.LOG_DATA = self.LOG_TOKEN, self.LOG_DATA
|
72 |
79 |
req.dump_log()
|
73 |
80 |
log_response = getattr(self, 'log_response', None)
|
74 |
81 |
if log_response:
|
... | ... | |
83 |
90 |
return r
|
84 |
91 |
|
85 |
92 |
|
86 |
|
class AstakosClient(Client):
|
|
93 |
class CachedAstakosClient(Client):
|
87 |
94 |
"""Synnefo Astakos cached client wraper"""
|
88 |
95 |
|
89 |
96 |
@_astakos_error
|
90 |
97 |
def __init__(self, base_url, token=None):
|
91 |
|
super(AstakosClient, self).__init__(base_url, token)
|
|
98 |
super(CachedAstakosClient, self).__init__(base_url, token)
|
92 |
99 |
self._astakos = dict()
|
93 |
100 |
self._uuids = dict()
|
94 |
101 |
self._cache = dict()
|
... | ... | |
115 |
122 |
@_astakos_error
|
116 |
123 |
def authenticate(self, token=None):
|
117 |
124 |
"""Get authentication information and store it in this client
|
118 |
|
As long as the AstakosClient instance is alive, the latest
|
|
125 |
As long as the CachedAstakosClient instance is alive, the latest
|
119 |
126 |
authentication information for this token will be available
|
120 |
127 |
|
121 |
128 |
:param token: (str) custom token to authenticate
|
... | ... | |
123 |
130 |
token = self._resolve_token(token)
|
124 |
131 |
astakos = SynnefoAstakosClient(
|
125 |
132 |
token, self.base_url, logger=getLogger('astakosclient'))
|
|
133 |
astakos.LOG_TOKEN = getattr(self, 'LOG_TOKEN', False)
|
|
134 |
astakos.LOG_DATA = getattr(self, 'LOG_DATA', False)
|
126 |
135 |
r = astakos.authenticate()
|
127 |
136 |
uuid = r['access']['user']['id']
|
128 |
137 |
self._uuids[token] = uuid
|