def encode(self): # Do nothing if we have a SOAP message already if isinstance(self.data, basestring): return self.data # self.data is here a Python object. Wrap it in a SOAP Body. soap = Object(Body=self.data) # Marshall it. marshaller = XmlMarshaller(rootTag='Envelope', namespaces=self.ns, namespacedTags=self.namespacedTags) return marshaller.marshall(soap)
def encode(self): # Do nothing if we have a SOAP message already if isinstance(self.data, basestring): return self.data # self.data is here a Python object. Wrap it a SOAP Body. soap = Object(Body=self.data) # Marshall it. marshaller = XmlMarshaller(rootTag='Envelope', namespaces=self.ns, namespacedTags=self.namespacedTags) return marshaller.marshall(soap)
def export(self, at='string'): '''Creates an "exportable", XML version of this object. If p_at is "string", this method returns the XML version, without the XML prologue. Else, (a) if not p_at, the XML will be exported on disk, in the OS temp folder, with an ugly name; (b) else, it will be exported at path p_at.''' # Determine where to put the result toDisk = (at != 'string') if toDisk and not at: at = getOsTempFolder() + '/' + self.o.UID() + '.xml' # Create the XML version of the object marshaller = XmlMarshaller(cdata=True, dumpUnicode=True, dumpXmlPrologue=toDisk, rootTag=self.klass.__name__) xml = marshaller.marshall(self.o, objectType='appy') # Produce the desired result if toDisk: f = file(at, 'w') f.write(xml.encode('utf-8')) f.close() return at else: return xml
def export(self, at='string', format='xml', include=None, exclude=None): '''Creates an "exportable" version of this object. p_format is "xml" by default, but can also be "csv". If p_format is: * "xml", if p_at is "string", this method returns the XML version, without the XML prologue. Else, (a) if not p_at, the XML will be exported on disk, in the OS temp folder, with an ugly name; (b) else, it will be exported at path p_at. * "csv", if p_at is "string", this method returns the CSV data as a string. If p_at is an opened file handler, the CSV line will be appended in it. If p_include is given, only fields whose names are in it will be included. p_exclude, if given, contains names of fields that will not be included in the result. ''' if format == 'xml': # Todo: take p_include and p_exclude into account. # Determine where to put the result toDisk = (at != 'string') if toDisk and not at: at = getOsTempFolder() + '/' + self.o.UID() + '.xml' # Create the XML version of the object marshaller = XmlMarshaller(cdata=True, dumpUnicode=True, dumpXmlPrologue=toDisk, rootTag=self.klass.__name__) xml = marshaller.marshall(self.o, objectType='appy') # Produce the desired result if toDisk: f = file(at, 'w') f.write(xml.encode('utf-8')) f.close() return at else: return xml elif format == 'csv': if isinstance(at, basestring): marshaller = CsvMarshaller(include=include, exclude=exclude) return marshaller.marshall(self) else: marshaller = CsvMarshaller(at, include=include, exclude=exclude) marshaller.marshall(self)
def export(self, at="string", format="xml", include=None, exclude=None): """Creates an "exportable" version of this object. p_format is "xml" by default, but can also be "csv". If p_format is: * "xml", if p_at is "string", this method returns the XML version, without the XML prologue. Else, (a) if not p_at, the XML will be exported on disk, in the OS temp folder, with an ugly name; (b) else, it will be exported at path p_at. * "csv", if p_at is "string", this method returns the CSV data as a string. If p_at is an opened file handler, the CSV line will be appended in it. If p_include is given, only fields whose names are in it will be included. p_exclude, if given, contains names of fields that will not be included in the result. """ if format == "xml": # Todo: take p_include and p_exclude into account. # Determine where to put the result toDisk = at != "string" if toDisk and not at: at = getOsTempFolder() + "/" + self.o.UID() + ".xml" # Create the XML version of the object marshaller = XmlMarshaller( cdata=True, dumpUnicode=True, dumpXmlPrologue=toDisk, rootTag=self.klass.__name__ ) xml = marshaller.marshall(self.o, objectType="appy") # Produce the desired result if toDisk: f = file(at, "w") f.write(xml.encode("utf-8")) f.close() return at else: return xml elif format == "csv": if isinstance(at, basestring): marshaller = CsvMarshaller(include=include, exclude=exclude) return marshaller.marshall(self) else: marshaller = CsvMarshaller(at, include=include, exclude=exclude) marshaller.marshall(self)
def marshall(self, instance, **kwargs): res = XmlMarshaller.marshall(self, instance, objectType='archetype') return (self.marshallContentType, len(res), res)
def main(): allocationReq1 = """<ns1:AllocationRequest xmlns:ns1="http://www.smals.be/XSD/OCMW_CPAS/HeatingAllocationRequest" xmlns:com="http://www.smals.be/XSD/OCMW_CPAS/HeatingAllocationDataTypes"><ns1:ComputeAllocation><ns1:PrimaryBeneficiary><ns1:SSIN>67031703807</ns1:SSIN><ns1:Category>1</ns1:Category></ns1:PrimaryBeneficiary><ns1:Invoice><com:Amount>8390100</com:Amount><com:Quantity>1000</com:Quantity><com:HousingNumber>1</com:HousingNumber><com:DeliveryDate>2011-09-19</com:DeliveryDate><com:UnitFuel>3</com:UnitFuel></ns1:Invoice></ns1:ComputeAllocation><ns1:OCMW_CPAS><ns1:KboBceNumber>0212344876</ns1:KboBceNumber></ns1:OCMW_CPAS></ns1:AllocationRequest>""" ssin = '67031703807' allocationReq = Object( ComputeAllocation=Object( Invoice=Object( Amount="8390100", Quantity=1000, HousingNumber=1, DeliveryDate="2011-09-19", UnitFuel=3), PrimaryBeneficiary=Object(SSIN=ssin,Category="1"), ), OCMW_CPAS=Object(KboBceNumber='0212344876') ) ns = dict( ns1="http://www.smals.be/XSD/OCMW_CPAS/HeatingAllocationRequest", com="http://www.smals.be/XSD/OCMW_CPAS/HeatingAllocationDataTypes") nst = dict() nst.update(AllocationRequest='ns1') nst.update(ComputeAllocation='ns1') nst.update(OCMW_CPAS='******') nst.update(KboBceNumber='ns1') nst.update(Invoice='ns1') nst.update(Quantity='com') nst.update(HousingNumber='com') nst.update(DeliveryDate='com') nst.update(UnitFuel='com') m = XmlMarshaller(namespaces=ns,namespacedTags=nst,dumpXmlPrologue=False,rootTag="AllocationRequest") allocationReq2 = m.marshall(allocationReq) #~ assert_equivalent(allocationReq1,allocationReq2) contenu1 = """<SSDNRequest xmlns="http://www.ksz-bcss.fgov.be/XSD/SSDN/Service"> <RequestContext><AuthorizedUser><UserID>00901732883</UserID><Email>[email protected]</Email><OrgUnit>0212344876</OrgUnit><MatrixID>17</MatrixID><MatrixSubID>1</MatrixSubID></AuthorizedUser><Message><Reference>630230001126766</Reference><TimeRequest>20110921T105230</TimeRequest></Message></RequestContext><ServiceRequest><ServiceId>OCMWCPASHeatingAllocation</ServiceId><Version>20090409</Version> %s </ServiceRequest></SSDNRequest>""" % allocationReq1 ssdnReq = Object( RequestContext=Object( AuthorizedUser=Object(**settings.SITE.bcss_user_params), Message=Object(Reference='630230001126766',TimeRequest='20110921T105230') ), ServiceRequest=Object( ServiceId="OCMWCPASHeatingAllocation", Version="20090409" ), _any=allocationReq2 ) m = AnyMarshaller(dumpXmlPrologue=False, rootTag='SSDNRequest', defaultNamespace="http://www.ksz-bcss.fgov.be/XSD/SSDN/Service") contenu2 = m.marshall(ssdnReq) assert_equivalent(contenu1,contenu2) body = Object( #~ xmlString="<![CDATA[%s]]>" % contenu) xmlString=contenu2) raise Exception("ok jusqu'ici") server = Resource('https://bcssksz-services-test.smals.be/connectors/webservice/KSZBCSSWebServiceConnectorPort',measure=True) res = server.soap(body,namespace="http://ksz-bcss.fgov.be/connectors/WebServiceConnector") print res.code print res.data s = str(res.data.xmlString) #~ s = res.data.xmlString.replace('"UTF-8"','"utf-8"') #~ s = s.replace('?>','?>\n') print s reply = XmlUnmarshaller().parse(s) import pdb pdb.set_trace()
def main(): allocationReq1 = """<ns1:AllocationRequest xmlns:ns1="http://www.smals.be/XSD/OCMW_CPAS/HeatingAllocationRequest" xmlns:com="http://www.smals.be/XSD/OCMW_CPAS/HeatingAllocationDataTypes"><ns1:ComputeAllocation><ns1:PrimaryBeneficiary><ns1:SSIN>67031703807</ns1:SSIN><ns1:Category>1</ns1:Category></ns1:PrimaryBeneficiary><ns1:Invoice><com:Amount>8390100</com:Amount><com:Quantity>1000</com:Quantity><com:HousingNumber>1</com:HousingNumber><com:DeliveryDate>2011-09-19</com:DeliveryDate><com:UnitFuel>3</com:UnitFuel></ns1:Invoice></ns1:ComputeAllocation><ns1:OCMW_CPAS><ns1:KboBceNumber>0212344876</ns1:KboBceNumber></ns1:OCMW_CPAS></ns1:AllocationRequest>""" ssin = '67031703807' allocationReq = Object(ComputeAllocation=Object( Invoice=Object(Amount="8390100", Quantity=1000, HousingNumber=1, DeliveryDate="2011-09-19", UnitFuel=3), PrimaryBeneficiary=Object(SSIN=ssin, Category="1"), ), OCMW_CPAS=Object(KboBceNumber='0212344876')) ns = dict( ns1="http://www.smals.be/XSD/OCMW_CPAS/HeatingAllocationRequest", com="http://www.smals.be/XSD/OCMW_CPAS/HeatingAllocationDataTypes") nst = dict() nst.update(AllocationRequest='ns1') nst.update(ComputeAllocation='ns1') nst.update(OCMW_CPAS='******') nst.update(KboBceNumber='ns1') nst.update(Invoice='ns1') nst.update(Quantity='com') nst.update(HousingNumber='com') nst.update(DeliveryDate='com') nst.update(UnitFuel='com') m = XmlMarshaller(namespaces=ns, namespacedTags=nst, dumpXmlPrologue=False, rootTag="AllocationRequest") allocationReq2 = m.marshall(allocationReq) #~ assert_equivalent(allocationReq1,allocationReq2) contenu1 = """<SSDNRequest xmlns="http://www.ksz-bcss.fgov.be/XSD/SSDN/Service"> <RequestContext><AuthorizedUser><UserID>00901732883</UserID><Email>[email protected]</Email><OrgUnit>0212344876</OrgUnit><MatrixID>17</MatrixID><MatrixSubID>1</MatrixSubID></AuthorizedUser><Message><Reference>630230001126766</Reference><TimeRequest>20110921T105230</TimeRequest></Message></RequestContext><ServiceRequest><ServiceId>OCMWCPASHeatingAllocation</ServiceId><Version>20090409</Version> %s </ServiceRequest></SSDNRequest>""" % allocationReq1 ssdnReq = Object(RequestContext=Object( AuthorizedUser=Object(**settings.SITE.bcss_user_params), Message=Object(Reference='630230001126766', TimeRequest='20110921T105230')), ServiceRequest=Object( ServiceId="OCMWCPASHeatingAllocation", Version="20090409"), _any=allocationReq2) ns = dict(xmlns="http://www.ksz-bcss.fgov.be/XSD/SSDN/Service") m = AnyMarshaller(namespaces=ns, dumpXmlPrologue=False, rootTag='SSDNRequest') contenu2 = m.marshall(ssdnReq) assert_equivalent(contenu1, contenu2) body = Object( #~ xmlString="<![CDATA[%s]]>" % contenu) xmlString=contenu2) raise Exception("ok jusqu'ici") server = Resource( 'https://bcssksz-services-test.smals.be/connectors/webservice/KSZBCSSWebServiceConnectorPort', measure=True) res = server.soap( body, namespace="http://ksz-bcss.fgov.be/connectors/WebServiceConnector") print res.code print res.data s = str(res.data.xmlString) #~ s = res.data.xmlString.replace('"UTF-8"','"utf-8"') #~ s = s.replace('?>','?>\n') print s reply = XmlUnmarshaller().parse(s) import pdb pdb.set_trace()