Skip to content

Commit

Permalink
Merge pull request #35 from iconfinder/feature/request-timeout
Browse files Browse the repository at this point in the history
Add default request timeout.
  • Loading branch information
JanmanX authored Jan 5, 2021
2 parents 63ea7d2 + f8d8ce5 commit e6f1646
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
35 changes: 23 additions & 12 deletions pyvat/registries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import requests
import xml.dom.minidom

from requests import Timeout

from .result import VatNumberCheckResult
from .xml_utils import get_first_child_element, get_text, NodeNotFoundError
from .exceptions import ServerError
Expand Down Expand Up @@ -33,6 +36,9 @@ class ViesRegistry(Registry):
"""URL for the VAT checking service.
"""

DEFAULT_TIMEOUT = 8
"""Timeout for the requests."""

def check_vat_number(self, vat_number, country_code):
# Non-ISO code used for Greece.
if country_code == 'GR':
Expand All @@ -42,15 +48,15 @@ def check_vat_number(self, vat_number, country_code):
result = VatNumberCheckResult()

request_data = (
u'<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope'
u' xmlns:ns0="urn:ec.europa.eu:taxud:vies:services:checkVa'
u't:types" xmlns:ns1="http://schemas.xmlsoap.org/soap/enve'
u'lope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta'
u'nce" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/env'
u'elope/"><SOAP-ENV:Header/><ns1:Body><ns0:checkVat><ns0:c'
u'ountryCode>%s</ns0:countryCode><ns0:vatNumber>%s</ns0:va'
u'tNumber></ns0:checkVat></ns1:Body></SOAP-ENV:Envelope>' %
(country_code, vat_number)
u'<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope'
u' xmlns:ns0="urn:ec.europa.eu:taxud:vies:services:checkVa'
u't:types" xmlns:ns1="http://schemas.xmlsoap.org/soap/enve'
u'lope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta'
u'nce" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/env'
u'elope/"><SOAP-ENV:Header/><ns1:Body><ns0:checkVat><ns0:c'
u'ountryCode>%s</ns0:countryCode><ns0:vatNumber>%s</ns0:va'
u'tNumber></ns0:checkVat></ns1:Body></SOAP-ENV:Envelope>' %
(country_code, vat_number)
)

result.log_lines += [
Expand All @@ -64,8 +70,13 @@ def check_vat_number(self, vat_number, country_code):
data=request_data.encode('utf-8'),
headers={
'Content-Type': 'text/xml; charset=utf-8',
}
},
timeout=self.DEFAULT_TIMEOUT
)
except Timeout as e:
result.log_lines.append(u'< Request to EU VIEW registry timed out:'
u' {}'.format(e))
return result
except Exception as exception:
# Do not completely fail problematic requests.
result.log_lines.append(u'< Request failed with exception: %r' %
Expand All @@ -77,11 +88,11 @@ def check_vat_number(self, vat_number, country_code):
u'< Response with status %d of content type %s:' %
(response.status_code, response.headers['Content-Type']),
response.text,
]
]

# Do not completely fail problematic requests.
if response.status_code != 200 or \
not response.headers['Content-Type'].startswith('text/xml'):
not response.headers['Content-Type'].startswith('text/xml'):
result.log_lines.append(u'< Response is nondeterministic due to '
u'invalid response status code or MIME '
u'type')
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
long_description_content_type='text/x-rst',
author='Iconfinder',
author_email='[email protected]',
url = 'https://www.iconfinder.com',
project_urls = {
url='https://www.iconfinder.com',
project_urls={
'Issue Tracker': 'https://github.com/iconfinder/pyvat/issues',
},
packages=packages,
Expand Down

0 comments on commit e6f1646

Please sign in to comment.