def check_any_nill(self): result = ['23', {'a' : None, 'b': 5}] soap = str(SoapWriter().serialize(result, TC.Any(pname="NilRequest", nillable=True, aslist=True))) ps = ParsedSoap(soap) tc = TC.Any(nillable=True) pyobj = ps.Parse(tc)
def check_empty_array(self): """Empty Array returned as list() """ data = [] s = str(SoapWriter().serialize(data, TC.Any(aslist=True))) p = ParsedSoap(s).Parse(TC.Any()) self.assertTrue(data == p, 'expecting "%s", got "%s"' % (data, p))
def __call__(self, **params): # Pull out arguments that Send() uses kw = {} for key in ['auth_header', 'nsdict', 'requesttypecode', 'soapaction']: if params.has_key(key): kw[key] = params[key] del params[key] nsuri = self.namespace if nsuri is None: return self.binding.RPC( None, self.name, None, encodingStyle="http://schemas.xmlsoap.org/soap/encoding/", _args=params, replytype=TC.Any(self.name + "Response", aslist=False), **kw) return self.binding.RPC( None, (nsuri, self.name), None, encodingStyle="http://schemas.xmlsoap.org/soap/encoding/", _args=params, replytype=TC.Any((nsuri, self.name + "Response"), aslist=False), **kw)
def check_empty_struct(self): """Empty Struct is None, maybe dict() makes more sense, but this is fairly hard to determine if not typed (which is the norm). """ data = {} s = str(SoapWriter().serialize(data, TC.Any())) p = ParsedSoap(s).Parse(TC.Any()) self.assertTrue(p == None, 'expecting "%s", got "%s"' % (None, p))
def check_any_dict_list_rpcenc(self): sw = SoapWriter() testObj = [{"a":1,"b":2}, {"d":4,"e":5}, {"f":{"x":9}, "g":[6,7.0]}] typecode = TC.Any(aslist=True) sw.serialize(testObj, typecode=typecode) xml = str(sw) ps = ParsedSoap(xml) result = TC.Any().parse(ps.body_root, ps) self.assertTrue(result == testObj)
def check_any_compound(self): # from zsi developer's guide xml = """ <tns:foo %(tns)s %(xsi)s %(soap)s> <tns:i xsi:type="SOAP-ENC:integer">12</tns:i> <tns:name xsi:type="SOAP-ENC:string">Hello world</tns:name> </tns:foo>""" % NSDICT ps = ParsedSoap(xml, envelope=False) self.assertTrue(ps.Parse(TC.Any()) == {'i': 12, 'name': 'Hello world'}) self.assertTrue(ps.Parse(TC.Any(aslist=True)) == [12, 'Hello world'])
def __call__(self, *args): nsuri = self.namespace if nsuri is None: return self.binding.RPC( None, self.name, args, encodingStyle="http://schemas.xmlsoap.org/soap/encoding/", replytype=TC.Any(self.name + "Response")) return self.binding.RPC( None, (nsuri, self.name), args, encodingStyle="http://schemas.xmlsoap.org/soap/encoding/", replytype=TC.Any((nsuri, self.name + "Response")))
def check_any_untyped_int(self): # from zsi developer's guide d = dict(value=12) d.update(NSDICT) xml = """<tns:i %(tns)s>12</tns:i>""" % NSDICT ps = ParsedSoap(xml, envelope=False) self.assertRaises(EvaluateException, ps.Parse, TC.Any())
def __call__(self, *args): return self.binding.RPC( None, self.name, args, encodingStyle="http://schemas.xmlsoap.org/soap/encoding/", replytype=TC.Any(self.name + "Response"))
def check_any_untyped_int(self): # from zsi developer's guide d = dict(value=12) d.update(NSDICT) xml = """<tns:i %(tns)s>12</tns:i>""" %NSDICT ps = ParsedSoap(xml, envelope=False) self.assertTrue(int(ps.Parse(TC.Any())) == 12)
def check_any_typed_nonNegativeInteger(self): # from zsi developer's guide value = 12 d = dict(value=value) d.update(NSDICT) xml = """<tns:i xsi:type="xsd:nonNegativeInteger" %(xsi)s %(soap)s %(tns)s %(xsd)s>%(value)d</tns:i>""" % d ps = ParsedSoap(xml, envelope=False) self.assertTrue(ps.Parse(TC.Any()) == value)
def deserialize(objectToDeserialize): sw = SoapWriter(nsdict={}, header=True, outputclass=None, encodingStyle=None) tc = TC.Any(pname=None, aslist=False) deserializedObject = sw.serialize(objectToDeserialize, tc).body root = etree.XML(str(deserializedObject)) body = root[0] return etree.tostring(body, encoding='utf-8', pretty_print=True)
def check_any_typed_xsd_int(self): # from zsi developer's guide value = 12 d = dict(value=value) d.update(NSDICT) xml = """<tns:i xsi:type="xsd:int" %(xsi)s %(soap)s %(tns)s %(xsd)s>%(value)d</tns:i>""" % d ps = ParsedSoap(xml, envelope=False) self.failUnless(ps.Parse(TC.Any()) == value)
def listMembers(url): Binding.defaultHttpsTransport = HTTPSConnection2 fp = open('soap_trace', 'w') b = Binding(url=url, tracefile=fp) reply = b.RPC(None, 'listMembers', [], encodingStyle="http://schemas.xmlsoap.org/soap/encoding/", replytype=TC.Any("listMembersResponse", nillable=True)) return reply['listMembersReturn']
def check_parse_empty_string(self): # Empty String typecodes = list(TC.Any.parsemap.values()) for tc in [c for c in list(TC.Any.parsemap.values()) if isinstance(c, TC.String)]: sw = SoapWriter() sw.serialize("", typecode=tc, typed=True) soap = str(sw) ps = ParsedSoap(soap) parsed = ps.Parse(TC.Any()) self.assertEqual("", parsed)
def __parse_child(self, node): '''for rpc-style map each message part to a class in typesmodule ''' try: tc = self.gettypecode(self.typesmodule, node) except: self.logger.debug('didnt find typecode for "%s" in typesmodule: %s', node.localName, self.typesmodule) tc = TC.Any(aslist=1) return tc.parse(node, self.ps) self.logger.debug('parse child with typecode : %s', tc) try: return tc.parse(node, self.ps) except Exception: self.logger.debug('parse failed try Any : %s', tc) tc = TC.Any(aslist=1) return tc.parse(node, self.ps)
def check_parse_empty_all(self): # None skip = [TC.FPEnumeration, TC.Enumeration, TC.IEnumeration, TC.List, TC.Integer] for typeclass in [c for c in list(TC.__dict__.values()) if type(c) in [type,type] and not issubclass(c, TC.String) and issubclass(c, TC.SimpleType)]: if typeclass in skip: continue tc = typeclass() sw = SoapWriter() sw.serialize(None, typecode=tc, typed=True) soap = str(sw) ps = ParsedSoap(soap) parsed = ps.Parse(TC.Any()) self.assertEqual(None, parsed)
def soap_parse_ZSI(xml): ''' This function receives an XML SOAP and returns a dictionary. It is intended to be used inside a Node, this is why it raises a NodeParserError ''' parsed_obj = None parsed_dict = dict() try: parsed_obj = ParsedSoap(xml) parsed_dict = parsed_obj.Parse(TC.Any()) except Exception, e: print e
def check_builtins(self): myInt,myLong,myStr,myDate,myFloat = 123,2147483648,\ "hello", time.gmtime(), 1.0001 orig = [myInt, myLong, myStr, myDate, myFloat] sw = SoapWriter() sw.serialize(orig, typecode=TC.Any(pname="builtins", aslist=True)) ps = ParsedSoap(str(sw)) parsed = ps.Parse(TC.Any()) self.assertEqual(len(orig), len(parsed)) self.assertEqual(myInt, parsed[0]) self.assertEqual(myLong, parsed[1]) self.assertEqual(myStr, parsed[2]) self.assertEqual(myDate[0:6], parsed[3][0:6]) self.assertEqual(myFloat, parsed[4]) self.assertEqual(type(myInt), type(parsed[0])) self.assertEqual(type(myLong), type(parsed[1])) self.assertEqual(type(myStr), type(parsed[2])) self.assertEqual(tuple, type(parsed[3])) self.assertEqual(type(myFloat), type(parsed[4]))
def _Dispatch(ps, modules, SendResponse, SendFault, nsdict={}, typesmodule=None, gettypecode=gettypecode, rpc=False, docstyle=False, **kw): '''Find a handler for the SOAP request in ps; search modules. Call SendResponse or SendFault to send the reply back, appropriately. Behaviors: default -- Call "handler" method with pyobj representation of body root, and return a self-describing request (w/typecode). Parsing done via a typecode from typesmodule, or Any. docstyle -- Call "handler" method with ParsedSoap instance and parse result with an XML typecode (DOM). Behavior, wrap result in a body_root "Response" appended message. rpc -- Specify RPC wrapper of result. Behavior, ignore body root (RPC Wrapper) of request, parse all "parts" of message via individual typecodes. Expect the handler to return the parts of the message, whether it is a dict, single instance, or a list try to serialize it as a Struct but if this is not possible put it in an Array. Parsing done via a typecode from typesmodule, or Any. ''' global _client_binding try: what = str(ps.body_root.localName) # See what modules have the element name. if modules is None: modules = ( sys.modules['__main__'], ) handlers = [ getattr(m, what) for m in modules if hasattr(m, what) ] if len(handlers) == 0: raise TypeError("Unknown method " + what) # Of those modules, see who's callable. handlers = [ h for h in handlers if callable(h) ] if len(handlers) == 0: raise TypeError("Unimplemented method " + what) if len(handlers) > 1: raise TypeError("Multiple implementations found: %s" % handlers) handler = handlers[0] _client_binding = ClientBinding(ps) if docstyle: result = handler(ps.body_root) tc = TC.XML(aslist=1, pname=what+'Response') elif not rpc: try: tc = gettypecode(typesmodule, ps.body_root) except Exception: tc = TC.Any() try: arg = tc.parse(ps.body_root, ps) except EvaluateException as ex: SendFault(FaultFromZSIException(ex), **kw) return try: result = handler(arg) except Exception as ex: SendFault(FaultFromZSIException(ex), **kw) return try: tc = result.typecode except AttributeError as ex: SendFault(FaultFromZSIException(ex), **kw) return elif typesmodule is not None: kwargs = {} for e in _child_elements(ps.body_root): try: tc = gettypecode(typesmodule, e) except Exception: tc = TC.Any() try: kwargs[str(e.localName)] = tc.parse(e, ps) except EvaluateException as ex: SendFault(FaultFromZSIException(ex), **kw) return result = handler(**kwargs) aslist = False # make sure data is wrapped, try to make this a Struct if isinstance(result,_seqtypes): for _ in result: aslist = hasattr(result, 'typecode') if aslist: break elif not isinstance(result, dict): aslist = not hasattr(result, 'typecode') result = (result,) tc = TC.Any(pname=what+'Response', aslist=aslist) else: # if this is an Array, call handler with list # if this is an Struct, call handler with dict tp = _find_type(ps.body_root) isarray = ((isinstance(tp, (tuple,list)) and tp[1] == 'Array') or _find_arraytype(ps.body_root)) data = _child_elements(ps.body_root) tc = TC.Any() if isarray and len(data) == 0: result = handler() elif isarray: try: arg = [ tc.parse(e, ps) for e in data ] except EvaluateException as e: #SendFault(FaultFromZSIException(e), **kw) SendFault(RuntimeError("THIS IS AN ARRAY: %s" %isarray)) return result = handler(*arg) else: try: kwarg = dict([ (str(e.localName),tc.parse(e, ps)) for e in data ]) except EvaluateException as e: SendFault(FaultFromZSIException(e), **kw) return result = handler(**kwarg) # reponse typecode #tc = getattr(result, 'typecode', TC.Any(pname=what+'Response')) tc = TC.Any(pname=what+'Response') sw = SoapWriter(nsdict=nsdict) sw.serialize(result, tc) return SendResponse(str(sw), **kw) except Fault as e: return SendFault(e, **kw) except Exception as e: # Something went wrong, send a fault. return SendFault(FaultFromException(e, 0, sys.exc_info()[2]), **kw)
def Send(self, url, opname, obj, nsdict={}, soapaction=None, wsaction=None, endPointReference=None, soapheaders=(), **kw): '''Send a message. If url is None, use the value from the constructor (else error). obj is the object (data) to send. Data may be described with a requesttypecode keyword, the default is the class's typecode (if there is one), else Any. Try to serialize as a Struct, if this is not possible serialize an Array. If data is a sequence of built-in python data types, it will be serialized as an Array, unless requesttypecode is specified. arguments: url -- opname -- struct wrapper obj -- python instance key word arguments: nsdict -- soapaction -- wsaction -- WS-Address Action, goes in SOAP Header. endPointReference -- set by calling party, must be an EndPointReference type instance. soapheaders -- list of pyobj, typically w/typecode attribute. serialized in the SOAP:Header. requesttypecode -- ''' url = url or self.url endPointReference = endPointReference or self.endPointReference # Serialize the object. d = {} d.update(self.nsdict) d.update(nsdict) sw = SoapWriter( nsdict=d, header=True, outputclass=self.writerclass, encodingStyle=kw.get('encodingStyle'), ) requesttypecode = kw.get('requesttypecode') if kw.has_key('_args'): #NamedParamBinding tc = requesttypecode or TC.Any(pname=opname, aslist=False) sw.serialize(kw['_args'], tc) elif not requesttypecode: tc = getattr(obj, 'typecode', None) or TC.Any(pname=opname, aslist=False) try: if type(obj) in _seqtypes: obj = dict(map(lambda i: (i.typecode.pname, i), obj)) except AttributeError: # can't do anything but serialize this in a SOAP:Array tc = TC.Any(pname=opname, aslist=True) else: tc = TC.Any(pname=opname, aslist=False) sw.serialize(obj, tc) else: sw.serialize(obj, requesttypecode) for i in soapheaders: sw.serialize_header(i) # # Determine the SOAP auth element. SOAP:Header element if self.auth_style & AUTH.zsibasic: sw.serialize_header(_AuthHeader(self.auth_user, self.auth_pass), _AuthHeader.typecode) # # Serialize WS-Address if self.wsAddressURI is not None: if self.soapaction and wsaction.strip('\'"') != self.soapaction: raise WSActionException, 'soapAction(%s) and WS-Action(%s) must match'\ %(self.soapaction,wsaction) self.address = Address(url, self.wsAddressURI) self.address.setRequest(endPointReference, wsaction) self.address.serialize(sw) # # WS-Security Signature Handler if self.sig_handler is not None: self.sig_handler.sign(sw) scheme, netloc, path, nil, nil, nil = urlparse.urlparse(url) transport = self.transport if transport is None and url is not None: if scheme == 'https': transport = self.defaultHttpsTransport elif scheme == 'http': transport = self.defaultHttpTransport else: raise RuntimeError, 'must specify transport or url startswith https/http' # Send the request. if issubclass(transport, httplib.HTTPConnection) is False: raise TypeError, 'transport must be a HTTPConnection' soapdata = str(sw) self.h = transport(netloc, None, **self.transdict) self.h.connect() self.SendSOAPData(soapdata, url, soapaction, **kw)