X Tutup
Skip to content

Commit ca43510

Browse files
authored
Merge pull request python-telegram-bot#542 from python-telegram-bot/urllib3-vendor-beta
Vendor (embed) urllib3 with our package.
2 parents c7dbdce + 34059c9 commit ca43510

File tree

10 files changed

+120
-54
lines changed

10 files changed

+120
-54
lines changed

.coveragerc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
[run]
22
source = telegram
3+
omit = telegram/vendor/*
34

45
[report]
5-
omit = tests/
6+
omit =
7+
tests/
8+
telegram/vendor/*

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "telegram/vendor/urllib3"]
2+
path = telegram/vendor/urllib3
3+
url = https://github.com/python-telegram-bot/urllib3.git
4+
branch = ptb

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ install:
1616
- pip install -r requirements-dev.txt
1717
- if [[ $TRAVIS_PYTHON_VERSION != 'pypy'* ]]; then pip install ujson; fi
1818
script:
19-
- nosetests -v --with-flaky --no-flaky-report --with-coverage --cover-package=telegram/
19+
- nosetests -v --with-flaky --no-flaky-report --with-coverage --cover-package=telegram/ tests
2020
- if [[ $TRAVIS_PYTHON_VERSION == 3.5 ]]; then pre-commit run --all-files; fi
2121
after_success:
2222
coveralls

AUTHORS.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ Credits
33

44
``python-telegram-bot`` was originally created by
55
`Leandro Toledo <https://github.com/leandrotoledo>`_ and is now maintained by
6-
`Jannes Höke <https://github.com/jh0ker>`_ (`@jh0ker <https://t.me/jh0ker>`_ on Telegram).
6+
`Jannes Höke <https://github.com/jh0ker>`_ (`@jh0ker <https://t.me/jh0ker>`_ on Telegram) and
7+
`Noam Meltzer <https://github.com/tsnoam>`_.
8+
9+
We're vendoring urllib3 as part of ``python-telegram-bot`` which is distributed under the MIT
10+
license. For more info, full credits & license terms, the sources can be found here:
11+
`https://github.com/python-telegram-bot/urllib3`.
712

813
Contributors
914
------------

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
future>=0.15.2
2-
urllib3==1.20
32
certifi

setup.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ def requirements():
1717
return requirements_list
1818

1919

20+
packages = find_packages(exclude=['tests*'])
21+
packages.extend(['telegram.vendor.urllib3.urllib3',
22+
'telegram.vendor.urllib3.urllib3.packages', 'telegram.vendor.urllib3.urllib3.packages.ssl_match_hostname',
23+
'telegram.vendor.urllib3.urllib3.packages.backports', 'telegram.vendor.urllib3.urllib3.contrib',
24+
'telegram.vendor.urllib3.urllib3.util',
25+
])
26+
2027
with codecs.open('README.rst', 'r', 'utf-8') as fd:
2128
fn = os.path.join('telegram', 'version.py')
2229
with open(fn) as fh:
@@ -32,7 +39,7 @@ def requirements():
3239
keywords='python telegram bot api wrapper',
3340
description="We have made you a wrapper you can't refuse",
3441
long_description=fd.read(),
35-
packages=find_packages(exclude=['tests*']),
42+
packages=packages,
3643
install_requires=requirements(),
3744
extras_require={
3845
'json': 'ujson',

telegram/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
"""A library that provides a Python interface to the Telegram Bot API"""
2020

2121
from sys import version_info
22+
import sys
23+
import os
24+
25+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'vendor', 'urllib3'))
2226

2327
from .base import TelegramObject
2428
from .user import User

telegram/bot.py

Lines changed: 91 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -129,31 +129,33 @@ def decorator(self, *args, **kwargs):
129129

130130
return decorator
131131

132-
def message(func):
132+
def _message_wrapper(self, url, data, *args, **kwargs):
133+
if kwargs.get('reply_to_message_id'):
134+
data['reply_to_message_id'] = kwargs.get('reply_to_message_id')
133135

134-
@functools.wraps(func)
135-
def decorator(self, *args, **kwargs):
136-
url, data = func(self, *args, **kwargs)
136+
if kwargs.get('disable_notification'):
137+
data['disable_notification'] = kwargs.get('disable_notification')
137138

138-
if kwargs.get('reply_to_message_id'):
139-
data['reply_to_message_id'] = kwargs.get('reply_to_message_id')
139+
if kwargs.get('reply_markup'):
140+
reply_markup = kwargs.get('reply_markup')
141+
if isinstance(reply_markup, ReplyMarkup):
142+
data['reply_markup'] = reply_markup.to_json()
143+
else:
144+
data['reply_markup'] = reply_markup
140145

141-
if kwargs.get('disable_notification'):
142-
data['disable_notification'] = kwargs.get('disable_notification')
146+
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
143147

144-
if kwargs.get('reply_markup'):
145-
reply_markup = kwargs.get('reply_markup')
146-
if isinstance(reply_markup, ReplyMarkup):
147-
data['reply_markup'] = reply_markup.to_json()
148-
else:
149-
data['reply_markup'] = reply_markup
148+
if result is True:
149+
return result
150150

151-
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
151+
return Message.de_json(result, self)
152152

153-
if result is True:
154-
return result
153+
def message(func):
155154

156-
return Message.de_json(result, self)
155+
@functools.wraps(func)
156+
def decorator(self, *args, **kwargs):
157+
url, data = func(self, *args, **kwargs)
158+
return Bot._message_wrapper(self, url, data, *args, **kwargs)
157159

158160
return decorator
159161

@@ -283,15 +285,14 @@ def forwardMessage(self,
283285
return url, data
284286

285287
@log
286-
@message
287288
def sendPhoto(self,
288289
chat_id,
289290
photo,
290291
caption=None,
291292
disable_notification=False,
292293
reply_to_message_id=None,
293294
reply_markup=None,
294-
timeout=None,
295+
timeout=20.,
295296
**kwargs):
296297
"""Use this method to send photos.
297298
@@ -308,9 +309,7 @@ def sendPhoto(self,
308309
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
309310
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
310311
to remove reply keyboard or to force a reply from the user.
311-
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
312-
from the server (instead of the one specified during creation of the connection
313-
pool).
312+
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
314313
**kwargs (dict): Arbitrary keyword arguments.
315314
316315
Returns:
@@ -327,10 +326,19 @@ def sendPhoto(self,
327326
if caption:
328327
data['caption'] = caption
329328

330-
return url, data
329+
return self._message_wrapper(
330+
url,
331+
data,
332+
chat_id=chat_id,
333+
photo=photo,
334+
caption=caption,
335+
disable_notification=disable_notification,
336+
reply_to_message_id=reply_to_message_id,
337+
reply_markup=reply_markup,
338+
timeout=timeout,
339+
**kwargs)
331340

332341
@log
333-
@message
334342
def sendAudio(self,
335343
chat_id,
336344
audio,
@@ -341,7 +349,7 @@ def sendAudio(self,
341349
disable_notification=False,
342350
reply_to_message_id=None,
343351
reply_markup=None,
344-
timeout=None,
352+
timeout=20.,
345353
**kwargs):
346354
"""Use this method to send audio files, if you want Telegram clients to
347355
display them in the music player. Your audio must be in an .mp3 format.
@@ -370,9 +378,7 @@ def sendAudio(self,
370378
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
371379
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
372380
to remove reply keyboard or to force a reply from the user.
373-
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
374-
from the server (instead of the one specified during creation of the connection
375-
pool).
381+
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
376382
**kwargs (dict): Arbitrary keyword arguments.
377383
378384
Returns:
@@ -395,10 +401,22 @@ def sendAudio(self,
395401
if caption:
396402
data['caption'] = caption
397403

398-
return url, data
404+
return self._message_wrapper(
405+
url,
406+
data,
407+
chat_id=chat_id,
408+
audio=audio,
409+
duration=duration,
410+
performer=performer,
411+
title=title,
412+
caption=caption,
413+
disable_notification=disable_notification,
414+
reply_to_message_id=reply_to_message_id,
415+
reply_markup=reply_markup,
416+
timeout=20.,
417+
**kwargs)
399418

400419
@log
401-
@message
402420
def sendDocument(self,
403421
chat_id,
404422
document,
@@ -407,7 +425,7 @@ def sendDocument(self,
407425
disable_notification=False,
408426
reply_to_message_id=None,
409427
reply_markup=None,
410-
timeout=None,
428+
timeout=20.,
411429
**kwargs):
412430
"""Use this method to send general files.
413431
@@ -426,9 +444,7 @@ def sendDocument(self,
426444
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
427445
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
428446
to remove reply keyboard or to force a reply from the user.
429-
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
430-
from the server (instead of the one specified during creation of the connection
431-
pool).
447+
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
432448
**kwargs (dict): Arbitrary keyword arguments.
433449
434450
Returns:
@@ -447,7 +463,18 @@ def sendDocument(self,
447463
if caption:
448464
data['caption'] = caption
449465

450-
return url, data
466+
return self._message_wrapper(
467+
url,
468+
data,
469+
chat_id=chat_id,
470+
document=document,
471+
filename=filename,
472+
caption=caption,
473+
disable_notification=disable_notification,
474+
reply_to_message_id=reply_to_message_id,
475+
reply_markup=reply_markup,
476+
timeout=timeout,
477+
**kwargs)
451478

452479
@log
453480
@message
@@ -492,7 +519,6 @@ def sendSticker(self,
492519
return url, data
493520

494521
@log
495-
@message
496522
def sendVideo(self,
497523
chat_id,
498524
video,
@@ -501,7 +527,7 @@ def sendVideo(self,
501527
disable_notification=False,
502528
reply_to_message_id=None,
503529
reply_markup=None,
504-
timeout=None,
530+
timeout=20.,
505531
**kwargs):
506532
"""Use this method to send video files, Telegram clients support mp4
507533
videos (other formats may be sent as telegram.Document).
@@ -521,9 +547,7 @@ def sendVideo(self,
521547
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
522548
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
523549
to remove reply keyboard or to force a reply from the user.
524-
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
525-
from the server (instead of the one specified during creation of the connection
526-
pool).
550+
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
527551
528552
Returns:
529553
:class:`telegram.Message`: On success, instance representing the message posted.
@@ -541,10 +565,20 @@ def sendVideo(self,
541565
if caption:
542566
data['caption'] = caption
543567

544-
return url, data
568+
return self._message_wrapper(
569+
url,
570+
data,
571+
chat_id=chat_id,
572+
video=video,
573+
duration=duration,
574+
caption=caption,
575+
disable_notification=disable_notification,
576+
reply_to_message_id=reply_to_message_id,
577+
reply_markup=reply_markup,
578+
timeout=timeout,
579+
**kwargs)
545580

546581
@log
547-
@message
548582
def sendVoice(self,
549583
chat_id,
550584
voice,
@@ -553,7 +587,7 @@ def sendVoice(self,
553587
disable_notification=False,
554588
reply_to_message_id=None,
555589
reply_markup=None,
556-
timeout=None,
590+
timeout=20.,
557591
**kwargs):
558592
"""Use this method to send audio files, if you want Telegram clients to display the file as
559593
a playable voice message. For this to work, your audio must be in an .ogg file encoded with
@@ -575,9 +609,7 @@ def sendVoice(self,
575609
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
576610
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
577611
to remove reply keyboard or to force a reply from the user.
578-
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
579-
from the server (instead of the one specified during creation of the connection
580-
pool).
612+
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
581613
**kwargs (dict): Arbitrary keyword arguments.
582614
583615
Returns:
@@ -596,7 +628,18 @@ def sendVoice(self,
596628
if caption:
597629
data['caption'] = caption
598630

599-
return url, data
631+
return self._message_wrapper(
632+
url,
633+
data,
634+
chat_id=chat_id,
635+
voice=voice,
636+
duration=duration,
637+
caption=caption,
638+
disable_notification=disable_notification,
639+
reply_to_message_id=reply_to_message_id,
640+
reply_markup=reply_markup,
641+
timeout=timeout,
642+
**kwargs)
600643

601644
@log
602645
@message

telegram/utils/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(self,
7878
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
7979
],
8080
timeout=urllib3.Timeout(
81-
connect=self._connect_timeout, read=read_timeout),)
81+
connect=self._connect_timeout, read=read_timeout, total=None))
8282

8383
# Set a proxy according to the following order:
8484
# * proxy defined in proxy_url (+ urllib3_proxy_kwargs)

telegram/vendor/urllib3

Submodule urllib3 added at 4b076ee

0 commit comments

Comments
 (0)
X Tutup