Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Refactor kwargs handling in Bot methods#1924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Bibo-Joshi merged 9 commits intov13fromfix-1918
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
779 changes: 347 additions & 432 deletionstelegram/bot.py
View file
Open in desktop

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletionstelegram/files/animation.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,14 +94,15 @@ def de_json(cls, data, bot):

return cls(bot=bot, **data)

def get_file(self, timeout=None,**kwargs):
def get_file(self, timeout=None,api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`

Args:
timeout (:obj:`int` | :obj:`float`, optional): 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).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.

Returns:
:class:`telegram.File`
Expand All@@ -110,4 +111,4 @@ def get_file(self, timeout=None, **kwargs):
:class:`telegram.TelegramError`

"""
return self.bot.get_file(self.file_id, timeout=timeout,**kwargs)
return self.bot.get_file(self.file_id, timeout=timeout,api_kwargs=api_kwargs)
7 changes: 4 additions & 3 deletionstelegram/files/audio.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -91,14 +91,15 @@ def de_json(cls, data, bot):

return cls(bot=bot, **data)

def get_file(self, timeout=None,**kwargs):
def get_file(self, timeout=None,api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`

Args:
timeout (:obj:`int` | :obj:`float`, optional): 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).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.

Returns:
:class:`telegram.File`
Expand All@@ -107,4 +108,4 @@ def get_file(self, timeout=None, **kwargs):
:class:`telegram.TelegramError`

"""
return self.bot.get_file(self.file_id, timeout=timeout,**kwargs)
return self.bot.get_file(self.file_id, timeout=timeout,api_kwargs=api_kwargs)
6 changes: 4 additions & 2 deletionstelegram/files/chatphoto.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -83,7 +83,8 @@ def get_small_file(self, timeout=None, **kwargs):
timeout (:obj:`int` | :obj:`float`, optional): 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).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.

Returns:
:class:`telegram.File`
Expand All@@ -102,7 +103,8 @@ def get_big_file(self, timeout=None, **kwargs):
timeout (:obj:`int` | :obj:`float`, optional): 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).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.

Returns:
:class:`telegram.File`
Expand Down
7 changes: 4 additions & 3 deletionstelegram/files/document.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -82,14 +82,15 @@ def de_json(cls, data, bot):

return cls(bot=bot, **data)

def get_file(self, timeout=None,**kwargs):
def get_file(self, timeout=None,api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`

Args:
timeout (:obj:`int` | :obj:`float`, optional): 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).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.

Returns:
:class:`telegram.File`
Expand All@@ -98,4 +99,4 @@ def get_file(self, timeout=None, **kwargs):
:class:`telegram.TelegramError`

"""
return self.bot.get_file(self.file_id, timeout=timeout,**kwargs)
return self.bot.get_file(self.file_id, timeout=timeout,api_kwargs=api_kwargs)
7 changes: 4 additions & 3 deletionstelegram/files/photosize.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,14 +84,15 @@ def de_list(cls, data, bot):

return photos

def get_file(self, timeout=None,**kwargs):
def get_file(self, timeout=None,api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`

Args:
timeout (:obj:`int` | :obj:`float`, optional): 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).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.

Returns:
:class:`telegram.File`
Expand All@@ -100,4 +101,4 @@ def get_file(self, timeout=None, **kwargs):
:class:`telegram.TelegramError`

"""
return self.bot.get_file(self.file_id, timeout=timeout,**kwargs)
return self.bot.get_file(self.file_id, timeout=timeout,api_kwargs=api_kwargs)
7 changes: 4 additions & 3 deletionstelegram/files/sticker.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -110,14 +110,15 @@ def de_list(cls, data, bot):

return [cls.de_json(d, bot) for d in data]

def get_file(self, timeout=None,**kwargs):
def get_file(self, timeout=None,api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`

Args:
timeout (:obj:`int` | :obj:`float`, optional): 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).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.

Returns:
:class:`telegram.File`
Expand All@@ -126,7 +127,7 @@ def get_file(self, timeout=None, **kwargs):
:class:`telegram.TelegramError`

"""
return self.bot.get_file(self.file_id, timeout=timeout,**kwargs)
return self.bot.get_file(self.file_id, timeout=timeout,api_kwargs=api_kwargs)


class StickerSet(TelegramObject):
Expand Down
7 changes: 4 additions & 3 deletionstelegram/files/video.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -89,14 +89,15 @@ def de_json(cls, data, bot):

return cls(bot=bot, **data)

def get_file(self, timeout=None,**kwargs):
def get_file(self, timeout=None,api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`

Args:
timeout (:obj:`int` | :obj:`float`, optional): 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).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.

Returns:
:class:`telegram.File`
Expand All@@ -105,4 +106,4 @@ def get_file(self, timeout=None, **kwargs):
:class:`telegram.TelegramError`

"""
return self.bot.get_file(self.file_id, timeout=timeout,**kwargs)
return self.bot.get_file(self.file_id, timeout=timeout,api_kwargs=api_kwargs)
7 changes: 4 additions & 3 deletionstelegram/files/videonote.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -81,14 +81,15 @@ def de_json(cls, data, bot):

