Merge pull request #90 from rahiel/master

raise exception if telegram times out on long-polling
This commit is contained in:
Jannes Höke 2015-11-10 04:56:18 +01:00
commit 3928e27cf4

View file

@ -20,6 +20,7 @@
"""This module contains methods to make POST and GET requests""" """This module contains methods to make POST and GET requests"""
import json import json
from ssl import SSLError
try: try:
from urllib.parse import urlencode from urllib.parse import urlencode
@ -78,6 +79,11 @@ def post(url,
Returns: Returns:
A JSON object. A JSON object.
""" """
if "timeout" in data:
timeout = data["timeout"] + 1.
else:
timeout = None
try: try:
if InputFile.is_inputfile(data): if InputFile.is_inputfile(data):
data = InputFile(data) data = InputFile(data)
@ -90,7 +96,7 @@ def post(url,
data=data.encode(), data=data.encode(),
headers={'Content-Type': 'application/json'}) headers={'Content-Type': 'application/json'})
result = urlopen(request).read() result = urlopen(request, timeout=timeout).read()
except HTTPError as error: except HTTPError as error:
if error.getcode() == 403: if error.getcode() == 403:
raise TelegramError('Unauthorized') raise TelegramError('Unauthorized')
@ -99,7 +105,11 @@ def post(url,
message = _parse(error.read()) message = _parse(error.read())
raise TelegramError(message) raise TelegramError(message)
except SSLError as error:
if "The read operation timed out" == error.message:
raise TelegramError("Timed out")
raise TelegramError(error.message)
return _parse(result) return _parse(result)