def post(url, data, network_delay=2.): """Request an URL. Args: url: The web location we want to retrieve. data: A dict of (str, unicode) key/value pairs. network_delay: Additional timeout in seconds to allow the response from Telegram to take some time. Returns: A JSON object. """ # Add time to the timeout of urlopen to allow data to be transferred over # the network. if 'timeout' in data: timeout = data['timeout'] + network_delay else: timeout = None if InputFile.is_inputfile(data): data = InputFile(data) request = Request(url, data=data.to_form(), headers=data.headers) else: data = json.dumps(data) request = Request(url, data=data.encode(), headers={'Content-Type': 'application/json'}) result = urlopen(request, timeout=timeout).read() return _parse(result)
def post(url, data): """Request an URL. Args: url: The web location we want to retrieve. data: A dict of (str, unicode) key/value pairs. Returns: A JSON object. """ try: if InputFile.is_inputfile(data): data = InputFile(data) request = Request(url, data=data.to_form(), headers=data.headers) else: data = json.dumps(data) request = Request(url, data=data.encode(), headers={'Content-Type': 'application/json'}) result = urlopen(request).read() except HTTPError as error: if error.getcode() == 403: raise TelegramError('Unauthorized') message = _parse(error.read()) raise TelegramError(message) return _parse(result)
def post(url, data, timeout=None): """Request an URL. Args: url: The web location we want to retrieve. data: A dict of (str, unicode) key/value pairs. timeout: float. If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. [Optional] Notes: If neither `timeout` nor `data['timeout']` is specified. The underlying defaults are used. Returns: A JSON object. """ urlopen_kwargs = {} if timeout is not None: urlopen_kwargs['timeout'] = timeout if InputFile.is_inputfile(data): data = InputFile(data) result = _request_wrapper('POST', url, body=data.to_form(), headers=data.headers) else: data = json.dumps(data) result = _request_wrapper('POST', url, body=data.encode(), headers={'Content-Type': 'application/json'}) return _parse(result)
def post(self, url, data, timeout=None): """Request an URL. Args: url (:obj:`str`): The web location we want to retrieve. data (dict[str, str|int]): A dict of key/value pairs. Note: On py2.7 value is unicode. timeout (:obj:`int` | :obj:`float`): If this value is specified, use it as the read timeout from the server (instead of the one specified during creation of the connection pool). Returns: A JSON object. """ urlopen_kwargs = {} if timeout is not None: urlopen_kwargs['timeout'] = Timeout(read=timeout, connect=self._connect_timeout) if InputFile.is_inputfile(data): data = InputFile(data) result = self._request_wrapper( 'POST', url, body=data.to_form(), headers=data.headers, **urlopen_kwargs) else: data = json.dumps(data) result = self._request_wrapper( 'POST', url, body=data.encode(), headers={'Content-Type': 'application/json'}, **urlopen_kwargs) return self._parse(result)
def post(url, data, timeout=None): """Request an URL. Args: url: The web location we want to retrieve. data: A dict of (str, unicode) key/value pairs. timeout: float. If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. [Optional] Notes: If neither `timeout` nor `data['timeout']` is specified. The underlying defaults are used. Returns: A JSON object. """ urlopen_kwargs = {} if timeout is not None: urlopen_kwargs['timeout'] = timeout if InputFile.is_inputfile(data): data = InputFile(data) request = Request(url, data=data.to_form(), headers=data.headers) else: data = json.dumps(data) request = Request(url, data=data.encode(), headers={'Content-Type': 'application/json'}) result = urlopen(request, **urlopen_kwargs).read() return _parse(result)
def post(url, data, network_delay=2.): """Request an URL. Args: url: The web location we want to retrieve. data: A dict of (str, unicode) key/value pairs. network_delay: Additional timeout in seconds to allow the response from Telegram to take some time. Returns: A JSON object. """ # Add time to the timeout of urlopen to allow data to be transferred over # the network. if 'timeout' in data: timeout = data['timeout'] + network_delay else: timeout = None try: if InputFile.is_inputfile(data): data = InputFile(data) request = Request(url, data=data.to_form(), headers=data.headers) else: data = json.dumps(data) request = Request(url, data=data.encode(), headers={'Content-Type': 'application/json'}) result = urlopen(request, timeout=timeout).read() except HTTPError as error: if error.getcode() == 403: raise TelegramError('Unauthorized') if error.getcode() == 502: raise TelegramError('Bad Gateway') try: message = _parse(error.read()) except ValueError: message = 'Unknown HTTPError' raise TelegramError(message) except (SSLError, socket.timeout) as error: if "operation timed out" in str(error): raise TelegramError("Timed out") raise TelegramError(str(error)) return _parse(result)
def post(url, data): """Request an URL. Args: url: The web location we want to retrieve. data: A dict of (str, unicode) key/value pairs. Returns: A JSON object. """ # Add one second to the timeout of urlopen to allow data to be transferred # over the network. if 'timeout' in data: timeout = data['timeout'] + 1. else: timeout = None try: if InputFile.is_inputfile(data): data = InputFile(data) request = Request(url, data=data.to_form(), headers=data.headers) else: data = json.dumps(data) request = Request(url, data=data.encode(), headers={'Content-Type': 'application/json'}) result = urlopen(request, timeout=timeout).read() except HTTPError as error: if error.getcode() == 403: raise TelegramError('Unauthorized') if error.getcode() == 502: raise TelegramError('Bad Gateway') message = _parse(error.read()) 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)
def post(url, data, timeout=None, network_delay=2.): """Request an URL. Args: url: The web location we want to retrieve. data: A dict of (str, unicode) key/value pairs. timeout: float. If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. [Optional] network_delay: float. If using the timeout specified in `data` (which is a timeout for the Telegram servers operation), then `network_delay` as an extra delay (in seconds) to compensate for network latency. default: 2 [Optional] Notes: If neither `timeout` nor `data['timeout']` is specified. The underlying defaults are used. Returns: A JSON object. """ urlopen_kwargs = {} if timeout is not None: urlopen_kwargs['timeout'] = timeout elif 'timeout' in data: urlopen_kwargs['timeout'] = data['timeout'] + network_delay if InputFile.is_inputfile(data): data = InputFile(data) request = Request(url, data=data.to_form(), headers=data.headers) else: data = json.dumps(data) request = Request(url, data=data.encode(), headers={'Content-Type': 'application/json'}) result = urlopen(request, **urlopen_kwargs).read() return _parse(result)