source: TI03-DataExtractor/branches/titania_install/pydxc/WSCaller.py @ 1520

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI03-DataExtractor/branches/titania_install/pydxc/WSCaller.py@1709
Revision 1520, 3.0 KB checked in by astephen, 14 years ago (diff)

This is the live version on titania - changes have been made so safest to SVN it.

Line 
1#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
2#   This software may be distributed under the terms of the
3#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
4
5"""
6WSCaller.py
7===========
8
9Holds the WSCaller class used to make calls to the Web Service
10across the network to a remote server.
11
12"""
13
14# Import standard library modules
15import os, sys, re
16
17# Import SOAP library
18from ZSI.client import Binding
19
20# Import package modules
21from clientConfig import *
22from common import *
23from DisplayManager import DisplayManager
24
25
26class WSCaller:
27    """
28    Used to call Web Service on a remote machine.
29    """         
30   
31    def __init__(self):
32        """
33        Sets up the instance.
34        """
35        # Set up SOAP bindings
36        # Need to deal with different ZSI versions
37       
38        urlPattern=re.compile(r"(\w+)://([\w.]+):(\d+)/(.*)$")
39        (transport, soap_server_host, soap_server_port, cmd)=urlPattern.match(SOAP_SERVER_URL).groups()
40        from ZSI.version import Version as zsiv
41        zsiv=float("%s.%s" % zsiv[:2])
42        if zsiv<2.0:
43            self.server=Binding(url="", host=soap_server_host, port=soap_server_port)
44        else:
45            self.server = Binding(url=SOAP_SERVER_URL)
46       
47
48    def callServerMethod(self, methodName, args, packArgs=None):
49        """
50        Calls method "methodName(args)" and returns whatever is returned.
51        """
52        if packArgs:
53            args=self._packArgsAsList(args)
54       
55        # Make that call
56        try:
57            response=apply(getattr(self.server, methodName), args) 
58            response=deUnicodeObject(response)
59        except Exception, error:
60            if str(error)=="(111, 'Connection refused')":
61                error="DX Server is currently unavailable. Please try again later."
62            self._displayErrorPage(error)
63        response=self._webServiceCallWrapper(response)
64        return response
65       
66       
67    def _packArgsAsList(self, args):
68        """
69        In order to work with ZSI SOAP library need to pack up arguments as a list
70        of [keyword, value] pairs rather than a dictionary.
71        """
72        newList=[]
73        for key, value in args.items():
74            newList.append([key, value])
75        return newList
76       
77
78    def _deUnicodeObject(self, obj):
79        """
80        Returns an identical object with all unicode strings returned as normal strings.
81        """
82        return deUnicodeObject(obj)     
83       
84
85    def _webServiceCallWrapper(self, response):
86        """
87        Analyses the response from a Web Service call to check if an error has
88        occurred. If so it parses and reports the error. Otherwise it returns 
89        the object returned from the Web Service call.
90        """
91        if (type(response) in (type(""), type(u'w'))):
92            # String returns are errors
93            self._displayErrorPage(response)
94        elif (type(response)==type([1,2]) and (type(response[0])==type("jlkjl") and len(response)==1)):
95            self._displayErrorPage(response[0])
96        else:
97            return response
98
99
100    def _displayErrorPage(self, error):
101        """
102        Displays the error if one is found.
103        """
104        self.displayer=DisplayManager()
105        self.displayer._displayErrorPage(error)
106        if DEBUG==1: raise Exception, error
107        sys.exit()
108       
Note: See TracBrowser for help on using the repository browser.