# -*- coding: utf-8
from SOAPpy import SOAPProxy
from SOAPpy import Types
from SOAPpy import Config
import re
import sys
import cElementTree
import random
#url = "http://opah.htk.tlu.ee:8080/WaramuWeb/waramuService"
#url = "http://opah:8080/waramuService/waramu"
url = "http://localhost:8080/waramuService/waramu"
#url = "http://localhost:8080/WaramuWeb/waramuService"
namespace= "http://wssoap.waramu.htk.tlu.ee/"
username='admin'
password='adminadmin'
#username='vahur'
#password='parool'
c = Config
c.debug = 0
s = SOAPProxy(url, config=c)
# ======= Login in
sessIdresponse = s._ns(namespace).newSession(uid=username, pwd=password)
respCode = int(re.search('(.*?)', sessIdresponse).group(1))
if respCode: # not 0
msg = re.search('(.*?)', sessIdresponse).group(1)
print msg
sys.exit();
sessId = re.search('(.*?)', sessIdresponse).group(1)
print "Session id:", sessId
# ======= list types
listTypesresponse = s._ns(namespace).listTypes(sessId = sessId)
xml = cElementTree.fromstring(listTypesresponse)
respCode = int(xml.find('status').text)
if respCode: # not 0
msg = xml.find('message').text
print "List types failed. Reason:",msg
xtypes = xml.find('types')
wtypes = {}
for t in xtypes:
tid = t.find('id').text
tname = t.find('name').text
wtypes[tid] = {'name': tname, 'fields':{} }
print "Types on server:"
for w in wtypes.keys():
print "\t", w, ":", wtypes.get(w).get('name')
# ======= list type fields
for w in wtypes.keys():
fieldsResponse = s._ns(namespace).describeType(sessId = sessId, typeId = w)
xml = cElementTree.fromstring(fieldsResponse)
respCode = int(xml.find('status').text)
if respCode: # not 0
msg = xml.find('message').text
print "Getting type description failed. Reason:", msg
continue
print "\nDescription for %s" % wtypes.get(w).get('name')
xfields = xml.find('description').find('fields')
for f in xfields:
print "\t", f.get('name'), "multilingual:", f.get('multiLingual'), f.get('type'), "autovalue:", f.get('autovalue'), "nice:", f.text, "vocabulary:", f.get('vocabulary')
wtypes[w]['fields'][f.get('name')] = {'multiLingual': f.get('multiLingual'), 'type': f.get('type'), 'vocabulary': f.get('vocabulary')}
# ======= create object
def _generate(_w):
woxml = "\n"
woxml += "\n"
for f in wtypes.get(_w).get('fields').keys():
typ = wtypes.get(_w).get('fields').get(f)
if typ.get('type') == 'Integer': rdata = str(random.randint(1,1000))
else: rdata = '' % f
att = ""
if f == 'language':
rdata = random.choice(langs)
if typ.get('multiLingual') == '1':
for l in langs:
att = ' lang="'+l+'"'
woxml += "\t<"+f+att+">"+rdata+" lang is:"+l+""+f+">\n"
else:
woxml += "\t<"+f+">"+rdata+""+f+">\n"
woxml += ""
woxml = woxml.decode('utf-8')
return woxml
wobjUids = []
wobjTypes = []
langs = ['et', 'en']
for w in wtypes.keys():
woxml = _generate(w)
newResourceResponse = s._ns(namespace).newResource(sessId = sessId, typeId = w, data = woxml)
xml = cElementTree.fromstring(newResourceResponse)
respCode = int(xml.find('status').text)
if respCode: # not 0
msg = xml.find('message').text
print "Object creation failed. Reason:", msg
continue
print "New object with %s uid created" % xml.find('uid').text
wobjUids.append(xml.find('uid').text)
wobjTypes.append(w)
print "Created total of %s object(s)" % len(wobjUids)
# ======= getting objects
def _list():
print "\nListing objects."
for w in wobjUids:
getResourceResponse = s._ns(namespace).getResource(sessId = sessId, uid = w)
getResourceResponse = getResourceResponse.encode('utf-8')
xml = cElementTree.fromstring(getResourceResponse)
respCode = int(xml.find('status').text)
if respCode: # not 0
msg = xml.find('message').text
print "getting object %s failed. Reason:" % w, msg
else:
print "\nObject %s from server" % w
print getResourceResponse
_list()
# ======= list vocabulary
from random import choice
rtypeid = choice(wtypes.keys())
#rtypeid = '1'
rty = wtypes.get(rtypeid)
vocfields = []
for k, v in rty.get('fields').items():
if v.get('vocabulary') == "1":
vocfields.append(k)
rfield = choice(vocfields)
rfield = "keywords"
print "\nVocabulary for", rty.get('name'), " field", rfield
getVocabularyResponse = s._ns(namespace).getVocabulary(sessId = sessId, typeId = rtypeid, field = rfield)
getVocabularyResponse = getVocabularyResponse.encode('utf-8')
xml = None
try:
xml = cElementTree.fromstring(getVocabularyResponse)
respCode = int(xml.find('status').text)
if respCode: # not 0
msg = xml.find('message').text
print "getting object %s failed. Reason:" % w, msg
vocab = xml.find('vocabulary')
if len(vocab.getchildren())<10: tos = len(vocab.getchildren())
else: tos = 10
print "Top %s used:" % tos
for x in range(0, tos):
v = vocab[x]
e = v.find('element')
c = v.find('count')
print "\t", e.text, " ", c.text
except SyntaxError:
print "not well-formed xml:", getVocabularyResponse
# ======= modify object
def _modify():
print "\nModifing objects."
for xx in range(len(wobjUids)):
w = wobjUids[xx]
wt = wobjTypes[xx]
wuxml = _generate(wt)
updateResourceResponse = s._ns(namespace).updateResource(sessId = sessId, uid = w, data = wuxml)
updateResourceResponse = updateResourceResponse.encode('utf-8')
xml = cElementTree.fromstring(updateResourceResponse)
respCode = int(xml.find('status').text)
if respCode: # not 0
msg = xml.find('message').text
print "updating object %s failed. Reason:" % w, msg
_modify()
_list()
# ======= deleting object
def _delete():
print "\nDeleting objects."
delCount = 0
totalObjs = len(wobjUids)
for w in wobjUids:
delResourceResponse = s._ns(namespace).deleteResource(sessId = sessId, uid = w)
xml = cElementTree.fromstring(delResourceResponse)
respCode = int(xml.find('status').text)
if respCode: # not 0
msg = xml.find('message').text
print "Object deletion failed. Reason:", msg
delCount += 1
print "Deleted %s of %s object(s)" % (delCount, totalObjs)
_delete()
# ======= test nice failing
print "\nFOLLOWING MUST FAIL!"
_list()
_delete()
_modify()
print "\nFAIL END"
# ======= listIdentifiers
def _listIdentifiers():
print "\nSearching and listing identifiers."
listquery = "\n"
listquery += ""
listquery += "kala"
listquery += ""
print "Query:", listquery
listIdentResponse = s._ns(namespace).listIdentifiers(sessId = sessId, query = listquery)
listIdentResponse = listIdentResponse.encode('utf-8')
xml = cElementTree.fromstring(listIdentResponse)
respCode = int(xml.find('status').text)
if respCode: # not 0
msg = xml.find('message').text
print "listing identifiers failed. Reason:" % msg
else:
ids = xml.findall('identifiers/id')
print "Number of results:", len(ids)
_listIdentifiers()
# ======= search
def _search(qqs):
print "\nSearching and listing objects."
searchquery = "\n"+qqs
print "Query:", searchquery
searchResponse = s._ns(namespace).search(sessId = sessId, query = searchquery)
searchResponse = searchResponse.encode('utf-8')
xml = cElementTree.fromstring(searchResponse)
respCode = int(xml.find('status').text)
if respCode: # not 0
msg = xml.find('message').text
print "Search failed. Reason: %s" % msg
else:
ids = xml.findall('objects/object')
print "Number of results:", len(ids)
queries = [
"kala",
"sai",
"kala"
]
print "\nTwo search calls MUST fail!"
for qq in queries:
_search(qq)
# ======= Logging out
print "\nEnd. Bye-bye."
logoutResponse = s._ns(namespace).closeSession(sessId=sessId)
respCode = int(re.search('(.*?)', logoutResponse).group(1))
if respCode:
msg = re.search('(.*?)', logoutResponse).group(1)
print "Logout failed. Reason:", msg
else:
print "Logout successful"