Revision c700f742 snf-common/synnefo/lib/astakos.py
b/snf-common/synnefo/lib/astakos.py | ||
---|---|---|
34 | 34 |
import logging |
35 | 35 |
|
36 | 36 |
from time import time, mktime |
37 |
from urlparse import urlparse |
|
37 |
from urlparse import urlparse, urlsplit, urlunsplit
|
|
38 | 38 |
from urllib import quote, unquote |
39 | 39 |
|
40 | 40 |
from django.conf import settings |
41 | 41 |
from django.utils import simplejson as json |
42 |
from django.utils.http import urlencode |
|
42 | 43 |
|
43 | 44 |
from synnefo.lib.pool.http import get_http_connection |
44 | 45 |
|
45 | 46 |
logger = logging.getLogger(__name__) |
46 | 47 |
|
47 |
def authenticate(token, authentication_url='http://127.0.0.1:8000/im/authenticate'): |
|
48 |
p = urlparse(authentication_url) |
|
48 |
def retry(howmany): |
|
49 |
def execute(func): |
|
50 |
def f(*args, **kwargs): |
|
51 |
attempts = 0 |
|
52 |
while attempts < howmany: |
|
53 |
try: |
|
54 |
return func(*args, **kwargs) |
|
55 |
except Exception, e: |
|
56 |
if e.args: |
|
57 |
status = e.args[-1] |
|
58 |
# In case of Unauthorized response or Not Found return directly |
|
59 |
if status == 401 or status == 404: |
|
60 |
raise e |
|
61 |
attempts += 1 |
|
62 |
return f |
|
63 |
return execute |
|
64 |
|
|
65 |
def call(token, url, headers={}): |
|
66 |
p = urlparse(url) |
|
49 | 67 |
|
50 | 68 |
kwargs = {} |
51 |
kwargs['headers'] = {}
|
|
69 |
kwargs['headers'] = headers
|
|
52 | 70 |
kwargs['headers']['X-Auth-Token'] = token |
53 | 71 |
kwargs['headers']['Content-Length'] = 0 |
54 | 72 |
|
... | ... | |
69 | 87 |
|
70 | 88 |
return json.loads(data) |
71 | 89 |
|
90 |
|
|
91 |
def authenticate(token, authentication_url='http://127.0.0.1:8000/im/authenticate'): |
|
92 |
return call(token, authentication_url) |
|
93 |
|
|
94 |
@retry(3) |
|
95 |
def get_username(token, uuid, url='http://127.0.0.1:8000/im/service/api/v2.0/users'): |
|
96 |
try: |
|
97 |
data = call(token, url, {'X-User-Uuid': uuid}) |
|
98 |
except Exception, e: |
|
99 |
raise e |
|
100 |
else: |
|
101 |
return data.get('username') |
|
102 |
|
|
103 |
|
|
104 |
@retry(3) |
|
105 |
def get_user_uuid(token, username, url='http://127.0.0.1:8000/im/service/api/v2.0/users'): |
|
106 |
try: |
|
107 |
data = call(token, url, {'X-User-Username': username}) |
|
108 |
except Exception, e: |
|
109 |
raise e |
|
110 |
else: |
|
111 |
return data.get('uuid') |
|
112 |
|
|
113 |
|
|
72 | 114 |
def user_for_token(token, authentication_url, override_users): |
73 | 115 |
if not token: |
74 | 116 |
return None |
Also available in: Unified diff