How to get unparsed XML from suds response, and the best django model field to use for storage

I use foam to request data from a third party using wsdl. I only save some data that is currently returned, but I pay for the data I receive, so I would like to save all this. I decided that the best way to save this data is to capture the original XML response in the database field as for future use, I have to decide that I want to start using different parts of the data and as a paper trace in case of discrepancies,

So, I have two questions:

Is there an easy way to output the raw received xml from a suds.client object? In my search for an answer to this, I found out that this can be done by logging, but I hoped that you would not have to pull this information out of the logs to place in the database field. I also looked at MessagePlugin.recieved () hook, but couldn't figure out how to access this information after parsing it, only so that I could override this function and access the raw XML object when it is parsed (which before as I decided whether it really is worth saving or not). I also studied the retxml option, but I would also like to use the collapsible version and make two separate calls: one as retxml and the other parsed will cost me twice. I was hoping for a simple function built into the suds client (e.g. response.as_xml () or something as simple as that), but so far have not found anything like it. The option, bubbling around in my head, may consist in expanding the client object with the .received () plug-in, which saves the xml as an object parameter before parsing it, for referencing it later ... but doing it seems a bit difficult for me now , and I hardly believe that the suds client hasn't just created it somewhere, so I thought I'd ask first.

Another part of my question is: what type of django model field is best suited for processing up to 100 kilobytes of text data as raw xml? I was going to just use a simple CharField with a silly long max_length, but that seems wrong.

Thanks in advance.

+4
python soap xml django suds
Oct 08 '14 at 21:33
source share
2 answers

I solved this using the retxml flag when initializing the client:

client = Client(settings.WSDL_ADDRESS, retxml=True) raw_reply = client.service.PersonSearch(soapified_search_object) 

Then I was able to save raw_reply as the source xml in django models.TextField () and then enter the source xml to get the result of the foam analysis without resubmitting my lika request:

 parsed_result = client.service.PersonSearch(__inject={'reply': raw_reply}) 

I suppose that if I wanted to remove the contents of the envelope with the foam from the raw answer, I could use the python xml library to further use the answer, but since my existing code already accepted the information that I wanted to get the result from the suds client I just used this.

Hope this helps someone else.

+6
09 Oct '14 at 16:44
source share

I used the kyrayzk solution for a while, but always considered it a bit hacky, since I only had to create a separate dummy client when I needed to process raw XML. Therefore, I kind of redefined the .last_received() and .last_sent() methods (which were (IMHO, mistakenly) deleted in suds-jurko 0.4.1) via MessagePlugin .

Hope this helps someone:

 class MyPlugin(MessagePlugin): def __init__(self): self.last_sent_raw = None self.last_received_raw = None def sending(self, context): self.last_sent_raw = str(context.envelope) def received(self, context): self.last_received_raw = str(context.reply) 

Using:

 plugin = MyPlugin() client = Client(TRTH_WSDL_URL, plugins=[plugin]) client.service.SendSomeRequest() print plugin.last_sent_raw print plugin.last_received_raw 

And as an extra, if you want XML with excellent indentation, try the following:

 from lxml import etree def xmlpprint(xml): return etree.tostring(etree.fromstring(xml), pretty_print=True) 
+3
Apr 20 '16 at 3:11
source share



All Articles