Revision 27cfa807 snf-common/synnefo/lib/astakos.py
b/snf-common/synnefo/lib/astakos.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
from time import time, mktime |
35 | 35 |
from urlparse import urlparse |
36 |
from httplib import HTTPConnection, HTTPSConnection |
|
37 | 36 |
from urllib import quote, unquote |
38 | 37 |
|
39 | 38 |
from django.conf import settings |
40 | 39 |
from django.utils import simplejson as json |
41 | 40 |
|
41 |
from synnefo.lib.pool.http import get_http_connection |
|
42 |
|
|
42 | 43 |
|
43 | 44 |
def authenticate(token, authentication_url='http://127.0.0.1:8000/im/authenticate'): |
44 | 45 |
p = urlparse(authentication_url) |
45 |
if p.scheme == 'http': |
|
46 |
conn = HTTPConnection(p.netloc) |
|
47 |
elif p.scheme == 'https': |
|
48 |
conn = HTTPSConnection(p.netloc) |
|
49 |
else: |
|
50 |
raise Exception('Unknown URL scheme') |
|
51 |
|
|
46 |
|
|
52 | 47 |
kwargs = {} |
53 | 48 |
kwargs['headers'] = {} |
54 | 49 |
kwargs['headers']['X-Auth-Token'] = token |
55 | 50 |
kwargs['headers']['Content-Length'] = 0 |
56 |
|
|
57 |
conn.request('GET', p.path, **kwargs) |
|
58 |
response = conn.getresponse() |
|
59 |
|
|
60 |
headers = response.getheaders() |
|
61 |
headers = dict((unquote(h), unquote(v)) for h,v in headers) |
|
62 |
length = response.getheader('content-length', None) |
|
63 |
data = response.read(length) |
|
64 |
status = int(response.status) |
|
65 |
|
|
51 |
|
|
52 |
conn = get_http_connection(p.netloc, p.scheme) |
|
53 |
try: |
|
54 |
conn.request('GET', p.path, **kwargs) |
|
55 |
response = conn.getresponse() |
|
56 |
headers = response.getheaders() |
|
57 |
headers = dict((unquote(h), unquote(v)) for h,v in headers) |
|
58 |
length = response.getheader('content-length', None) |
|
59 |
data = response.read(length) |
|
60 |
status = int(response.status) |
|
61 |
finally: |
|
62 |
conn.close() |
|
63 |
|
|
66 | 64 |
if status < 200 or status >= 300: |
67 |
raise Exception(data, int(response.status))
|
|
65 |
raise Exception(data, status)
|
|
68 | 66 |
|
69 | 67 |
return json.loads(data) |
70 | 68 |
|
Also available in: Unified diff