Commit cf887056 authored by Ilias Tsitsimpis's avatar Ilias Tsitsimpis

astakosclient: Rename functions/methods

Rename functions/methods according to PEP-8 naming conventions.
parent 4ce259a6
......@@ -41,13 +41,13 @@ import simplejson
from astakosclient.utils import retry, scheme_to_class
from astakosclient.errors import \
AstakosClientException, Unauthorized, BadRequest, NotFound, Forbidden, \
NoDisplayName, NoUUID
NoUserName, NoUUID
# --------------------------------------------------------------------
# Astakos Client Class
def getTokenFromCookie(request, cookie_name):
def get_token_from_cookie(request, cookie_name):
"""Extract token from the cookie name provided
Cookie should be in the same form as astakos
......@@ -108,8 +108,8 @@ class AstakosClient():
# ----------------------------------
@retry
def _callAstakos(self, token, request_path,
headers=None, body=None, method="GET"):
def _call_astakos(self, token, request_path,
headers=None, body=None, method="GET"):
"""Make the actual call to Astakos Service"""
hashed_token = hashlib.sha1()
hashed_token.update(token)
......@@ -146,7 +146,7 @@ class AstakosClient():
# Send request
try:
(data, status) = _doRequest(conn, method, request_path, **kwargs)
(data, status) = _do_request(conn, method, request_path, **kwargs)
except Exception as err:
self.logger.error("Failed to send request: %s" % repr(err))
raise AstakosClientException(str(err))
......@@ -168,7 +168,7 @@ class AstakosClient():
return simplejson.loads(unicode(data))
# ------------------------
def getUserInfo(self, token, usage=False):
def get_user_info(self, token, usage=False):
"""Authenticate user and get user's info as a dictionary
Keyword arguments:
......@@ -183,23 +183,23 @@ class AstakosClient():
auth_path = "/im/authenticate"
if usage:
auth_path += "?usage=1"
return self._callAstakos(token, auth_path)
return self._call_astakos(token, auth_path)
# ----------------------------------
def _uuidCatalog(self, token, uuids, req_path):
def _uuid_catalog(self, token, uuids, req_path):
req_headers = {'content-type': 'application/json'}
req_body = simplejson.dumps({'uuids': uuids})
data = self._callAstakos(
data = self._call_astakos(
token, req_path, req_headers, req_body, "POST")
if "uuid_catalog" in data:
return data.get("uuid_catalog")
else:
m = "_uuidCatalog request returned %s. No uuid_catalog found" \
m = "_uuid_catalog request returned %s. No uuid_catalog found" \
% data
self.logger.error(m)
raise AstakosClientException(m)
def getDisplayNames(self, token, uuids):
def get_usernames(self, token, uuids):
"""Return a uuid_catalog dictionary for the given uuids
Keyword arguments:
......@@ -211,52 +211,52 @@ class AstakosClient():
"""
req_path = "/user_catalogs"
return self._uuidCatalog(token, uuids, req_path)
return self._uuid_catalog(token, uuids, req_path)
def getDisplayName(self, token, uuid):
"""Return the displayName of a uuid (see getDisplayNames)"""
def get_username(self, token, uuid):
"""Return the user name of a uuid (see get_usernames)"""
if not uuid:
m = "No uuid was given"
self.logger.error(m)
raise ValueError(m)
uuid_dict = self.getDisplayNames(token, [uuid])
uuid_dict = self.get_usernames(token, [uuid])
if uuid in uuid_dict:
return uuid_dict.get(uuid)
else:
raise NoDisplayName(uuid)
raise NoUserName(uuid)
def getServiceDisplayNames(self, token, uuids):
def service_get_usernames(self, token, uuids):
"""Return a uuid_catalog dict using a service's token"""
req_path = "/service/api/user_catalogs"
return self._uuidCatalog(token, uuids, req_path)
return self._uuid_catalog(token, uuids, req_path)
def getServiceDisplayName(self, token, uuid):
def service_get_username(self, token, uuid):
"""Return the displayName of a uuid using a service's token"""
if not uuid:
m = "No uuid was given"
self.logger.error(m)
raise ValueError(m)
uuid_dict = self.getServiceDisplayNames(token, [uuid])
uuid_dict = self.service_get_usernames(token, [uuid])
if uuid in uuid_dict:
return uuid_dict.get(uuid)
else:
raise NoDisplayName(uuid)
raise NoUserName(uuid)
# ----------------------------------
def _displayNameCatalog(self, token, display_names, req_path):
def _displayname_catalog(self, token, display_names, req_path):
req_headers = {'content-type': 'application/json'}
req_body = simplejson.dumps({'displaynames': display_names})
data = self._callAstakos(
data = self._call_astakos(
token, req_path, req_headers, req_body, "POST")
if "displayname_catalog" in data:
return data.get("displayname_catalog")
else:
m = "_displayNameCatalog request returned %s. " \
m = "_displayname_catalog request returned %s. " \
"No displayname_catalog found" % data
self.logger.error(m)
raise AstakosClientException(m)
def getUUIDs(self, token, display_names):
def get_uuids(self, token, display_names):
"""Return a displayname_catalog for the given names
Keyword arguments:
......@@ -268,48 +268,48 @@ class AstakosClient():
"""
req_path = "/user_catalogs"
return self._displayNameCatalog(token, display_names, req_path)
return self._displayname_catalog(token, display_names, req_path)
def getUUID(self, token, display_name):
def get_uuid(self, token, display_name):
"""Return the uuid of a name (see getUUIDs)"""
if not display_name:
m = "No display_name was given"
self.logger.error(m)
raise ValueError(m)
name_dict = self.getUUIDs(token, [display_name])
name_dict = self.get_uuids(token, [display_name])
if display_name in name_dict:
return name_dict.get(display_name)
else:
raise NoUUID(display_name)
def getServiceUUIDs(self, token, display_names):
def service_get_uuids(self, token, display_names):
"""Return a display_name catalog using a service's token"""
req_path = "/service/api/user_catalogs"
return self._displayNameCatalog(token, display_names, req_path)
return self._displayname_catalog(token, display_names, req_path)
def getServiceUUID(self, token, display_name):
def service_get_uuid(self, token, display_name):
"""Return the uuid of a name using a service's token"""
if not display_name:
m = "No display_name was given"
self.logger.error(m)
raise ValueError(m)
name_dict = self.getServiceUUIDs(token, [display_name])
name_dict = self.service_get_uuids(token, [display_name])
if display_name in name_dict:
return name_dict.get(display_name)
else:
raise NoUUID(display_name)
# ----------------------------------
def getServices(self):
def get_services(self):
"""Return a list of dicts with the registered services"""
return self._callAstakos("dummy token", "/im/get_services")
return self._call_astakos("dummy token", "/im/get_services")
# --------------------------------------------------------------------
# Private functions
# We want _doRequest to be a distinct function
# so that we can replace it during unit tests.
def _doRequest(conn, method, url, **kwargs):
def _do_request(conn, method, url, **kwargs):
"""The actual request. This function can easily be mocked"""
conn.request(method, url, **kwargs)
response = conn.getresponse()
......
......@@ -65,11 +65,11 @@ class NotFound(AstakosClientException):
super(NotFound, self).__init__(message, 404)
class NoDisplayName(AstakosClientException):
class NoUserName(AstakosClientException):
def __init__(self, uuid):
"""No display name for the given uuid"""
message = "No display name for the given uuid: %s" % uuid
super(NoDisplayName, self).__init__(message)
super(NoUserName, self).__init__(message)
class NoUUID(AstakosClientException):
......
This diff is collapsed.
......@@ -34,7 +34,7 @@ demonstrates how to get user's info using ``astakosclient``.
from astakosclient import AstakosClient
client = AstakosClient("https://accounts.example.com")
user_info = client.getUserInfo("UQpYas7ElzWGD5yCcEXtjw==")
user_info = client.get_user_info("UQpYas7ElzWGD5yCcEXtjw==")
print user_info['username']
Another example where we ask for the username of a user with UUID:
......@@ -45,8 +45,8 @@ Another example where we ask for the username of a user with UUID:
from astakosclient import AstakosClient
client = AstakosClient("https://accounts.example.com")
username = client.getDisplayName("UQpYas7ElzWGD5yCcEXtjw==",
"b3de8eb0-3958-477e-als9-789af8dd352c")
username = client.get_username("UQpYas7ElzWGD5yCcEXtjw==",
"b3de8eb0-3958-477e-als9-789af8dd352c")
print username
......@@ -67,58 +67,58 @@ retry=0, use_pool=False, pool_size=8, logger=None\ **)**
This class provides the following methods:
**getUserInfo(**\ token, usage=False\ **)**
**get_user_info(**\ token, usage=False\ **)**
Given a valid authentication token it returns a dict with the
correspoinding user's info. If usage is set to True more
information about user's resources will be returned.
In case of error raise an AstakosClientException exception.
**getDisplayNames(**\ token, uuids\ **)**
**get_usernames(**\ token, uuids\ **)**
Given a valid authentication token and a list of UUIDs
return a uuid_catalog, that is a dictionary with the given
UUIDs as keys and the corresponding user names as values.
Invalid UUIDs will not be in the dictionary.
In case of error raise an AstakosClientException exception.
**getDisplayName(**\ token, uuid\ **)**
**get_username(**\ token, uuid\ **)**
Given a valid authentication token and a UUID (as string)
return the corresponding user name (as string).
In case of invalid UUID raise NoDisplayName exception.
In case of invalid UUID raise NoUserName exception.
In case of error raise an AstakosClientException exception.
**getServiceDisplayNames(**\ token, uuids\ **)**
Same as getDisplayNames but called with a service's token.
**service_get_usernames(**\ token, uuids\ **)**
Same as get_usernames but called with a service's token.
**getServiceDisplayName(**\ token, uuid\ **)**
Same as getDisplayName but called with a service's token.
**service_get_username(**\ token, uuid\ **)**
Same as get_username but called with a service's token.
**getUUIDs(**\ token, display_names\ **)**
**get_uuids(**\ token, display_names\ **)**
Given a valid authentication token and a list of usernames
return a displayname_catalog, that is a dictionary with the given
usernames as keys and the corresponding UUIDs as values.
Invalid usernames will not be in the dictionary.
In case of error raise an AstakosClientException exception.
**getUUID(**\ token, display_name\ **)**
**get_uuid(**\ token, display_name\ **)**
Given a valid authentication token and a username (as string)
return the corresponding UUID (as string).
In case of invalid user name raise NoUUID exception.
In case of error raise an AstakosClientException exception.
**getServiceUUIDs(**\ token, uuids\ **)**
Same as getUUIDs but called with a service's token.
**service_get_uuids(**\ token, uuids\ **)**
Same as get_uuids but called with a service's token.
**getServiceUUID(**\ token, uuid\ **)**
Same as getUUID but called with a service's token.
**service_get_uuid(**\ token, uuid\ **)**
Same as get_uuid but called with a service's token.
**getServices()**
**get_services()**
Return a list of dicts with the registered services.
Public Functions
----------------
**getTokenFromCookie(**\ request, cookie_name\ **)**
**get_token_from_cookie(**\ request, cookie_name\ **)**
Given a Django request object and an Astakos cookie name
extract the user's token from it.
......@@ -144,7 +144,7 @@ Exceptions and Errors
*exception* **NotFound**
The server has not found anything matching the Request-URI. Status 404.
*exception* **NoDisplayName**
*exception* **NoUserName**
Raised by getDisplayName and getServiceDisplayName when an invalid
UUID was given.
......
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