def add_headers(self, **headers): """packing dicts into typecode pyclass, may fail if typecodes are used in the body (when asdict=True) """ class _holder: pass def _remap(pyobj, **d): pyobj.__dict__ = d for k,v in list(pyobj.__dict__.items()): if type(v) is not dict: continue pyobj.__dict__[k] = p = _holder() _remap(p, **v) for k,v in list(headers.items()): h = filter(lambda i: k in i.type, self.callinfo.inheaders)[0] if h.element_type != 1: raise RuntimeError('not implemented') typecode = GED(*h.type) if typecode is None: raise RuntimeError('no matching element for %s' %str(h.type)) pyclass = typecode.pyclass if pyclass is None: raise RuntimeError('no pyclass for typecode %s' %str(h.type)) if type(v) is not dict: pyobj = pyclass(v) else: pyobj = pyclass() _remap(pyobj, **v) self.soapheaders.append(pyobj)
def setRequest(self, endPointReference, action): '''Call For Request ''' self._action = action self.header_pyobjs = None pyobjs = [] namespaceURI = self.wsAddressURI addressTo = self._addressTo messageID = self._messageID = "uuid:%s" %time.time() # Set Message Information Headers # MessageID typecode = GED(namespaceURI, "MessageID") pyobjs.append(typecode.pyclass(messageID)) # Action typecode = GED(namespaceURI, "Action") pyobjs.append(typecode.pyclass(action)) # To typecode = GED(namespaceURI, "To") pyobjs.append(typecode.pyclass(addressTo)) # From typecode = GED(namespaceURI, "From") mihFrom = typecode.pyclass() mihFrom._Address = self.anonymousURI pyobjs.append(mihFrom) if endPointReference: if hasattr(endPointReference, 'typecode') is False: raise EvaluateException('endPointReference must have a typecode attribute') if isinstance(endPointReference.typecode, \ GTD(namespaceURI ,'EndpointReferenceType')) is False: raise EvaluateException('endPointReference must be of type %s' %GTD(namespaceURI ,'EndpointReferenceType')) ReferenceProperties = getattr(endPointReference, '_ReferenceProperties', None) if ReferenceProperties is None: # In recent WS-A attribute name changed ReferenceProperties = getattr(endPointReference, '_ReferenceParameters', None) if ReferenceProperties is not None: for v in getattr(ReferenceProperties, '_any', ()): if not hasattr(v,'typecode'): raise EvaluateException('<any> element, instance missing typecode attribute') pyobjs.append(v) self.header_pyobjs = tuple(pyobjs)
def setResponseFromWSAddress(self, address, localURL): '''Server-side has to set these fields in response. address -- Address instance, representing a WS-Address ''' self.From = localURL self.header_pyobjs = None pyobjs = [] namespaceURI = self.wsAddressURI for nsuri,name,value in (\ (namespaceURI, "Action", self._action), (namespaceURI, "MessageID","uuid:%s" %time.time()), (namespaceURI, "RelatesTo", address.getMessageID()), (namespaceURI, "To", self.anonymousURI),): typecode = GED(nsuri, name) pyobjs.append(typecode.pyclass(value)) typecode = GED(nsuri, "From") pyobj = typecode.pyclass() pyobj._Address = self.From pyobjs.append(pyobj) self.header_pyobjs = tuple(pyobjs)
def _getWSAddressTypeCodes(self, **kw): '''kw -- namespaceURI keys with sequence of element names. ''' typecodes = [] try: for nsuri,elements in list(kw.items()): for el in elements: typecode = GED(nsuri, el) if typecode is None: raise WSActionException('Missing namespace, import "%s"' %nsuri) typecodes.append(typecode) else: pass except EvaluateException as ex: raise EvaluateException('To use ws-addressing register typecodes for namespace(%s)' %self.wsAddressURI) return typecodes
def call_closure(*args, **kwargs): """Call the named remote web service method.""" if len(args) and len(kwargs): raise TypeError('Use positional or keyword argument only.') if len(args) > 0: raise TypeError('Not supporting SOAPENC:Arrays or XSD:List') if len(kwargs): args = kwargs callinfo = getattr(self, name).callinfo # go through the list of defined methods, and look for the one with # the same number of arguments as what was passed. this is a weak # check that should probably be improved in the future to check the # types of the arguments to allow for polymorphism for method in self._methods[name]: if len(method.callinfo.inparams) == len(kwargs): callinfo = method.callinfo binding = _Binding(url=self._url or callinfo.location, soapaction=callinfo.soapAction, **self._kw) kw = dict(unique=True) if callinfo.use == 'encoded': kw['unique'] = False if callinfo.style == 'rpc': request = TC.Struct(None, ofwhat=[], pname=(callinfo.namespace, name), **kw) response = TC.Struct(None, ofwhat=[], pname=(callinfo.namespace, name+"Response"), **kw) if len(callinfo.getInParameters()) != len(args): raise RuntimeError('expecting "%s" parts, got %s' %( str(callinfo.getInParameters(), str(args)))) for msg,pms in ((request,callinfo.getInParameters()), (response,callinfo.getOutParameters())): msg.ofwhat = [] for part in pms: klass = GTD(*part.type) if klass is None: if part.type: klass = [gt for gt in TC.TYPES if part.type==gt.type] if len(klass) == 0: klass = [gt for gt in TC.TYPES if part.type[1]==gt.type[1]] if not len(klass):klass = [TC.Any] if len(klass) > 1: #Enumerations, XMLString, etc klass = [i for i in klass if 'type' in i.__dict__] klass = klass[0] else: klass = TC.Any msg.ofwhat.append(klass(part.name)) msg.ofwhat = tuple(msg.ofwhat) if not args: args = {} else: # Grab <part element> attribute ipart,opart = callinfo.getInParameters(),callinfo.getOutParameters() if ( len(ipart) != 1 or not ipart[0].element_type or ipart[0].type is None ): raise RuntimeError('Bad Input Message "%s"' %callinfo.name) if ( len(opart) not in (0,1) or not opart[0].element_type or opart[0].type is None ): raise RuntimeError('Bad Output Message "%s"' %callinfo.name) # if ( len(args) > 1 ): # raise RuntimeError, 'Message has only one part: %s' %str(args) ipart = ipart[0] request,response = GED(*ipart.type),None if opart: response = GED(*opart[0].type) msg = args if self._asdict: if not msg: msg = dict() self._nullpyclass(request) elif request.pyclass is not None: if type(args) is dict: msg = request.pyclass() msg.__dict__.update(args) elif type(args) is list and len(args) == 1: msg = request.pyclass(args[0]) else: msg = request.pyclass() binding.Send(None, None, msg, requesttypecode=request, soapheaders=soapheaders, encodingStyle=callinfo.encodingStyle) if response is None: return None if self._asdict: self._nullpyclass(response) return binding.Receive(replytype=response, encodingStyle=callinfo.encodingStyle)
def call_closure(*args, **kwargs): """Call the named remote web service method.""" if len(args) and len(kwargs): raise TypeError('Use positional or keyword argument only.') if len(args) > 0: raise TypeError('Not supporting SOAPENC:Arrays or XSD:List') if len(kwargs): args = kwargs callinfo = getattr(self, name).callinfo # go through the list of defined methods, and look for the one with # the same number of arguments as what was passed. this is a weak # check that should probably be improved in the future to check the # types of the arguments to allow for polymorphism for method in self._methods[name]: if len(method.callinfo.inparams) == len(kwargs): callinfo = method.callinfo binding = _Binding(url=self._url or callinfo.location, soapaction=callinfo.soapAction, **self._kw) kw = dict(unique=True) if callinfo.use == 'encoded': kw['unique'] = False if callinfo.style == 'rpc': request = TC.Struct(None, ofwhat=[], pname=(callinfo.namespace, name), **kw) response = TC.Struct(None, ofwhat=[], pname=(callinfo.namespace, name+"Response"), **kw) if len(callinfo.getInParameters()) != len(args): raise RuntimeError('expecting "%s" parts, got %s' %( str(callinfo.getInParameters(), str(args)))) for msg,pms in ((request,callinfo.getInParameters()), (response,callinfo.getOutParameters())): msg.ofwhat = [] for part in pms: klass = GTD(*part.type) if klass is None: if part.type: klass = [gt for gt in TC.TYPES if part.type==gt.type] if len(klass) == 0: klass = [gt for gt in TC.TYPES if part.type[1]==gt.type[1]] if not len(klass):klass = [TC.Any] if len(klass) > 1: #Enumerations, XMLString, etc klass = [i for i in klass if 'type' in i.__dict__] klass = klass[0] else: klass = TC.Any msg.ofwhat.append(klass(part.name)) msg.ofwhat = tuple(msg.ofwhat) if not args: args = {} else: # Grab <part element> attribute ipart,opart = callinfo.getInParameters(),callinfo.getOutParameters() if ( len(ipart) != 1 or not ipart[0].element_type or ipart[0].type is None ): raise RuntimeError('Bad Input Message "%s"' %callinfo.name) if ( len(opart) not in (0,1) or not opart[0].element_type or opart[0].type is None ): raise RuntimeError('Bad Output Message "%s"' %callinfo.name) # if ( len(args) > 1 ): # raise RuntimeError, 'Message has only one part: %s' %str(args) ipart = ipart[0] request,response = GED(*ipart.type),None if opart: response = GED(*opart[0].type) msg = args if self._asdict: if not msg: msg = dict() self._nullpyclass(request) elif request.pyclass is not None: if isinstance(args, dict): msg = request.pyclass() msg.__dict__.update(args) elif isinstance(args, list) and len(args) == 1: msg = request.pyclass(args[0]) else: msg = request.pyclass() binding.Send(None, None, msg, requesttypecode=request, soapheaders=soapheaders, encodingStyle=callinfo.encodingStyle) if response is None: return None if self._asdict: self._nullpyclass(response) return binding.Receive(replytype=response, encodingStyle=callinfo.encodingStyle)