timeout implementation (#52)

Signed-off-by: Mateusz Susik <mateusz.susik@sigmoidal.io>
This commit is contained in:
Mateusz Susik 2017-12-30 14:56:50 +01:00 committed by ssut
parent 8105d07269
commit 57076b5ad8
3 changed files with 51 additions and 1 deletions

17
googletrans/adapters.py Normal file
View File

@ -0,0 +1,17 @@
from requests.adapters import HTTPAdapter
class TimeoutAdapter(HTTPAdapter):
"""HTTP adapter that adds timeout to each query."""
def __init__(self, timeout=None, *args, **kwargs):
"""HTTP adapter that adds timeout to each query.
:param timeout: Timeout that will be added to each query
"""
self.timeout = timeout
super(TimeoutAdapter, self).__init__(*args, **kwargs)
def send(self, *args, **kwargs):
kwargs['timeout'] = self.timeout
print(self.timeout)
return super(TimeoutAdapter, self).send(*args, **kwargs)

View File

@ -8,6 +8,7 @@ import requests
import random import random
from googletrans import urls, utils from googletrans import urls, utils
from googletrans.adapters import TimeoutAdapter
from googletrans.compat import PY3 from googletrans.compat import PY3
from googletrans.gtoken import TokenAcquirer from googletrans.gtoken import TokenAcquirer
from googletrans.constants import DEFAULT_USER_AGENT, LANGCODES, LANGUAGES, SPECIAL_CASES from googletrans.constants import DEFAULT_USER_AGENT, LANGCODES, LANGUAGES, SPECIAL_CASES
@ -32,15 +33,26 @@ class Translator(object):
:param proxies: proxies configuration. :param proxies: proxies configuration.
Dictionary mapping protocol or protocol and host to the URL of the proxy Dictionary mapping protocol or protocol and host to the URL of the proxy
For example ``{'http': 'foo.bar:3128', 'http://host.name': 'foo.bar:4012'}`` For example ``{'http': 'foo.bar:3128', 'http://host.name': 'foo.bar:4012'}``
:type proxies: dictionary
:param timeout: Definition of timeout for Requests library.
Will be used by every request.
:type timeout: number or a double of numbers
""" """
def __init__(self, service_urls=None, user_agent=DEFAULT_USER_AGENT, proxies=None): def __init__(self, service_urls=None, user_agent=DEFAULT_USER_AGENT,
proxies=None, timeout=None):
self.session = requests.Session() self.session = requests.Session()
if proxies is not None: if proxies is not None:
self.session.proxies = proxies self.session.proxies = proxies
self.session.headers.update({ self.session.headers.update({
'User-Agent': user_agent, 'User-Agent': user_agent,
}) })
if timeout is not None:
self.session.mount('https://', TimeoutAdapter(timeout))
self.session.mount('http://', TimeoutAdapter(timeout))
self.service_urls = service_urls or ['translate.google.com'] self.service_urls = service_urls or ['translate.google.com']
self.token_acquirer = TokenAcquirer(session=self.session, host=self.service_urls[0]) self.token_acquirer = TokenAcquirer(session=self.session, host=self.service_urls[0])

View File

@ -1,5 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from pytest import raises from pytest import raises
from requests.exceptions import ConnectionError
from requests.exceptions import ReadTimeout
from googletrans import Translator from googletrans import Translator
@ -112,3 +114,22 @@ def test_dest_not_in_supported_languages(translator):
with raises(ValueError): with raises(ValueError):
translator.translate(*args) translator.translate(*args)
def test_connection_timeout():
# Requests library specifies two timeouts: connection and read
with raises(ConnectionError):
"""If a number is passed to timeout parameter, both connection
and read timeouts will be set to it.
Firstly, the connection timeout will fail.
"""
translator = Translator(timeout=0.00001)
translator.translate('안녕하세요.')
def test_read_timeout():
with raises(ReadTimeout):
translator = Translator(timeout=(10, 0.00001))
translator.translate('안녕하세요.')