source: TI01-discovery/trunk/python/DiscoveryWS.py @ 932

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI01-discovery/trunk/python/DiscoveryWS.py@932
Revision 932, 2.4 KB checked in by lawrence, 14 years ago (diff)

Refactored discovery web service into a class, and
at least the search full text method works fine using
ZSI 1.6.0 from BNL laptop.

Line 
1#/usr/bin/env python
2from ZSI import ServiceProxy
3from ZSI.wstools.WSDLTools import WSDLReader
4import sys
5import os
6import pdb
7
8# This version known to work (in part) with ZSI 1.6 patched for NDG ...
9
10DEBUG=1
11
12class DiscoveryWS:
13   
14    ''' This is the class which provides an interface to the NDG web discovery
15    web services '''
16   
17    def __init__(self,
18        wsdl='http://superglue.badc.rl.ac.uk/exist/services/Discovery?WSDL',
19        tracefile=None,
20        user=None,
21        passwd=None):
22        ''' Establish connection to wsdl proxy at self.srv with self.sessID '''
23
24        try:
25            wr=WSDLReader()
26            self.srv=ServiceProxy(wsdl,use_wsdl=True,tracefile=tracefile)
27            if user is not None:
28                conResp=self.srv.connect(in0=user,in1=passwd)
29            else:
30                conResp=self.srv.connect()
31            self.sessID=conResp['connectReturn']
32            if DEBUG: print 'Connection Made to Session: ',self.sessID
33        except Exception, e:
34            raise Exception(str(e)+os.linesep)
35
36
37    def SearchFullText(self,query):
38        ''' Produce a result set from a full text query on the NDG database '''
39        result = self.srv.fullText(in0=self.sessID, in1=query)
40        self.result=result['fullTextReturn']
41        if DEBUG: self.__performance()
42       
43       
44    def __performance(self):
45        print 'QueryTime='+str(self.result['queryTime'])
46        print 'Hits='+str(self.result['hits'])
47
48       
49    def GetResults(self,
50            number=1, # number of records to return
51            offset=1, # offset index within result set (begins at 1)
52            highlight=u'none',  # string to highlight in result set
53            xinclude=1,
54            indent=0):
55        ''' Return <number> of records from the result set beginning
56        at <offset> '''
57
58        sets=self.srv.retrieve(in0=self.sessID,
59                              in1=offset,
60                              in2=number,
61                              in3=indent,
62                              in4=xinclude,
63                              in5=highlight)
64
65        return sets['retrieveReturn']   
66       
67    def release(self):
68        self.srv.disconnect(in0=self.sessID)
69
70if __name__ == '__main__':
71
72    ws=DiscoveryWS()
73    ws.SearchFullText('acsoe')
74    results=ws.GetResults(number=2)
75    ws.release()
76    f=file('output.html','w')
77    for r in results:f.write(r+'\n')
78    f.close()
79 
Note: See TracBrowser for help on using the repository browser.