0001-utils-move-SOAP-related-classes-to-passerelle.utils..patch
passerelle/base/models.py | ||
---|---|---|
182 | 182 |
resource_pk=self.id) |
183 | 183 | |
184 | 184 |
def soap_client(self, **kwargs): |
185 |
return passerelle.utils.SOAPClient(resource=self, **kwargs) |
|
185 |
return passerelle.utils.soap.SOAPClient(resource=self, **kwargs)
|
|
186 | 186 | |
187 | 187 |
@classmethod |
188 | 188 |
def get_verbose_name(cls): |
passerelle/utils/__init__.py | ||
---|---|---|
21 | 21 |
from itertools import islice, chain |
22 | 22 |
import warnings |
23 | 23 | |
24 |
from requests import Session as RequestSession, Response as RequestResponse, RequestException
|
|
24 |
from requests import Session as RequestSession, Response as RequestResponse |
|
25 | 25 |
from requests.structures import CaseInsensitiveDict |
26 | 26 |
from urllib3.exceptions import InsecureRequestWarning |
27 |
from django.utils.six.moves.urllib import parse as urlparse |
|
28 | 27 | |
29 | 28 |
from django.conf import settings |
30 | 29 |
from django.core.cache import cache |
... | ... | |
35 | 34 |
from django.views.generic.detail import SingleObjectMixin |
36 | 35 |
from django.contrib.contenttypes.models import ContentType |
37 | 36 |
from django.db import transaction |
38 |
from zeep import Client |
|
39 |
from zeep.cache import InMemoryCache |
|
40 |
from zeep.transports import Transport |
|
41 | 37 | |
42 | 38 |
from passerelle.base.models import ApiUser, AccessRight, BaseResource |
43 | 39 |
from passerelle.base.signature import check_query, check_url |
... | ... | |
272 | 268 |
log_http_request(self.logger, request=request, response=response, exception=exception, error_log=error_log) |
273 | 269 | |
274 | 270 | |
275 |
class SOAPTransport(Transport): |
|
276 |
"""Wrapper around zeep.Transport |
|
277 | ||
278 |
disable basic_authentication hosts unrelated to wsdl's endpoints |
|
279 |
""" |
|
280 |
def __init__(self, resource, wsdl_url, **kwargs): |
|
281 |
self.resource = resource |
|
282 |
self.wsdl_host = urlparse.urlparse(wsdl_url).netloc |
|
283 |
super(SOAPTransport, self).__init__(**kwargs) |
|
284 | ||
285 |
def _load_remote_data(self, url): |
|
286 |
try: |
|
287 |
if urlparse.urlparse(url).netloc != self.wsdl_host: |
|
288 |
response = self.session.get(url, timeout=self.load_timeout, auth=None, cert=None) |
|
289 |
response.raise_for_status() |
|
290 |
return response.content |
|
291 |
return super(SOAPTransport, self)._load_remote_data(url) |
|
292 |
except RequestException as e: |
|
293 |
# prevent import cycle |
|
294 |
from passerelle.utils.soap import SOAPError |
|
295 |
raise SOAPError('SOAP service is down, location %r cannot be loaded: %s' % (url, e), exception=e, url=url) |
|
296 | ||
297 | ||
298 |
class SOAPClient(Client): |
|
299 |
"""Wrapper around zeep.Client |
|
300 | ||
301 |
resource muste have a wsdl_url and a requests attribute |
|
302 |
""" |
|
303 |
def __init__(self, resource, **kwargs): |
|
304 |
wsdl_url = kwargs.pop('wsdl_url', None) or resource.wsdl_url |
|
305 |
transport_class = getattr(resource, 'soap_transport_class', SOAPTransport) |
|
306 |
transport = transport_class(resource, wsdl_url, session=resource.requests, cache=InMemoryCache()) |
|
307 |
super(SOAPClient, self).__init__(wsdl_url, transport=transport, **kwargs) |
|
308 | ||
309 | 271 | |
310 | 272 |
def export_site(): |
311 | 273 |
'''Dump passerelle configuration (users, resources and ACLs) to JSON dumpable dictionnary''' |
passerelle/utils/soap.py | ||
---|---|---|
1 |
# passerelle - uniform access to multiple data sources and services |
|
1 | 2 |
# Copyright (C) 2019 Entr'ouvert |
2 | 3 |
# |
3 | 4 |
# This program is free software: you can redistribute it and/or modify it |
... | ... | |
13 | 14 |
# You should have received a copy of the GNU Affero General Public License |
14 | 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 | 16 | |
17 |
from requests import RequestException |
|
18 | ||
19 |
from zeep import Client |
|
20 |
from zeep.cache import InMemoryCache |
|
21 |
from zeep.transports import Transport |
|
22 | ||
23 |
from django.utils.six.moves.urllib import parse as urlparse |
|
16 | 24 | |
17 | 25 |
from passerelle.utils.jsonresponse import APIError |
18 | 26 | |
19 | 27 | |
20 | 28 |
class SOAPError(APIError): |
21 | 29 |
pass |
30 | ||
31 | ||
32 |
class SOAPClient(Client): |
|
33 |
"""Wrapper around zeep.Client |
|
34 | ||
35 |
resource muste have a wsdl_url and a requests attribute |
|
36 |
""" |
|
37 |
def __init__(self, resource, **kwargs): |
|
38 |
wsdl_url = kwargs.pop('wsdl_url', None) or resource.wsdl_url |
|
39 |
transport_class = getattr(resource, 'soap_transport_class', SOAPTransport) |
|
40 |
transport = transport_class(resource, wsdl_url, session=resource.requests, cache=InMemoryCache()) |
|
41 |
super(SOAPClient, self).__init__(wsdl_url, transport=transport, **kwargs) |
|
42 | ||
43 | ||
44 |
class SOAPTransport(Transport): |
|
45 |
"""Wrapper around zeep.Transport |
|
46 | ||
47 |
disable basic_authentication hosts unrelated to wsdl's endpoints |
|
48 |
""" |
|
49 |
def __init__(self, resource, wsdl_url, **kwargs): |
|
50 |
self.resource = resource |
|
51 |
self.wsdl_host = urlparse.urlparse(wsdl_url).netloc |
|
52 |
super(SOAPTransport, self).__init__(**kwargs) |
|
53 | ||
54 |
def _load_remote_data(self, url): |
|
55 |
try: |
|
56 |
if urlparse.urlparse(url).netloc != self.wsdl_host: |
|
57 |
response = self.session.get(url, timeout=self.load_timeout, auth=None, cert=None) |
|
58 |
response.raise_for_status() |
|
59 |
return response.content |
|
60 |
return super(SOAPTransport, self)._load_remote_data(url) |
|
61 |
except RequestException as e: |
|
62 |
raise SOAPError('SOAP service is down, location %r cannot be loaded: %s' % (url, e), exception=e, url=url) |
tests/test_soap.py | ||
---|---|---|
1 | 1 |
import requests |
2 | 2 |
from zeep.plugins import Plugin |
3 | 3 | |
4 |
from passerelle.utils import SOAPClient |
|
4 |
from passerelle.utils.soap import SOAPClient
|
|
5 | 5 | |
6 | 6 | |
7 | 7 |
WSDL = 'tests/data/soap.wsdl' |
8 |
- |