Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.
Merged
Changes from all 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
25 changes: 16 additions & 9 deletions twitter/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ def __init__(self,
debugHTTP=False,
timeout=None,
sleep_on_rate_limit=False,
tweet_mode='compat'):
tweet_mode='compat',
proxies=None):
"""Instantiate a new twitter.Api object.

Args:
Expand Down Expand Up @@ -203,6 +204,9 @@ def __init__(self,
tweet_mode (str, optional):
Whether to use the new (as of Sept. 2016) extended tweet mode. See docs for
details. Choices are ['compatibility', 'extended'].
proxies (dict, optional):
A dictionary of proxies for the request to pass through, if not specified
allows requests lib to use environmental variables for proxy if any.
"""

# check to see if the library is running on a Google App Engine instance
Expand All @@ -229,6 +233,7 @@ def __init__(self,
self.rate_limit = RateLimit()
self.sleep_on_rate_limit = sleep_on_rate_limit
self.tweet_mode = tweet_mode
self.proxies = proxies

if base_url is None:
self.base_url = 'https://api.twitter.com/1.1'
Expand Down Expand Up @@ -4872,7 +4877,8 @@ def _RequestChunkedUpload(self, url, headers, data):
headers=headers,
data=data,
auth=self.__auth,
timeout=self._timeout
timeout=self._timeout,
proxies=self.proxies
)
except requests.RequestException as e:
raise TwitterError(str(e))
Expand Down Expand Up @@ -4909,20 +4915,20 @@ def _RequestUrl(self, url, verb, data=None, json=None):
if data:
if 'media_ids' in data:
url = self._BuildUrl(url, extra_params={'media_ids': data['media_ids']})
resp = requests.post(url, data=data, auth=self.__auth, timeout=self._timeout)
resp = requests.post(url, data=data, auth=self.__auth, timeout=self._timeout, proxies=self.proxies)
elif 'media' in data:
resp = requests.post(url, files=data, auth=self.__auth, timeout=self._timeout)
resp = requests.post(url, files=data, auth=self.__auth, timeout=self._timeout, proxies=self.proxies)
else:
resp = requests.post(url, data=data, auth=self.__auth, timeout=self._timeout)
resp = requests.post(url, data=data, auth=self.__auth, timeout=self._timeout, proxies=self.proxies)
elif json:
resp = requests.post(url, json=json, auth=self.__auth, timeout=self._timeout)
resp = requests.post(url, json=json, auth=self.__auth, timeout=self._timeout, proxies=self.proxies)
else:
resp = 0 # POST request, but without data or json

elif verb == 'GET':
data['tweet_mode'] = self.tweet_mode
url = self._BuildUrl(url, extra_params=data)
resp = requests.get(url, auth=self.__auth, timeout=self._timeout)
resp = requests.get(url, auth=self.__auth, timeout=self._timeout, proxies=self.proxies)

else:
resp = 0 # if not a POST or GET request
Expand Down Expand Up @@ -4954,14 +4960,15 @@ def _RequestStream(self, url, verb, data=None):
try:
return requests.post(url, data=data, stream=True,
auth=self.__auth,
timeout=self._timeout)
timeout=self._timeout,
proxies=self.proxies)
except requests.RequestException as e:
raise TwitterError(str(e))
if verb == 'GET':
url = self._BuildUrl(url, extra_params=data)
try:
return requests.get(url, stream=True, auth=self.__auth,
timeout=self._timeout)
timeout=self._timeout, proxies=self.proxies)
except requests.RequestException as e:
raise TwitterError(str(e))
return 0 # if not a POST or GET request