Revision d7c850a0 snf-pithos-app/pithos/api/delegate.py
b/snf-pithos-app/pithos/api/delegate.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
import logging |
35 | 35 |
|
36 |
from urlparse import urlparse
|
|
36 |
import urlparse |
|
37 | 37 |
|
38 | 38 |
from django.http import ( |
39 | 39 |
HttpResponseNotFound, HttpResponseRedirect, HttpResponse) |
40 | 40 |
from django.utils.http import urlencode |
41 | 41 |
from django.views.decorators.csrf import csrf_exempt |
42 |
from django.utils import simplejson as json |
|
42 | 43 |
|
43 | 44 |
from pithos.api.settings import ASTAKOS_URL |
44 | 45 |
|
45 |
from objpool.http import PooledHTTPConnection |
|
46 |
from astakosclient import AstakosClient |
|
47 |
from astakosclient.errors import AstakosClientException |
|
46 | 48 |
|
47 | 49 |
logger = logging.getLogger(__name__) |
48 | 50 |
|
49 |
USER_CATALOG_URL = urlparse.urljoin(ASTAKOS_URL, "astakos/api/user_catalogs") |
|
50 |
USER_FEEDBACK_URL = urlparse.urljoin(ASTAKOS_URL, "astakos/api/feedback") |
|
51 |
USER_LOGIN_URL = urljoin(ASTAKOS_URL, "login") |
|
51 |
USER_CATALOG_URL = "/astakos/api/user_catalogs" |
|
52 |
USER_FEEDBACK_URL = "/astakos/api/feedback" |
|
53 |
USER_LOGIN_URL = urlparse.urljoin(ASTAKOS_URL, "login") |
|
54 |
|
|
52 | 55 |
|
53 | 56 |
def delegate_to_login_service(request): |
54 | 57 |
url = USER_LOGIN_URL |
55 | 58 |
if not url: |
56 | 59 |
return HttpResponseNotFound() |
57 | 60 |
|
58 |
p = urlparse(url) |
|
61 |
p = urlparse.urlparse(url)
|
|
59 | 62 |
if request.is_secure(): |
60 | 63 |
proto = 'https://' |
61 | 64 |
else: |
... | ... | |
65 | 68 |
return HttpResponseRedirect(uri) |
66 | 69 |
|
67 | 70 |
|
68 |
def proxy(request, url, headers=None, body=None): |
|
69 |
p = urlparse(url) |
|
70 |
|
|
71 |
kwargs = {} |
|
72 |
if headers is None: |
|
73 |
headers = {} |
|
74 |
kwargs["headers"] = headers |
|
75 |
kwargs['headers'].update(request.META) |
|
76 |
kwargs['body'] = body |
|
77 |
kwargs['headers'].setdefault('content-type', 'application/json') |
|
78 |
kwargs['headers'].setdefault('content-length', len(body) if body else 0) |
|
79 |
|
|
80 |
with PooledHTTPConnection(p.netloc, p.scheme) as conn: |
|
81 |
conn.request(request.method, p.path + '?' + p.query, **kwargs) |
|
82 |
response = conn.getresponse() |
|
83 |
length = response.getheader('content-length', None) |
|
84 |
data = response.read(length) |
|
85 |
status = int(response.status) |
|
86 |
return HttpResponse(data, status=status) |
|
87 |
|
|
88 |
|
|
89 | 71 |
@csrf_exempt |
90 | 72 |
def delegate_to_feedback_service(request): |
91 | 73 |
token = request.META.get('HTTP_X_AUTH_TOKEN') |
92 |
headers = {'X-Auth-Token': token} |
|
93 |
return proxy( |
|
94 |
request, USER_FEEDBACK_URL, headers=headers, body=request.raw_post_data) |
|
74 |
body = request.raw_post_data |
|
75 |
method = request.method |
|
76 |
astakos = AstakosClient(ASTAKOS_URL, retry=2, use_pool=True, logger=logger) |
|
77 |
try: |
|
78 |
data = astakos._call_astakos(token, USER_FEEDBACK_URL, None, body, |
|
79 |
method) |
|
80 |
status = 200 |
|
81 |
except AstakosClientException, e: |
|
82 |
status = e.status |
|
83 |
details = json.loads(e.details) |
|
84 |
_, d = details.popitem() |
|
85 |
data = d.get('message') |
|
86 |
return HttpResponse(data, status=status) |
|
95 | 87 |
|
96 | 88 |
|
97 | 89 |
@csrf_exempt |
98 | 90 |
def delegate_to_user_catalogs_service(request): |
99 | 91 |
token = request.META.get('HTTP_X_AUTH_TOKEN') |
100 |
headers = {'X-Auth-Token': token, 'content-type': 'application/json'} |
|
101 |
return proxy( |
|
102 |
request, USER_CATALOG_URL, headers=headers, body=request.raw_post_data) |
|
92 |
headers = {'content-type': 'application/json'} |
|
93 |
body = request.raw_post_data |
|
94 |
method = request.method |
|
95 |
astakos = AstakosClient(ASTAKOS_URL, retry=2, use_pool=True, logger=logger) |
|
96 |
try: |
|
97 |
data = astakos._call_astakos(token, USER_CATALOG_URL, headers, body, |
|
98 |
method) |
|
99 |
data = json.dumps(data) |
|
100 |
status = 200 |
|
101 |
except AstakosClientException, e: |
|
102 |
status = e.status |
|
103 |
details = json.loads(e.details) |
|
104 |
_, d = details.popitem() |
|
105 |
data = d.get('message') |
|
106 |
return HttpResponse(data, status=status) |
Also available in: Unified diff