Skip to content

Commit

Permalink
Merge pull request #9 from wkoot/ccstolley-timeout-parameter
Browse files Browse the repository at this point in the history
Added timeout parameter for API objects
  • Loading branch information
wkoot authored Feb 27, 2017
2 parents a8704d3 + 248f55a commit 34e3c3f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,24 @@ except InstagramAPIError as e:
print "\nUser is set to private."
```

Setting Timeouts
------
By default there is no timeout for requests to the Instagram API. You can specify a timeout in one of two ways:
``` python
from instagram.client import InstagramAPI

# set a 30-second timeout for this particular InstagramAPI instance
api = InstagramAPI(access_token=access_token, client_secret=client_secret, timeout=30)
```
or
``` python
import socket

# Set the global default timeout, which applies to all sockets in your
# program where a timeout is not otherwise specified.
socket.setdefaulttimeout(30)
```

Trouble Shooting
------

Expand Down
12 changes: 8 additions & 4 deletions instagram/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ class OAuth2API(object):
# override with 'Instagram', etc
api_name = "Generic API"

def __init__(self, client_id=None, client_secret=None, client_ips=None, access_token=None, redirect_uri=None):
def __init__(self, client_id=None, client_secret=None, client_ips=None, access_token=None, redirect_uri=None, timeout=None):
self.client_id = client_id
self.client_secret = client_secret
self.client_ips = client_ips
self.access_token = access_token
self.redirect_uri = redirect_uri
self.timeout = timeout

def get_authorize_url(self, scope=None):
req = OAuth2AuthExchangeRequest(self)
Expand Down Expand Up @@ -100,7 +101,7 @@ def get_authorize_url(self, scope=None):
return self._url_for_authorize(scope=scope)

def get_authorize_login_url(self, scope=None):
http_object = Http(disable_ssl_certificate_validation=True)
http_object = Http(timeout=self.api.timeout, disable_ssl_certificate_validation=True)

url = self._url_for_authorize(scope=scope)
response, content = http_object.request(url)
Expand All @@ -112,7 +113,7 @@ def get_authorize_login_url(self, scope=None):

def exchange_for_access_token(self, code=None, username=None, password=None, scope=None, user_id=None):
data = self._data_for_exchange(code, username, password, scope=scope, user_id=user_id)
http_object = Http(disable_ssl_certificate_validation=True)
http_object = Http(timeout=self.api.timeout, disable_ssl_certificate_validation=True)
url = self.api.access_token_url

headers = {"Content-Type": "application/x-www-form-urlencoded"}
Expand Down Expand Up @@ -244,6 +245,9 @@ def make_request(self, url, method="GET", body=None, headers=None):

# https://github.com/jcgregorio/httplib2/issues/173
# bug in httplib2 w/ Python 3 and disable_ssl_certificate_validation=True
http_obj = Http() if six.PY3 else Http(disable_ssl_certificate_validation=True)
if six.PY3:
http_obj = Http(timeout=self.api.timeout)
else:
http_obj = Http(timeout=self.api.timeout, disable_ssl_certificate_validation=True)

return http_obj.request(url, method, body=body, headers=headers)

0 comments on commit 34e3c3f

Please sign in to comment.