return cls(bot=bot, **data)

def get_file(self, timeout=None,**kwargs):
def get_file(self, timeout=None,api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`

Args:
timeout (:obj:`int` | :obj:`float`, optional): 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).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.

Returns:
:class:`telegram.File`
Expand All@@ -97,4 +98,4 @@ def get_file(self, timeout=None, **kwargs):
:class:`telegram.TelegramError`

"""
return self.bot.get_file(self.file_id, timeout=timeout,**kwargs)
return self.bot.get_file(self.file_id, timeout=timeout,api_kwargs=api_kwargs)
7 changes: 4 additions & 3 deletionstelegram/files/voice.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -75,14 +75,15 @@ def de_json(cls, data, bot):

return cls(bot=bot, **data)

def get_file(self, timeout=None,**kwargs):
def get_file(self, timeout=None,api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`

Args:
timeout (:obj:`int` | :obj:`float`, optional): 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).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.

Returns:
:class:`telegram.File`
Expand All@@ -91,4 +92,4 @@ def get_file(self, timeout=None, **kwargs):
:class:`telegram.TelegramError`

"""
return self.bot.get_file(self.file_id, timeout=timeout,**kwargs)
return self.bot.get_file(self.file_id, timeout=timeout,api_kwargs=api_kwargs)
7 changes: 4 additions & 3 deletionstelegram/passport/passportfile.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -101,7 +101,7 @@ def de_list_decrypted(cls, data, bot, credentials):
return [cls.de_json_decrypted(passport_file, bot, credentials[i])
for i, passport_file in enumerate(data)]

def get_file(self, timeout=None,**kwargs):
def get_file(self, timeout=None,api_kwargs=None):
"""
Wrapper over :attr:`telegram.Bot.get_file`. Will automatically assign the correct
credentials to the returned :class:`telegram.File` if originating from
Expand All@@ -111,7 +111,8 @@ def get_file(self, timeout=None, **kwargs):
timeout (:obj:`int` | :obj:`float`, optional): 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).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.

Returns:
:class:`telegram.File`
Expand All@@ -120,6 +121,6 @@ def get_file(self, timeout=None, **kwargs):
:class:`telegram.TelegramError`

"""
file = self.bot.get_file(self.file_id, timeout=timeout,**kwargs)
file = self.bot.get_file(self.file_id, timeout=timeout,api_kwargs=api_kwargs)
file.set_credentials(self._credentials)
return file
32 changes: 7 additions & 25 deletionstelegram/utils/request.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -255,14 +255,15 @@ def _request_wrapper(self, *args, **kwargs):
else:
raise NetworkError('{} ({})'.format(message, resp.status))

defget(self, url, timeout=None):
defpost(self, url, data=None, timeout=None):
"""Request an URL.

Args:
url (:obj:`str`): The web location we want to retrieve.
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).
data (dict[str, str|int], optional): A dict of key/value pairs.
timeout (:obj:`int` | :obj:`float`, optional): 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.
Expand All@@ -273,27 +274,8 @@ def get(self, url, timeout=None):
if timeout is not None:
urlopen_kwargs['timeout'] = Timeout(read=timeout, connect=self._connect_timeout)

result = self._request_wrapper('GET', url, **urlopen_kwargs)
return self._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.
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 data is None:
data = {}

# Are we uploading files?
files = False
Expand Down
6 changes: 3 additions & 3 deletionstests/test_animation.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -72,7 +72,7 @@ def test_send_all_args(self, bot, chat_id, animation_file, animation, thumb_file
message = bot.send_animation(chat_id, animation_file, duration=self.duration,
width=self.width, height=self.height, caption=self.caption,
parse_mode='Markdown', disable_notification=False,
filename=self.file_name,thumb=thumb_file)
thumb=thumb_file)

assert isinstance(message.animation, Animation)
assert isinstance(message.animation.file_id, str)
Expand DownExpand Up@@ -158,10 +158,10 @@ def test_resend(self, bot, chat_id, animation):
assert message.animation == animation

def test_send_with_animation(self, monkeypatch, bot, chat_id, animation):
def test(_,url, data, **kwargs):
def test(url, data, **kwargs):
return data['animation'] == animation.file_id

monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.send_animation(animation=animation, chat_id=chat_id)
assert message

Expand Down
4 changes: 2 additions & 2 deletionstests/test_audio.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -135,10 +135,10 @@ def test_resend(self, bot, chat_id, audio):
assert message.audio == audio

def test_send_with_audio(self, monkeypatch, bot, chat_id, audio):
def test(_,url, data, **kwargs):
def test(url, data, **kwargs):
return data['audio'] == audio.file_id

monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.send_audio(audio=audio, chat_id=chat_id)
assert message

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp