def build_soap_call(method, arguments, encoding=SOAP_ENCODING, envelope_attrib=None, typed=None): """ Builds a soap call. @param method: method for the soap call. If set to None, the method element will be omitted and arguments will be added directly to the body (error message) @param arguments: arguments for the call @param encoding: encoding for the call @param envelope_attrib: envelope attribute @param typed: True if typed @type method: string or None @type arguments: dict or ElementTree.Element @type encoding: string @type envelope_attrib: list @type typed: boolean or None @return: soap call @rtype: string """ envelope = ElementTree.Element("s:Envelope") if envelope_attrib: for n in envelope_attrib: envelope.attrib.update({n[0]: n[1]}) else: envelope.attrib.update({'s:encodingStyle': "http://schemas.xmlsoap.org/soap/encoding/"}) envelope.attrib.update({'xmlns:s': "http://schemas.xmlsoap.org/soap/envelope/"}) body = ElementTree.SubElement(envelope, "s:Body") if method: re = ElementTree.SubElement(body, method) if encoding: re.set("%sencodingStyle" % NS_SOAP_ENV, encoding) else: re = body # append the arguments if isinstance(arguments, dict): for arg_name, arg_val in arguments.iteritems(): arg_type = map_upnp_type(arg_val) arg_val = map_upnp_value(arg_val) e = ElementTree.SubElement(re, arg_name) if typed and arg_type: if not isinstance(type, ElementTree.QName): arg_type = ElementTree.QName( "http://www.w3.org/1999/XMLSchema", arg_type) e.set('%stype' % NS_XSI, arg_type) e.text = arg_val else: re.append(arguments) preamble = """<?xml version="1.0" encoding="utf-8"?>""" return '%s%s' % (preamble, ElementTree.tostring(envelope, 'utf-8'))
def build_soap_call(method, arguments, encoding=NS_SOAP_ENC, envelope_attrib=None, typed=None, header_args=None): """ Builds a soap call. @param method: method for the soap call. If set to None, the method element will be omitted and arguments will be added directly to the body (error message) @param arguments: arguments for the call @param encoding: encoding for the call @param envelope_attrib: envelope attribute @param typed: True if typed @param header_args: header arguments if any. It should be dict of dict @type method: string or None @type arguments: dict or ElementTree.Element @type encoding: string @type envelope_attrib: list @type typed: boolean or None @type header_args: dict or ElementTree.Element or None @return: soap call @rtype: string """ envelope = ElementTree.Element("s:Envelope") if envelope_attrib: for n in envelope_attrib: envelope.attrib.update({n[0]: n[1]}) else: envelope.attrib.update({'s:encodingStyle': NS_SOAP_ENC}) envelope.attrib.update({'xmlns:s': NS_SOAP_ENV}) qname_ns = NS_XSD if header_args and isinstance(header_args, dict) and len(header_args) > 0: header = ElementTree.SubElement(envelope, "s:Header") if encoding: header.set("{%s}encodingStyle" % NS_SOAP_ENV, encoding) try: for header_name, header_args_dict in header_args.iteritems(): header_name_elem = ElementTree.SubElement(header, header_name) if isinstance(header_args_dict, dict): for header_arg_name, header_arg_val in header_args_dict.iteritems(): header_arg_type = map_upnp_type(header_arg_val) header_arg_val = map_upnp_value(header_arg_val) he = ElementTree.SubElement(header_name_elem, header_arg_name) if typed and arg_type: if not isinstance(type, ElementTree.QName): header_arg_type = ElementTree.QName(qname_ns, header_arg_type) he.set('{%s}type' % NS_XSI, header_arg_type) he.text = header_arg_val except Exception, e: log.error("Ignoring soap header due to malformed header_args dict") print str(e)
def build_notify_message_body(variables): log.debug("Building event message body") property_set = ElementTree.Element("e:propertyset") property_set.attrib.update({'xmlns:e': "urn:schemas-upnp-org:event-1-0"}) for var_name, var_value in variables.items(): property = ElementTree.SubElement(property_set, "e:property") try: var_val = map_upnp_value(var_value) except: #TODO - raise an error? log.error("Unknown state variable type") pass if var_val == None: var_val = '' e = ElementTree.SubElement(property, var_name) e.text = var_val return ElementTree.tostring(property_set, 'utf-8')
body = ElementTree.SubElement(envelope, "s:Body") if method: re = ElementTree.SubElement(body, method) if encoding: re.set("{%s}encodingStyle" % NS_SOAP_ENV, encoding) else: re = body # append the arguments if isinstance(arguments, dict): for arg_name, arg_val in arguments.iteritems(): arg_type = map_upnp_type(arg_val) arg_val = map_upnp_value(arg_val) e = ElementTree.SubElement(re, arg_name) if typed and arg_type: if not isinstance(type, ElementTree.QName): arg_type = ElementTree.QName(qname_ns, arg_type) e.set('{%s}type' % NS_XSI, arg_type) e.text = arg_val else: re.append(arguments) preamble = """<?xml version="1.0" encoding="utf-8"?>""" return '%s%s' % (preamble, ElementTree.tostring(envelope, 'utf-8')) def __decode_result(element):
def build_soap_call(method, arguments, encoding=NS_SOAP_ENC, envelope_attrib=None, typed=None, header_args=None): """ Builds a soap call. @param method: method for the soap call. If set to None, the method element will be omitted and arguments will be added directly to the body (error message) @param arguments: arguments for the call @param encoding: encoding for the call @param envelope_attrib: envelope attribute @param typed: True if typed @param header_args: header arguments if any. It should be dict of dict @type method: string or None @type arguments: dict or ElementTree.Element @type encoding: string @type envelope_attrib: list @type typed: boolean or None @type header_args: dict or ElementTree.Element or None @return: soap call @rtype: string """ envelope = ElementTree.Element("s:Envelope") if envelope_attrib: for n in envelope_attrib: envelope.attrib.update({n[0]: n[1]}) else: envelope.attrib.update({'s:encodingStyle': NS_SOAP_ENC}) envelope.attrib.update({'xmlns:s': NS_SOAP_ENV}) qname_ns = NS_XSD if header_args and isinstance(header_args, dict) and len(header_args) > 0: header = ElementTree.SubElement(envelope, "s:Header") if encoding: header.set("{%s}encodingStyle" % NS_SOAP_ENV, encoding) try: for header_name, header_args_dict in list(header_args.items()): header_name_elem = ElementTree.SubElement(header, header_name) if isinstance(header_args_dict, dict): for header_arg_name, header_arg_val in list(header_args_dict.items()): header_arg_type = map_upnp_type(header_arg_val) header_arg_val = map_upnp_value(header_arg_val) he = ElementTree.SubElement(header_name_elem, header_arg_name) if typed and arg_type: if not isinstance(type, ElementTree.QName): header_arg_type = ElementTree.QName(qname_ns, header_arg_type) he.set('{%s}type' % NS_XSI, header_arg_type) he.text = header_arg_val except Exception as e: log.error("Ignoring soap header due to malformed header_args dict") print((str(e))) elif ElementTree.iselement(header_args): header = ElementTree.SubElement(envelope, "s:Header") header.append(header_args) body = ElementTree.SubElement(envelope, "s:Body") if method: re = ElementTree.SubElement(body, method) if encoding: re.set("{%s}encodingStyle" % NS_SOAP_ENV, encoding) else: re = body # append the arguments if isinstance(arguments, dict): for arg_name, arg_val in list(arguments.items()): arg_type = map_upnp_type(arg_val) arg_val = map_upnp_value(arg_val) e = ElementTree.SubElement(re, arg_name) if typed and arg_type: if not isinstance(type, ElementTree.QName): arg_type = ElementTree.QName(qname_ns, arg_type) e.set('{%s}type' % NS_XSI, arg_type) e.text = arg_val else: re.append(arguments) preamble = """<?xml version="1.0" encoding="utf-8"?>""" return '%s%s' % (preamble, ElementTree.tostring(envelope, 'utf-8').decode('utf-8'))