Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement optional timeouts and add check for rate limits on server side. #123

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions pytumblr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TumblrRestClient(object):
A Python Client for the Tumblr API
"""

def __init__(self, consumer_key, consumer_secret="", oauth_token="", oauth_secret="", host="https://api.tumblr.com"):
def __init__(self, consumer_key, consumer_secret="", oauth_token="", oauth_secret="", host="https://api.tumblr.com", connect_timeout=None, read_timeout=None):
"""
Initializes the TumblrRestClient object, creating the TumblrRequest
object which deals with all request formatting.
Expand All @@ -28,7 +28,7 @@ def __init__(self, consumer_key, consumer_secret="", oauth_token="", oauth_secre

:returns: None
"""
self.request = TumblrRequest(consumer_key, consumer_secret, oauth_token, oauth_secret, host)
self.request = TumblrRequest(consumer_key, consumer_secret, oauth_token, oauth_secret, host, connect_timeout=connect_timeout, read_timeout=read_timeout)

def info(self):
"""
Expand Down
32 changes: 28 additions & 4 deletions pytumblr/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,27 @@
from requests.exceptions import TooManyRedirects, HTTPError


class RateLimitError(IOError):
"""Thrown when errors related to rate limiting occur."""

def __init__(self, *args, **kwargs):
"""Initialize RateLimitError with `request` and `response` objects."""
response = kwargs.pop('response', None)
self.response = response
self.request = kwargs.pop('request', None)
if (response is not None and not self.request and
hasattr(response, 'request')):
self.request = self.response.request
super(RateLimitError, self).__init__(*args, **kwargs)

class TumblrRequest(object):
"""
A simple request object that lets us query the Tumblr API
"""

__version = "0.0.8"

def __init__(self, consumer_key, consumer_secret="", oauth_token="", oauth_secret="", host="https://api.tumblr.com"):
def __init__(self, consumer_key, consumer_secret="", oauth_token="", oauth_secret="", host="https://api.tumblr.com", connect_timeout=None, read_timeout=None):
self.host = host
self.oauth = OAuth1(
consumer_key,
Expand All @@ -27,6 +40,7 @@ def __init__(self, consumer_key, consumer_secret="", oauth_token="", oauth_secre
resource_owner_secret=oauth_secret
)
self.consumer_key = consumer_key
self.timeout=(connect_timeout,read_timeout) if connect_timeout or read_timeout else None

self.headers = {
"User-Agent": "pytumblr/" + self.__version,
Expand All @@ -46,7 +60,9 @@ def get(self, url, params):
url = url + "?" + urllib.parse.urlencode(params)

try:
resp = requests.get(url, allow_redirects=False, headers=self.headers, auth=self.oauth)
resp = requests.get(url, allow_redirects=False, headers=self.headers, auth=self.oauth, timeout=self.timeout)
if resp.status_code == 429:
raise RateLimitError(resp.reason, response=resp)
except TooManyRedirects as e:
resp = e.response

Expand All @@ -71,7 +87,9 @@ def post(self, url, params={}, files=[]):
data = urllib.parse.urlencode(params)
if not PY3:
data = str(data)
resp = requests.post(url, data=data, headers=self.headers, auth=self.oauth)
resp = requests.post(url, data=data, headers=self.headers, auth=self.oauth, timeout=self.timeout)
if resp.status_code == 429:
raise RateLimitError(resp.reason, response=resp)
return self.json_parse(resp)
except HTTPError as e:
return self.json_parse(e.response)
Expand Down Expand Up @@ -114,6 +132,12 @@ def post_multipart(self, url, params, files):
files=files,
headers=self.headers,
allow_redirects=False,
auth=self.oauth
auth=self.oauth,
timeout=self.timeout
)
if resp.status_code == 429:
raise RateLimitError(resp.reason, response=resp)

return self.json_parse(resp)