1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Fix backward comptability

This commit is contained in:
_run 2022-08-12 15:25:51 +05:00
parent e860f114c6
commit 26db76f207
2 changed files with 22 additions and 7 deletions

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import copy
import time
from datetime import datetime
@ -89,8 +90,9 @@ def _make_request(token, method_name, method='get', params=None, files=None):
connect_timeout = CONNECT_TIMEOUT
if files:
files_copy = copy.deepcopy(files)
# process types.InputFile
for key, value in files.items():
for key, value in files_copy.items():
if isinstance(value, types.InputFile):
files[key] = value.file

View File

@ -83,11 +83,17 @@ async def _process_request(token, url, method='get', params=None, files=None, re
if not got_result:
raise RequestTimeout("Request timeout. Request: method={0} url={1} params={2} files={3} request_timeout={4}".format(method, url, params, files, request_timeout, current_try))
def _prepare_file(obj):
"""
Prepares file for upload.
"""
name = getattr(obj, 'name', None)
if name and isinstance(name, str) and name[0] != '<' and name[-1] != '>':
return os.path.basename(name)
def _prepare_data(params=None, files=None):
"""
prepare data for request.
Adds the parameters and files to the request.
:param params:
:param files:
@ -98,13 +104,20 @@ def _prepare_data(params=None, files=None):
if params:
for key, value in params.items():
data.add_field(key, str(value))
if files:
for key, f in files.items():
if isinstance(f, types.InputFile):
f = f.file
if isinstance(f, tuple):
if len(f) == 2:
file_name, file = f
else:
raise ValueError('Tuple must have exactly 2 elements: filename, fileobj')
elif isinstance(f, types.InputFile):
file_name = f.file_name
file = f.file
else:
file_name, file = _prepare_file(f) or key, f
data.add_field(key, f, filename=key)
data.add_field(key, file, filename=file_name)
return data