Plugins¶
New in version 0.15.
You can write plugins for zeep which can be used to process/modify data before it is sent to the server (egress) and after it is received (ingress).
Writing a plugin is really simple and best explained via an example.
from lxml import etree
from zeep import Plugin
class MyLoggingPlugin(Plugin):
def ingress(self, envelope, http_headers, operation):
print(etree.tostring(envelope, pretty_print=True))
return envelope, http_headers
def egress(self, envelope, http_headers, operation, binding_options):
print(etree.tostring(envelope, pretty_print=True))
return envelope, http_headers
The plugin can implement two methods: ingress
and egress
. Both methods
should always return an envelop (lxml element) and the http headers. The
envelope in the egress
plugin will only contain the body of the soap message.
This is important to remember if you want to inspect or do something
with the headers.
To register this plugin you need to pass it to the client. Plugins are always executed sequentially.
from zeep import Client
client = Client(..., plugins=[MyLoggingPlugin()])
HistoryPlugin¶
New in version 0.15.
The history plugin keep a list of sent and received requests. By default at
most one transaction (sent/received) is kept. But this can be changed when you
create the plugin by passing the maxlen
kwarg.
from zeep import Client
from zeep.plugins import HistoryPlugin
history = HistoryPlugin()
client = Client(
'http://examples.python-zeep.org/basic.wsdl',
plugins=[history])
client.service.DoSomething()
print(history.last_sent)
print(history.last_received)