base: add cache to soap_client method (#81418)
This commit is contained in:
parent
60bcc9d82e
commit
faf3e4692e
|
@ -8,6 +8,7 @@ import logging
|
|||
import os
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
import uuid
|
||||
from contextlib import contextmanager
|
||||
|
@ -234,8 +235,23 @@ class BaseResource(models.Model):
|
|||
except AvailabilityParameters.DoesNotExist:
|
||||
return AvailabilityParameters(resource_type=resource_type, resource_pk=self.id)
|
||||
|
||||
soap_client_cache_timeout = 0
|
||||
soap_client_cache = {}
|
||||
|
||||
def soap_client(self, **kwargs):
|
||||
return passerelle.utils.soap.SOAPClient(resource=self, **kwargs)
|
||||
if self.soap_client_cache_timeout:
|
||||
key = (self, kwargs['wsdl_url'])
|
||||
if key in self.soap_client_cache:
|
||||
client, timestamp = self.soap_client_cache[key]
|
||||
if timestamp > time.time() - self.soap_client_cache_timeout:
|
||||
return client
|
||||
|
||||
client = passerelle.utils.soap.SOAPClient(resource=self, **kwargs)
|
||||
|
||||
if self.soap_client_cache_timeout:
|
||||
self.soap_client_cache[key] = client, time.time()
|
||||
|
||||
return client
|
||||
|
||||
@classmethod
|
||||
def get_verbose_name(cls):
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
# Copyright (C) 2021 Entr'ouvert
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Affero General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import pytest
|
||||
|
||||
from passerelle.base.models import BaseResource
|
||||
from tests.utils import ResponsesSoap
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def dummy_resource_class():
|
||||
class DummyResource(BaseResource):
|
||||
class logging_parameters:
|
||||
log_level = 30
|
||||
trace_emails = ''
|
||||
|
||||
def down(self):
|
||||
return False
|
||||
|
||||
class Meta:
|
||||
app_label = 'tests'
|
||||
|
||||
return DummyResource
|
||||
|
||||
|
||||
def test_soap_client_method(dummy_resource_class):
|
||||
with open('passerelle/contrib/toulouse_maelis/tools/wsdl/ActivityService.wsdl', 'rb') as fd:
|
||||
wsdl_url = 'https://example.org/ActivityService?wsdl'
|
||||
responses_soap = ResponsesSoap(
|
||||
wsdl_url=wsdl_url,
|
||||
wsdl_content=fd.read(),
|
||||
)
|
||||
with responses_soap():
|
||||
resource = dummy_resource_class(
|
||||
pk='x'
|
||||
) # pk is necessary for the instance to be hashable and used a key in the soap_client cache
|
||||
assert resource.soap_client(wsdl_url=wsdl_url) != resource.soap_client(wsdl_url=wsdl_url)
|
||||
|
||||
dummy_resource_class.soap_client_cache_timeout = 300
|
||||
assert resource.soap_client(wsdl_url=wsdl_url) == resource.soap_client(wsdl_url=wsdl_url)
|
Loading…
Reference in New Issue