Skip to content

Commit

Permalink
Add getPersonFromDB to ID-service abstraction
Browse files Browse the repository at this point in the history
We add a function 'getPersonFromDB' to the ID-service abstraction
'idManager' to abstract the '/getUser/' interface of the ID service.

This new method enables the user to query a person from the database by
passing the person's ID. This way, the local cache of the idManager
abstraction is bypassed and the full information on the person stored in
the DB is returned (instead of the cached and, thus, short version).

Perform small refactorings to perform the task and some code formatting.

Signed-off-by: Claus Hunsen <[email protected]>
  • Loading branch information
clhunsen committed Jan 16, 2018
1 parent d80f523 commit f83c0a2
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions codeface/cluster/idManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import string
import random


class idManager:
"""Provide unique IDs for developers.
This class provides an interface to the REST id server. Heuristics to
detect developers who operate under multiple identities are included
in the server."""

def __init__(self, dbm, conf):
self.subsys_names = []

Expand Down Expand Up @@ -59,6 +61,11 @@ def __init__(self, dbm, conf):
self._projectID = self._dbm.getProjectID(conf["project"],
conf["tagging"])

# Construct request headers
self.headers = {"Content-type":
"application/x-www-form-urlencoded; charset=utf-8",
"Accept": "text/plain"}

# We need the subsystem names because PersonInfo instances
# are created from this class -- and we want to know in which
# subsystem(s) a developer is active
Expand All @@ -84,14 +91,14 @@ def _decompose_addr(self, addr):
# Replace "Surname, Name" by "Name Surname"
name = "{0} {1}".format(m2.group(2), m2.group(1))

# print "Fixup for addr {0} required -> ({1}/{2})".format(addr, name, email)
# print "Fixup for addr {0} required -> ({1}/{2})".format(addr, name, email)
else:
# In this case, no eMail address was specified.
# print("Fixup for email required, but FAILED for {0}".format(addr))
# print("Fixup for email required, but FAILED for {0}".format(addr))
name = addr
rand_str = "".join(random.choice(string.ascii_lowercase + string.digits)
for i in range(10))
email = "could.not.resolve@"+rand_str
email = "could.not.resolve@" + rand_str

email = email.lower()

Expand All @@ -106,12 +113,9 @@ def _query_user_id(self, name, email):
params = urllib.urlencode({'projectID': self._projectID,
'name': name,
'email': email})
headers = { "Content-type":
"application/x-www-form-urlencoded; charset=utf-8",
"Accept": "text/plain" }

try:
self._conn.request("POST", "/post_user_id", params, headers)
self._conn.request("POST", "/post_user_id", params, self.headers)
res = self._conn.getresponse()
except:
log.exception("Could not reach ID service. Is the server running?\n")
Expand All @@ -125,7 +129,8 @@ def _query_user_id(self, name, email):
id = jsond["id"]
except KeyError:
raise Exception("Bad response from server: '{}'".format(jsond))
return(id)

return (id)

def getPersonID(self, addr):
"""Obtain a unique ID from contributor identity credentials.
Expand All @@ -143,11 +148,26 @@ def getPersonID(self, addr):

# Construct a local instance of PersonInfo for the contributor
# if it is not yet available
if (not(self.persons.has_key(ID))):
if not self.persons.has_key(ID):
self.persons[ID] = PersonInfo(self.subsys_names, ID, name, email)

return ID

def getPersonFromDB(self, person_id):
"""Query the ID database for a contributor and all corresponding data"""

try:
self._conn.request("GET", "/getUser/{}".format(person_id), headers=self.headers)
res = self._conn.getresponse()
except:
log.exception("Could not reach ID service. Is the server running?\n")
raise

result = res.read()
jsond = json.loads(result)[0]

return (jsond)

def getPersons(self):
return self.persons

Expand Down

0 comments on commit f83c0a2

Please sign in to comment.