Commit c66d6339 authored by Ilias Tsitsimpis's avatar Ilias Tsitsimpis Committed by Christos Stavrakakis

Implement low-level _callAstakos function

This function will handle all requests to Astakos.
On top of this we should build high level wrapper
functions to handle more specific calls/requests
to Astakos. _callAstakos function should log
every single action.
parent 9dd8f240
......@@ -30,3 +30,77 @@
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
import logging
import urlparse
import httplib
import simplejson
import objpool.http
logger = logging.getLogger(__name__)
# --------------------------------------------------------------------
# Private functions
def _scheme_to_class(scheme):
"""Return the appropriate httplib class for given scheme"""
if scheme == "http":
return httplib.HTTPConnection
elif scheme == "https":
return httplib.HTTPSConnection
return None
def _callAstakos(token, url, headers={}, body=None,
method='GET', use_pool=False):
"""Make the actual call to astakos service"""
logger.debug("Make a %s request to %s with token %s, "
"headers %s and body %s, %s using the pool" %
(method, url, token, headers, body,
"not" if not use_pool else ""))
# Build request's header and body
kwargs = {}
kwargs['headers'] = headers
kwargs['headers']['X-Auth-Token'] = token
if body:
kwargs['body'] = body
'content-type', 'application/octet-stream')
kwargs['headers'].setdefault('content-length', len(body) if body else 0)
# Check for supported scheme
p = urlparse.urlparse(url)
connection_class = _scheme_to_class(p.scheme)
if connection_class is None:
m = "Unsupported scheme: %s" % p.scheme
raise ValueError(m)
# Get connection object
if use_pool:
conn = objpool.http.get_http_connection(p.netloc, p.scheme)
conn = connection_class(p.netloc)
# Send request
conn.request(method, p.path + '?' + p.query, **kwargs)
response = conn.getresponse()
length = response.getheader('content-length', None)
data =
status = int(response.status)
except httplib.HTTPException as err:
logger.error("Failed to send request: %s" % err)
# Return
logger.debug("Request returned with status %s" % status)
if status < 200 or status >= 300:
raise Exception(data, status)
return simplejson.loads(unicode(data))
......@@ -59,7 +59,8 @@ CLASSIFIERS = []
# Package requirements
# Provided as an attribute, so you can append to these instead
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment