def ZsiTransfomComplexType(wsdl_types, obj, xsi_type, web_service): """Transforms a python dictionary into a ZSI object. Args: wsdl_types: dict WSDL definitions of all types in the same service as the given xsi_type. obj: dict Dictionary to be made into a ZSI object. xsi_type: str The name of the complex type this dictionary represents. web_service: module The generated web service which uses this complex type. Returns: obj A ZSI object representing this complex type. """ obj_contained_type, contained_type_key = Utils.GetExplicitType(wsdl_types, obj, xsi_type) if obj_contained_type: xsi_type = obj_contained_type new_object = ZsiSanityCheck.GetPyClass(xsi_type, web_service) parameters = Utils.GenParamOrder(wsdl_types, xsi_type) for key in obj: if not obj[key] or key == contained_type_key: continue for parameter, param_type in parameters: if parameter == key: new_object.__dict__.__setitem__( '_%s' % key, MakeZsiCompatible(obj[key], param_type, wsdl_types, web_service)) break return new_object
def _PackDictionaryAsXml(obj, xml_tag_name, wsdl_type_map, wrap_list, xsi_type): """Pack a Python dictionary object into an XML string. Args: obj: dict Python dictionary to pack. xml_tag_name: str The name of the XML tag that will house this dict. wsdl_type_map: dict Information on all WSDL-defined types. wrap_list: bool If true, wraps lists in an extra layer (e.g., "'ids': ['12345']" becomes "<ids><ids>12345</ids></ids>"). If False, "'ids': ['12345']" becomes "<ids>12345</ids>". xsi_type: str The WSDL-defined type of this object, if applicable. Returns: str An XML element representing the given dictionary. """ buf = '' obj_copy = obj.copy() # See if this object specifies its xsi_type and, if so, override the xsi_type # given. This has to occur before the main packing loop to ensure that # elements which exist only in a specified subtype can be handled correctly. obj_contained_type, xsi_override_key = Utils.GetExplicitType( wsdl_type_map, obj, xsi_type) if obj_contained_type: xsi_type = obj_contained_type del obj_copy[xsi_override_key] param_order = [] if xsi_type in wsdl_type_map: param_order = Utils.GenParamOrder(wsdl_type_map, xsi_type) # Recursively pack this dictionary's elements. If there is a specified param # order, pack things in this order. After going through the parameters with # an ordering, pack anything that remains in no particular order. for parameter, param_type in param_order: if parameter in obj_copy: buf += PackVarAsXml(obj[parameter], parameter, wsdl_type_map, wrap_list, param_type) del obj_copy[parameter] for key in obj_copy: buf += PackVarAsXml(obj[key], key, wsdl_type_map, wrap_list) if xsi_type: if obj_contained_type and len(obj.keys()) == 1: return '<%s xsi3:type="%s"/>' % (xml_tag_name, xsi_type) else: return '<%s xsi3:type="%s">%s</%s>' % (xml_tag_name, xsi_type, buf, xml_tag_name) else: return '<%s>%s</%s>' % (xml_tag_name, buf, xml_tag_name)
def _SanityCheckComplexType(wsdl_types, obj, xsi_type): """Validates a dict representing a complex type against its WSDL definition. Args: wsdl_types: dict WSDL-defined types in the same service as the given type. obj: dict Object that should represent an instance of the given type. xsi_type: str The complex type name defined in the WSDL. Raises: ValidationError: The given object is not an acceptable representation of the given WSDL-defined complex type. """ ValidateTypes(((obj, dict), )) obj_contained_type, contained_type_key = Utils.GetExplicitType( wsdl_types, obj, xsi_type) if obj_contained_type and not obj_contained_type == xsi_type: if not IsSuperType(wsdl_types, obj_contained_type, xsi_type): raise ValidationError( 'Expecting type of \'%s\' but given type of class ' '\'%s\'.' % (xsi_type, obj_contained_type)) xsi_type = obj_contained_type parameters = Utils.GenParamOrder(wsdl_types, xsi_type) for key in obj: if obj[key] is None or (obj_contained_type and key == contained_type_key): continue found = False for parameter, param_type in parameters: if parameter == key: found = True if Utils.IsXsdOrSoapenc(param_type): ValidateTypes(((obj[key], (str, unicode)), )) else: NewSanityCheck(wsdl_types, obj[key], param_type) break if not found: raise ValidationError('Field \'%s\' is not in type \'%s\'.' % (key, xsi_type))
def _RestoreListTypesForResponse(response, xsi_type, service_type_map): """Restores list types for an individual response object. Args: response: obj An individual object returned by the webservice. May be a dictionary, list, or string depending on what it represents. xsi_type: str The WSDL-defined type of this response. service_type_map: dict Information on WSDL-defined types in one service. Returns: obj The response in its proper format. May be a dictionary, list, or string depending on what was input. Not guaranteed to output the same data type that was input - may output a list instead. """ if isinstance(response, dict): if not response: return response for param, param_type in Utils.GenParamOrder(service_type_map, xsi_type): if not param in response or Utils.IsXsdOrSoapenc(param_type): continue value = response[param] if (service_type_map[param_type]['soap_type'] == 'array' and not isinstance(response[param], list)): value = [value] response[param] = _RestoreListTypesForResponse( value, param_type, service_type_map) return response elif isinstance(response, list): lst = [] for item in response: if item is None: continue lst.append( _RestoreListTypesForResponse( item, service_type_map[xsi_type]['base_type'], service_type_map)) return lst else: return response