diff --git a/googletrans/adapters.py b/googletrans/adapters.py new file mode 100644 index 0000000..6eb21ce --- /dev/null +++ b/googletrans/adapters.py @@ -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) diff --git a/googletrans/client.py b/googletrans/client.py index 016c5e2..73914d4 100644 --- a/googletrans/client.py +++ b/googletrans/client.py @@ -8,6 +8,7 @@ import requests import random from googletrans import urls, utils +from googletrans.adapters import TimeoutAdapter from googletrans.compat import PY3 from googletrans.gtoken import TokenAcquirer from googletrans.constants import DEFAULT_USER_AGENT, LANGCODES, LANGUAGES, SPECIAL_CASES @@ -32,15 +33,26 @@ class Translator(object): :param proxies: proxies configuration. 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'}`` + :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() if proxies is not None: self.session.proxies = proxies self.session.headers.update({ '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.token_acquirer = TokenAcquirer(session=self.session, host=self.service_urls[0]) diff --git a/tests/test_client.py b/tests/test_client.py index e51bee9..07ab85f 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- from pytest import raises +from requests.exceptions import ConnectionError +from requests.exceptions import ReadTimeout from googletrans import Translator @@ -112,3 +114,22 @@ def test_dest_not_in_supported_languages(translator): with raises(ValueError): 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('안녕하세요.')