Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2017-10-14 18:55:20 +0200
committerArthur de Jong <arthur@arthurdejong.org>2017-10-22 21:22:00 +0200
commit4ab1e3b538460078c87878b730b2ac06e6d1f9cd (patch)
treecc50d29a27432a27fe2808e9e4bd9d688e0d7887 /stdnum
parentcecd35cbce73ab166394352f75f85b4f83de367f (diff)
Cache SOAP client in get_soap_client()
This caches the instantiated SOAP client classes in the util module instead of doing the caching in every module that performs requests.
Diffstat (limited to 'stdnum')
-rw-r--r--stdnum/eu/vat.py19
-rw-r--r--stdnum/tr/tckimlik.py17
-rw-r--r--stdnum/util.py33
3 files changed, 26 insertions, 43 deletions
diff --git a/stdnum/eu/vat.py b/stdnum/eu/vat.py
index 3a6a2eb..8f25841 100644
--- a/stdnum/eu/vat.py
+++ b/stdnum/eu/vat.py
@@ -56,9 +56,6 @@ _country_modules = dict()
vies_wsdl = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl'
"""The WSDL URL of the VAT Information Exchange System (VIES)."""
-# a cached version of the suds client for VIES
-_vies_client = None
-
def _get_cc_module(cc):
"""Get the VAT number module based on the country code."""
@@ -112,16 +109,6 @@ def guess_country(number):
if _get_cc_module(cc).is_valid(number)]
-def _get_client(): # pragma: no cover (no tests for this function)
- """Get a SOAP client for performing VIES requests."""
- # this function isn't automatically tested because the functions using
- # it are not automatically tested
- global _vies_client
- if _vies_client is None:
- _vies_client = get_soap_client(vies_wsdl)
- return _vies_client
-
-
def check_vies(number): # pragma: no cover (no tests for this function)
"""Query the online European Commission VAT Information Exchange System
(VIES) for validity of the provided number. Note that the service has
@@ -130,7 +117,8 @@ def check_vies(number): # pragma: no cover (no tests for this function)
# this function isn't automatically tested because it would require
# network access for the tests and unnecessarily load the VIES website
number = compact(number)
- return _get_client().checkVat(number[:2], number[2:])
+ client = get_soap_client(vies_wsdl)
+ return client.checkVat(number[:2], number[2:])
def check_vies_approx(number, requester): # pragma: no cover
@@ -143,6 +131,7 @@ def check_vies_approx(number, requester): # pragma: no cover
# network access for the tests and unnecessarily load the VIES website
number = compact(number)
requester = compact(requester)
- return _get_client().checkVatApprox(
+ client = get_soap_client(vies_wsdl)
+ return client.checkVatApprox(
countryCode=number[:2], vatNumber=number[2:],
requesterCountryCode=requester[:2], requesterVatNumber=requester[2:])
diff --git a/stdnum/tr/tckimlik.py b/stdnum/tr/tckimlik.py
index 26eebf3..dc35922 100644
--- a/stdnum/tr/tckimlik.py
+++ b/stdnum/tr/tckimlik.py
@@ -53,10 +53,6 @@ tckimlik_wsdl = 'https://tckimlik.nvi.gov.tr/Service/KPSPublic.asmx?WSDL'
"""The WSDL URL of the T.C. Kimlik validation service."""
-# a cached version of the SOAP client for Kimlik validation
-_tckimlik_client = None
-
-
def compact(number):
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
@@ -93,16 +89,6 @@ def is_valid(number):
return False
-def _get_client(): # pragma: no cover (no tests for this function)
- """Get a SOAP client for performing T.C. Kimlik validation."""
- # this function isn't automatically tested because the functions using
- # it are not automatically tested
- global _tckimlik_client
- if _tckimlik_client is None:
- _tckimlik_client = get_soap_client(tckimlik_wsdl)
- return _tckimlik_client
-
-
def check_kps(number, name, surname, birth_year): # pragma: no cover
"""Query the online T.C. Kimlik validation service run by the Directorate
of Population and Citizenship Affairs. This returns a boolean but may
@@ -110,7 +96,8 @@ def check_kps(number, name, surname, birth_year): # pragma: no cover
# this function isn't automatically tested because it would require
# network access for the tests and unnecessarily load the online service
number = compact(number)
- result = _get_client().TCKimlikNoDogrula(
+ client = get_soap_client(tckimlik_wsdl)
+ result = client.TCKimlikNoDogrula(
TCKimlikNo=number, Ad=name, Soyad=surname, DogumYili=birth_year)
if hasattr(result, 'get'):
return result.get('TCKimlikNoDogrulaResult')
diff --git a/stdnum/util.py b/stdnum/util.py
index 7338557..b410d4f 100644
--- a/stdnum/util.py
+++ b/stdnum/util.py
@@ -165,19 +165,26 @@ def get_cc_module(cc, name):
return
+# this is a cache of SOAP clients
+_soap_clients = {}
+
+
def get_soap_client(wsdlurl): # pragma: no cover (no tests for this function)
- """Get a SOAP client for performing requests."""
+ """Get a SOAP client for performing requests. The client is cached."""
# this function isn't automatically tested because the functions using
# it are not automatically tested
- try:
- from urllib import getproxies
- except ImportError:
- from urllib.request import getproxies
- # try suds first
- try:
- from suds.client import Client
- return Client(wsdlurl, proxy=getproxies()).service
- except ImportError:
- # fall back to using pysimplesoap
- from pysimplesoap.client import SoapClient
- return SoapClient(wsdl=wsdlurl, proxy=getproxies())
+ if wsdlurl not in _soap_clients:
+ try:
+ from urllib import getproxies
+ except ImportError:
+ from urllib.request import getproxies
+ # try suds first
+ try:
+ from suds.client import Client
+ client = Client(wsdlurl, proxy=getproxies()).service
+ except ImportError:
+ # fall back to using pysimplesoap
+ from pysimplesoap.client import SoapClient
+ client = SoapClient(wsdl=wsdlurl, proxy=getproxies())
+ _soap_clients[wsdlurl] = client
+ return _soap_clients[wsdlurl]