Commit 26c74d43 authored by Ilias Tsitsimpis's avatar Ilias Tsitsimpis Committed by Christos Stavrakakis

Move token argument

Remove token argument from class initialization
method and make it a mandatory argument to all
the other class methods.
parent 5616f53e
......@@ -73,12 +73,10 @@ class AstakosClient():
"""AstakosClient Class Implementation"""
# ----------------------------------
def __init__(self, token, astakos_url,
use_pool=False, retry=0, logger=None):
def __init__(self, astakos_url, use_pool=False, retry=0, logger=None):
"""Intialize AstakosClient Class
Keyword arguments:
token -- user's token (string)
astakos_url -- i.e https://accounts.example.com (string)
use_pool -- use objpool for http requests (boolean)
retry -- how many time to retry (integer)
......@@ -90,11 +88,6 @@ class AstakosClient():
logger.debug("Intialize AstakosClient: astakos_url = %s"
"use_pool = %s" % (astakos_url, use_pool))
# Check Input
if not token:
m = "Token not given"
logger.error(m)
raise ValueError(m)
if not astakos_url:
m = "Astakos url not given"
logger.error(m)
......@@ -108,10 +101,9 @@ class AstakosClient():
logger.error(m)
raise ValueError(m)
# Save token and url
# Save astakos_url etc. in our class
self.retry = retry
self.logger = logger
self.token = token
self.netloc = p.netloc
self.scheme = p.scheme
self.conn = conn
......@@ -136,16 +128,23 @@ class AstakosClient():
# ----------------------------------
@retry
def _callAstakos(self, request_path, headers={}, body={}, method="GET"):
def _callAstakos(self, token, request_path,
headers={}, body={}, method="GET"):
"""Make the actual call to Astakos Service"""
self.logger.debug(
"Make a %s request to %s with headers %s "
"and body %s" % (method, request_path, headers, body))
# Check Input
if not token:
m = "Token not given"
self.logger.error(m)
raise ValueError(m)
# Build request's header and body
kwargs = {}
kwargs['headers'] = headers
kwargs['headers']['X-Auth-Token'] = self.token
kwargs['headers']['X-Auth-Token'] = token
if body:
kwargs['body'] = body
kwargs['headers'].setdefault(
......@@ -172,26 +171,29 @@ class AstakosClient():
return simplejson.loads(unicode(data))
# ------------------------
def authenticate(self, usage=False):
def authenticate(self, token, usage=False):
"""Check if user is authenticated Astakos user
Keyword arguments:
token -- user's token (string)
usage -- return usage information for user (boolean)
In case of success return user information (json parsed format).
Otherwise raise an AstakosClientException.
"""
# Send request
auth_path = "/im/authenticate"
if usage:
auth_path += "?usage=1"
return self._callAstakos(auth_path)
return self._callAstakos(token, auth_path)
# ----------------------------------
def getDisplayNames(self, uuids):
def getDisplayNames(self, token, uuids):
"""Return a uuid_catalog dictionary for the given uuids
Keyword arguments:
token -- user's token (string)
uuids -- list of user ids (list of strings)
The returned uuid_catalog is a dictionary with uuids as
......@@ -202,17 +204,18 @@ class AstakosClient():
req_body = simplejson.dumps({'uuids': uuids})
req_path = "/user_catalogs"
data = self._callAstakos(req_path, req_headers, req_body, "POST")
data = self._callAstakos(
token, req_path, req_headers, req_body, "POST")
# XXX: check if exists
return data.get("uuid_catalog")
def getDisplayName(self, uuid):
def getDisplayName(self, token, uuid):
"""Return the displayName of a uuid (see getDisplayNames)"""
if not uuid:
m = "No uuid was given"
self.logger.error(m)
raise ValueError(m)
uuid_dict = self.getDisplayNames([uuid])
uuid_dict = self.getDisplayNames(token, [uuid])
# XXX: check if exists
return uuid_dict.get(uuid)
......
......@@ -259,9 +259,8 @@ class TestCallAstakos(unittest.TestCase):
global token_1
_mockRequest([_requestOffline])
try:
client = AstakosClient(
token_1, "https://example.com", use_pool=pool)
client._callAstakos("/im/authenticate")
client = AstakosClient("https://example.com", use_pool=pool)
client._callAstakos(token_1, "/im/authenticate")
except AstakosClientException:
pass
else:
......@@ -281,9 +280,8 @@ class TestCallAstakos(unittest.TestCase):
token = "skaksaFlBl+fasFdaf24sx=="
_mockRequest([_requestOk])
try:
client = AstakosClient(
token, "https://example.com", use_pool=pool)
client._callAstakos("/im/authenticate")
client = AstakosClient("https://example.com", use_pool=pool)
client._callAstakos(token, "/im/authenticate")
except AstakosClientException as err:
if err.status != 401:
self.fail("Should have returned 401 (Invalid X-Auth-Token)")
......@@ -304,9 +302,8 @@ class TestCallAstakos(unittest.TestCase):
global token_1
_mockRequest([_requestOk])
try:
client = AstakosClient(
token_1, "https://example.com", use_pool=pool)
client._callAstakos("/im/misspelled")
client = AstakosClient("https://example.com", use_pool=pool)
client._callAstakos(token_1, "/im/misspelled")
except AstakosClientException as err:
if err.status != 404:
self.fail("Should have returned 404 (Not Found)")
......@@ -327,9 +324,8 @@ class TestCallAstakos(unittest.TestCase):
global token_1
_mockRequest([_requestOk])
try:
client = AstakosClient(
token_1, "ftp://example.com", use_pool=pool)
client._callAstakos("/im/authenticate")
client = AstakosClient("ftp://example.com", use_pool=pool)
client._callAstakos(token_1, "/im/authenticate")
except ValueError:
pass
except Exception:
......@@ -351,9 +347,8 @@ class TestCallAstakos(unittest.TestCase):
global token_1
_mockRequest([_requestOk])
try:
client = AstakosClient(
token_1, "http://example.com", use_pool=pool)
client._callAstakos("/im/authenticate")
client = AstakosClient("http://example.com", use_pool=pool)
client._callAstakos(token_1, "/im/authenticate")
except AstakosClientException as err:
if err.status != 302:
self.fail("Should have returned 302 (Found)")
......@@ -374,9 +369,8 @@ class TestCallAstakos(unittest.TestCase):
global token_1
_mockRequest([_requestOk])
try:
client = AstakosClient(
token_1, "https://example.com", use_pool=pool)
client._callAstakos("/im/authenticate", method="POST")
client = AstakosClient("https://example.com", use_pool=pool)
client._callAstakos(token_1, "/im/authenticate", method="POST")
except AstakosClientException as err:
if err.status != 400:
self.fail("Should have returned 400 (Method not allowed)")
......@@ -397,9 +391,8 @@ class TestCallAstakos(unittest.TestCase):
global token_1
_mockRequest([_requestOk])
try:
client = AstakosClient(
token_1, "https://example.com", use_pool=pool)
client._callAstakos("/user_catalogs")
client = AstakosClient("https://example.com", use_pool=pool)
client._callAstakos(token_1, "/user_catalogs")
except AstakosClientException as err:
if err.status != 400:
self.fail("Should have returned 400 (Method not allowed)")
......@@ -425,9 +418,8 @@ class TestAuthenticate(unittest.TestCase):
global token_1
_mockRequest([_requestOffline])
try:
client = AstakosClient(
token_1, "https://example.com", retry=3)
client.authenticate()
client = AstakosClient("https://example.com", retry=3)
client.authenticate(token_1)
except AstakosClientException:
pass
else:
......@@ -439,9 +431,8 @@ class TestAuthenticate(unittest.TestCase):
token = "skaksaFlBl+fasFdaf24sx=="
_mockRequest([_requestOk])
try:
client = AstakosClient(
token, "https://example.com", use_pool=pool)
client.authenticate()
client = AstakosClient("https://example.com", use_pool=pool)
client.authenticate(token)
except AstakosClientException as err:
if err.status != 401:
self.fail("Should have returned 401 (Invalid X-Auth-Token)")
......@@ -461,9 +452,8 @@ class TestAuthenticate(unittest.TestCase):
def _authUser(self, token, user_info, usage, pool):
_mockRequest([_requestOk])
try:
client = AstakosClient(
token, "https://example.com", use_pool=pool)
auth_info = client.authenticate(usage=usage)
client = AstakosClient("https://example.com", use_pool=pool)
auth_info = client.authenticate(token, usage=usage)
except:
self.fail("Shouldn't raise an Exception")
self.assertEqual(user_info, auth_info)
......@@ -509,9 +499,8 @@ class TestAuthenticate(unittest.TestCase):
global token_1, user_1
_mockRequest([_requestOffline, _requestOffline, _requestOk])
try:
client = AstakosClient(
token_1, "https://example.com", retry=2)
auth_info = client.authenticate(usage=True)
client = AstakosClient("https://example.com", retry=2)
auth_info = client.authenticate(token_1, usage=True)
except:
self.fail("Shouldn't raise an Exception")
self.assertEqual(user_1, auth_info)
......@@ -528,8 +517,8 @@ class TestDisplayNames(unittest.TestCase):
token = "skaksaFlBl+fasFdaf24sx=="
_mockRequest([_requestOk])
try:
client = AstakosClient(token, "https://example.com")
client.getDisplayNames([user_1['uuid']])
client = AstakosClient("https://example.com")
client.getDisplayNames(token, [user_1['uuid']])
except AstakosClientException as err:
if err.status != 401:
self.fail("Should have returned 401 (Invalid X-Auth-Token)")
......@@ -543,8 +532,9 @@ class TestDisplayNames(unittest.TestCase):
global token_1, user_1, user_2
_mockRequest([_requestOk])
try:
client = AstakosClient(token_1, "https://example.com")
catalog = client.getDisplayNames([user_1['uuid'], user_2['uuid']])
client = AstakosClient("https://example.com")
catalog = client.getDisplayNames(
token_1, [user_1['uuid'], user_2['uuid']])
except:
self.fail("Shouldn't raise an Exception")
self.assertEqual(catalog[user_1['uuid']], user_1['username'])
......@@ -558,8 +548,8 @@ class TestDisplayNames(unittest.TestCase):
_mockRequest([_requestOffline, _requestOk])
try:
client = AstakosClient(
token_2, "https://example.com", use_pool=True, retry=2)
info = client.getDisplayName(user_1['uuid'])
"https://example.com", use_pool=True, retry=2)
info = client.getDisplayName(token_2, user_1['uuid'])
except:
self.fail("Shouldn't raise an Exception")
self.assertEqual(info, user_1['username'])
......
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