From d0fb647c9c3f41e15cdd15c9900a89406ea2ef38 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rn=20K=C3=BCnsem=C3=B6ller?=
 <jkuensem@physik.uni-bielefeld.de>
Date: Thu, 9 Feb 2017 21:47:01 +0000
Subject: [PATCH] Task #9091 - Now functional ID service integration with
 Identifier constructor

---
 lib/query.py  | 72 ++++++++++++++++++++++++++++-----------------------
 lib/siplib.py |  3 +--
 2 files changed, 40 insertions(+), 35 deletions(-)

diff --git a/lib/query.py b/lib/query.py
index bad8531..98ae34c 100644
--- a/lib/query.py
+++ b/lib/query.py
@@ -12,6 +12,9 @@ import copy
 path = expanduser("~/.siplibrc")
 user = None
 passw = None
+host = "lta-ingest-test.lofar.eu:19443"
+#host = "lofar-ingest.target.rug.nl:9443"
+
 with open(path,'r') as file:
         print "Parsing user credentials from",path
         for line in file:
@@ -19,50 +22,49 @@ with open(path,'r') as file:
                 user = line.split('=')[1].strip()
             if line.startswith("password"):
                 passw = line.split('=')[1].strip()
+            if line.startswith("host"):
+                host = line.split('=')[1].strip()
 
 login_data = {
     'j_username' : user,
     'j_password' : passw
     }
 
-url = 'https://'+user+':'+passw+'@lofar-ingest.target.rug.nl:9443/'
+url = 'https://'+user+':'+passw+'@'+host
 client = xmlrpclib.ServerProxy(url)
 
-# todo: comment out this testing dict
-id_cache = {}
-
-
+# id_cache = {}
 
 def _call_idservice(source, userlabel=None):
 
-    # todo: use this query to momqueryservice
-    # if userlabel is not None:
-    # response = client.getUniqueIDForLabel(source, label)
-    # response = client.getUniqueID(source)
-
-    # todo: comment out this mock of the service for testing:
-    if userlabel in id_cache:
-        print "using existing", userlabel
-        response = id_cache.get(userlabel)
+    if userlabel is not None:
+        response = client.GetUniqueIDForLabel(source, userlabel)
     else:
-        print "creating new", userlabel
-        response = {"version": "version",
-                    "result": "ok",
-                    "error_nr": 0,
-                    "error": "",
-                    "id": uuid.uuid1().int>>64,
-                    "user_label": userlabel,
-                    "data_type": "type",
-                    "data_source": source,
-                    "is_new": True}
-        if userlabel is not None:
-            print "keeping copy", userlabel
-            keeper = copy.deepcopy(response)
-            keeper["is_new"] = False
-            id_cache[userlabel] = keeper
-
+        response = client.GetUniqueID(source)
     return response
 
+# for testing:
+    # if userlabel in id_cache:
+    #     print "using existing", userlabel
+    #     response = id_cache.get(userlabel)
+    # else:
+    #     print "creating new", userlabel
+    #     response = {"version": "version",
+    #                 "result": "ok",
+    #                 "id": uuid.uuid1().int>>64,
+    #                 "user_label": userlabel,
+    #                 "data_type": "type",
+    #                 "identifier_source": source,
+    #                 "is_new": True,
+    #                 "error": ''}
+    #     if userlabel is not None:
+    #         print "keeping copy", userlabel
+    #         keeper = copy.deepcopy(response)
+    #         keeper["is_new"] = False
+    #         id_cache[userlabel] = keeper
+    #
+    # return response
+
 
 def create_unique_id(source, userlabel=None):
     """
@@ -71,9 +73,11 @@ def create_unique_id(source, userlabel=None):
     Throws an exception if the given label already exists for the given source.
     """
     response = _call_idservice(source, userlabel)
+    if not response.get("result") == "ok":
+        raise Exception('An identifier for this userlabel could not be created -> '+str(response.get("error")))
     if not response.get("is_new"):
         raise Exception('An identifier for this userlabel already exists -> '+str(userlabel))
-    return response
+    return response.get('id')
 
 
 def get_unique_id(source, userlabel):
@@ -83,9 +87,11 @@ def get_unique_id(source, userlabel):
     Throws an exception if the given label does not exist for the given source.
     """
     response = _call_idservice(source, userlabel)
+    if not response.get("result") == "ok":
+        raise Exception('An identifier for this userlabel could not be retrieved -> '+str(response.get("error")))
     if response.get("is_new"):
         raise Exception('An identifier for this userlabel does not exist -> '+str(userlabel))
-    return response
+    return response.get('id')
 
 
 
@@ -93,4 +99,4 @@ def getsip_fromlta_byprojectandltadataproductid(projectname,dpid):
     return client.GetSip(projectname,dpid).get("sip")
 
 def getltadataproductids_fromlta_byprojectandsasid(projectname, sasid):
-    return client.GetDataProductIDS(projectname,sasid).get("ids")
\ No newline at end of file
+    return client.GetDataProductIDS(projectname,sasid).get("ids")
diff --git a/lib/siplib.py b/lib/siplib.py
index cf34a29..3a2979e 100644
--- a/lib/siplib.py
+++ b/lib/siplib.py
@@ -89,10 +89,9 @@ class Identifier(object):
         at the time the Identifier is created to allow this lookup to work).
         Throws an exception if the given label does not exist for the given source.
         """
-
         unique_id = query.get_unique_id(source, userlabel)
         identifier = Identifier.__new__(Identifier)
-        identifier._setpyxb_identifier(
+        identifier._set_pyxb_identifier(
             ltasip.IdentifierType(
                 source=str(source),
                 identifier=str(unique_id),
-- 
GitLab