8000 Add a check_uid() function to the stdnum.ch.uid module · unho/python-stdnum@a218032 · GitHub
[go: up one dir, main page]

Skip to content

Commit a218032

Browse files
committed
< 8000 span class="ws-pre-wrap f5 wb-break-word text-mono prc-Text-Text-0ima0">
Add a check_uid() function to the stdnum.ch.uid module
This function can be used to performa a lookup of organisation information by the Swiss Federal Statistical Office web service. Related to arthurdejong#336
1 parent 1364e19 commit a218032

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

stdnum/ch/uid.py

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# uid.py - functions for handling Swiss business identifiers
22
# coding: utf-8
33
#
4-
# Copyright (C) 2015 Arthur de Jong
4+
# Copyright (C) 2015-2022 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
@@ -43,7 +43,7 @@
4343
"""
4444

4545
from stdnum.exceptions import *
46-
from stdnum.util import clean, isdigits
46+
from stdnum.util import clean, get_soap_client, isdigits
4747

4848

4949
def compact(number):
@@ -88,3 +88,60 @@ def format(number):
8888
number = compact(number)
8989
return number[:3] + '-' + '.'.join(
9090
number[i:i + 3] for i in range(3, len(number), 3))
91+
92+
93+
uid_wsdl = 'https://www.uid-wse.admin.ch/V5.0/PublicServices.svc?wsdl'
94+
95+
96+
def check_uid(number, timeout=30): # pragma: no cover
97+
"""Look up information via the Swiss Federal Statistical Office web service.
98+
99+
This uses the UID registry web service run by the the Swiss Federal
100+
Statistical Office to provide more details on the provided number.
101+
102+
Returns a dict-like object for valid numbers with the following structure::
103+
104+
{
105+
'organisation': {
106+
'organisationIdentification': {
107+
'uid': {'uidOrganisationIdCategorie': 'CHE', 'uidOrganisationId': 113690319},
108+
'OtherOrganisationId': [
109+
{'organisationIdCategory': 'CH.ESTVID', 'organisationId': '052.0111.1006'},
110+
],
111+
'organisationName': 'Staatssekretariat für Migration SEM Vermietung von Parkplätzen',
112+
'legalForm': '0220',
113+
},
114+
'address': [
115+
{
116+
'addressCategory': 'LEGAL',
117+
'street': 'Quellenweg',
118+
'houseNumber': '6',
119+
'town': 'Wabern',
120+
'countryIdISO2': 'CH',
121+
},
122+
],
123+
},
124+
'uidregInformation': {
125+
'uidregStatusEnterpriseDetail': '3',
126+
...
127+
},
128+
'vatRegisterInformation': {
129+
'vatStatus': '2',
130+
'vatEntryStatus': '1',
131+
...
132+
},
133+
}
134+
135+
See the following document for more details on the GetByUID return value
136+
https://www.bfs.admin.ch/bfs/en/home/registers/enterprise-register/enterprise-identification/uid-register/uid-interfaces.html
137+
"""
138+
# this function isn't always tested because it would require network access
139+
# for the tests and might unnecessarily load the web service
140+
number = compact(number)
141+
client = get_soap_client(uid_wsdl, timeout)
142+
try:
143+
return client.GetByUID(uid={'uidOrganisationIdCategorie': number[:3], 'uidOrganisationId': number[3:]})[0]
144+
except Exception: # noqa: B902 (excpetion type depends on SOAP client)
145+
# Error responses by the server seem to result in exceptions raised
146+
# by the SOAP client implementation
147+
return

tests/test_ch_uid.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# test_eu_vat.py - functions for testing the UID Webservice
2+
# coding: utf-8
3+
#
4+
# Copyright (C) 2022 Arthur de Jong
5+
#
6+
# This library is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU Lesser General Public
8+
# License as published by the Free Software Foundation; either
9+
# version 2.1 of the License, or (at your option) any later version.
10+
#
11+
# This library is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public
17+
# License along with this library; if not, write to the Free Software
18+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19+
# 02110-1301 USA
20+
21+
# This is a separate test file because it should not be run regularly
22+
# because it could negatively impact the VIES service.
23+
24+
"""Extra tests for the stdnum.ch.uid module."""
25+
26+
import os
27+
import unittest
28+
29+
from stdnum. 685C ch import uid
30+
31+
32+
@unittest.skipIf(
33+
not os.environ.get('ONLINE_TESTS'),
34+
'Do not overload online services')
35+
class TestUid(unittest.TestCase):
36+
"""Test the UID Webservice provided by the Swiss Federal Statistical
37+
Office for validating UID numbers."""
38+
39+
def test_check_uid(self):
40+
"""Test stdnum.ch.uid.check_uid()"""
41+
result = uid.check_uid('CHE113690319')
42+
self.assertTrue(result)
43+
self.assertEqual(result['organisation']['organisationIdentification']['uid']['uidOrganisationId'], 113690319)
44+
self.assertEqual(result['organisation']['organisationIdentification']['legalForm'], '0220')
45+
self.assertEqual(result['vatRegisterInformation']['vatStatus'], '2')

0 commit comments

Comments
 (0)
0