Skip to content

Commit feab917

Browse files
committed
Implement online TC Kimlik check
This refactors out the SOAP client function that was implemented for VIES to the stdnum.utils module.
1 parent 619b097 commit feab917

3 files changed

Lines changed: 54 additions & 15 deletions

File tree

stdnum/eu/vat.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"""
4141

4242
from stdnum.exceptions import *
43-
from stdnum.util import clean, get_vat_module
43+
from stdnum.util import clean, get_vat_module, get_soap_client
4444

4545

4646
country_codes = set([
@@ -118,18 +118,7 @@ def _get_client(): # pragma: no cover (no tests for this function)
118118
# it are not automatically tested
119119
global _vies_client
120120
if _vies_client is None:
121-
try:
122-
from urllib import getproxies
123-
except ImportError:
124-
from urllib.request import getproxies
125-
# try suds first
126-
try:
127-
from suds.client import Client
128-
_vies_client = Client(vies_wsdl, proxy=getproxies()).service
129-
except ImportError:
130-
# fall back to using pysimplesoap
131-
from pysimplesoap.client import SoapClient
132-
_vies_client = SoapClient(wsdl=vies_wsdl, proxy=getproxies())
121+
_vies_client = get_soap_client(vies_wsdl)
133122
return _vies_client
134123

135124

stdnum/tr/tckimlik.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@
4545
"""
4646

4747
from stdnum.exceptions import *
48-
from stdnum.util import clean
48+
from stdnum.util import clean, get_soap_client
49+
50+
51+
tckimlik_wsdl = 'https://tckimlik.nvi.gov.tr/Service/KPSPublic.asmx?WSDL'
52+
"""The WSDL URL of the T.C. Kimlik validation service."""
53+
54+
55+
# a cached version of the SOAP client for Kimlik validation
56+
_tckimlik_client = None
4957

5058

5159
def compact(number):
@@ -83,3 +91,27 @@ def is_valid(number):
8391
return bool(validate(number))
8492
except ValidationError:
8593
return False
94+
95+
96+
def _get_client(): # pragma: no cover (no tests for this function)
97+
"""Get a SOAP client for performing T.C. Kimlik validation."""
98+
# this function isn't automatically tested because the functions using
99+
# it are not automatically tested
100+
global _tckimlik_client
101+
if _tckimlik_client is None:
102+
_tckimlik_client = get_soap_client(tckimlik_wsdl)
103+
return _tckimlik_client
104+
105+
106+
def check_kps(number, name, surname, birth_year): # pragma: no cover
107+
"""Queries the online T.C. Kimlik validation service run by the
108+
Directorate of Population and Citizenship Affairs. This returns a boolean
109+
but may raise a SOAP exception for missing or invalid values."""
110+
# this function isn't automatically tested because it would require
111+
# network access for the tests and unnecessarily load the online service
112+
number = compact(number)
113+
result = _get_client().TCKimlikNoDogrula(
114+
TCKimlikNo=number, Ad=name, Soyad=surname, DogumYili=birth_year)
115+
if hasattr(result, 'get'):
116+
return result.get('TCKimlikNoDogrulaResult')
117+
return result

stdnum/util.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# util.py - common utility functions
22
# coding: utf-8
33
#
4-
# Copyright (C) 2012, 2013, 2015 Arthur de Jong
4+
# Copyright (C) 2012-2016 Arthur de Jong
55
#
66
# This library is free software; you can redistribute it and/or
77
# modify it under the terms of the GNU Lesser General Public
@@ -178,3 +178,21 @@ def get_vat_module(cc):
178178
cc = cc.lower()
179179
cc = _cc_translations.get(cc, cc)
180180
return __import__('stdnum.%s' % cc, globals(), locals(), ['vat']).vat
181+
182+
183+
def get_soap_client(wsdlurl): # pragma: no cover (no tests for this function)
184+
"""Get a SOAP client for performing requests."""
185+
# this function isn't automatically tested because the functions using
186+
# it are not automatically tested
187+
try:
188+
from urllib import getproxies
189+
except ImportError:
190+
from urllib.request import getproxies
191+
# try suds first
192+
try:
193+
from suds.client import Client
194+
return Client(wsdlurl, proxy=getproxies()).service
195+
except ImportError:
196+
# fall back to using pysimplesoap
197+
from pysimplesoap.client import SoapClient
198+
return SoapClient(wsdl=wsdlurl, proxy=getproxies())

0 commit comments

Comments
 (0)