From 6f39c4363b444e41b74b740b9b66ed67a53fde59 Mon Sep 17 00:00:00 2001 From: Emmanuel Cazenave Date: Mon, 25 Jun 2018 16:59:38 +0200 Subject: [PATCH] start a SOAP common infrastructure (#16104) --- debian/control | 3 +- passerelle/base/models.py | 3 + passerelle/utils/__init__.py | 13 ++++ setup.py | 3 +- tests/data/soap.wsdl | 130 +++++++++++++++++++++++++++++++++++ tests/test_soap.py | 35 ++++++++++ 6 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 tests/data/soap.wsdl create mode 100644 tests/test_soap.py diff --git a/debian/control b/debian/control index 6a0f016..c83bd31 100644 --- a/debian/control +++ b/debian/control @@ -27,7 +27,8 @@ Depends: ${python:Depends}, python-lxml, python-dateutil, python-pyproj, - python-pil + python-pil, + python-zeep Recommends: python-soappy, python-phpserialize Description: Uniform access to multiple data sources and services (Python module) diff --git a/passerelle/base/models.py b/passerelle/base/models.py index dc6c57a..5cb943e 100644 --- a/passerelle/base/models.py +++ b/passerelle/base/models.py @@ -165,6 +165,9 @@ class BaseResource(models.Model): def requests(self): return passerelle.utils.Request(resource=self, logger=self.logger) + def soap_client(self, **kwargs): + return passerelle.utils.SOAPClient(resource=self, **kwargs) + @classmethod def get_verbose_name(cls): return cls._meta.verbose_name diff --git a/passerelle/utils/__init__.py b/passerelle/utils/__init__.py index d054903..28c69a5 100644 --- a/passerelle/utils/__init__.py +++ b/passerelle/utils/__init__.py @@ -18,6 +18,9 @@ from django.utils.decorators import available_attrs from django.views.generic.detail import SingleObjectMixin from django.contrib.contenttypes.models import ContentType from django.db import transaction +from zeep import Client +from zeep.cache import InMemoryCache +from zeep.transports import Transport from passerelle.base.context_processors import template_vars from passerelle.base.models import ApiUser, AccessRight, BaseResource @@ -264,6 +267,16 @@ class Request(RequestSession): log_function(message, extra=extra) +class SOAPClient(Client): + """Wrapper around zeep.Client + + resource muste have a wsdl_url and a requests attribute + """ + def __init__(self, resource, **kwargs): + transport = Transport(session=resource.requests, cache=InMemoryCache()) + super(SOAPClient, self).__init__(resource.wsdl_url, transport=transport, **kwargs) + + def export_site(): '''Dump passerelle configuration (users, resources and ACLs) to JSON dumpable dictionnary''' d = {} diff --git a/setup.py b/setup.py index 676dc52..2a352a9 100755 --- a/setup.py +++ b/setup.py @@ -102,7 +102,8 @@ setup(name='passerelle', 'lxml', 'python-dateutil', 'Pillow', - 'python-magic' + 'python-magic', + 'zeep' ], cmdclass={ 'build': build, diff --git a/tests/data/soap.wsdl b/tests/data/soap.wsdl new file mode 100644 index 0000000..5783270 --- /dev/null +++ b/tests/data/soap.wsdl @@ -0,0 +1,130 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + My first service + + + + + diff --git a/tests/test_soap.py b/tests/test_soap.py new file mode 100644 index 0000000..15b67ed --- /dev/null +++ b/tests/test_soap.py @@ -0,0 +1,35 @@ +import requests +from zeep.settings import Settings +from zeep.plugins import Plugin + +from passerelle.utils import SOAPClient + + +WSDL = 'tests/data/soap.wsdl' + + +class FooPlugin(Plugin): + pass + + +class BarPlugin(Plugin): + pass + + +class SOAPResource(object): + + def __init__(self): + self.requests = requests.Session() + self.wsdl_url = WSDL + + +def test_soap_client(): + soap_resource = SOAPResource() + settings = Settings(strict=False) + plugins = [FooPlugin, BarPlugin] + client = SOAPClient(soap_resource, settings=settings, plugins=plugins) + assert client.wsdl.location.endswith(WSDL) + assert client.transport.session == soap_resource.requests + assert client.transport.cache + assert client.settings.strict is False + assert client.plugins == plugins -- 2.17.1