Skip to content
This repository was archived by the owner on Jul 3, 2025. It is now read-only.

Commit 34e3c3f

Browse files
authored
Merge pull request facebookarchive#9 from wkoot/ccstolley-timeout-parameter
Added timeout parameter for API objects
2 parents a8704d3 + 248f55a commit 34e3c3f

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,24 @@ except InstagramAPIError as e:
225225
print "\nUser is set to private."
226226
```
227227

228+
Setting Timeouts
229+
------
230+
By default there is no timeout for requests to the Instagram API. You can specify a timeout in one of two ways:
231+
``` python
232+
from instagram.client import InstagramAPI
233+
234+
# set a 30-second timeout for this particular InstagramAPI instance
235+
api = InstagramAPI(access_token=access_token, client_secret=client_secret, timeout=30)
236+
```
237+
or
238+
``` python
239+
import socket
240+
241+
# Set the global default timeout, which applies to all sockets in your
242+
# program where a timeout is not otherwise specified.
243+
socket.setdefaulttimeout(30)
244+
```
245+
228246
Trouble Shooting
229247
------
230248

instagram/oauth2.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ class OAuth2API(object):
2727
# override with 'Instagram', etc
2828
api_name = "Generic API"
2929

30-
def __init__(self, client_id=None, client_secret=None, client_ips=None, access_token=None, redirect_uri=None):
30+
def __init__(self, client_id=None, client_secret=None, client_ips=None, access_token=None, redirect_uri=None, timeout=None):
3131
self.client_id = client_id
3232
self.client_secret = client_secret
3333
self.client_ips = client_ips
3434
self.access_token = access_token
3535
self.redirect_uri = redirect_uri
36+
self.timeout = timeout
3637

3738
def get_authorize_url(self, scope=None):
3839
req = OAuth2AuthExchangeRequest(self)
@@ -100,7 +101,7 @@ def get_authorize_url(self, scope=None):
100101
return self._url_for_authorize(scope=scope)
101102

102103
def get_authorize_login_url(self, scope=None):
103-
http_object = Http(disable_ssl_certificate_validation=True)
104+
http_object = Http(timeout=self.api.timeout, disable_ssl_certificate_validation=True)
104105

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

113114
def exchange_for_access_token(self, code=None, username=None, password=None, scope=None, user_id=None):
114115
data = self._data_for_exchange(code, username, password, scope=scope, user_id=user_id)
115-
http_object = Http(disable_ssl_certificate_validation=True)
116+
http_object = Http(timeout=self.api.timeout, disable_ssl_certificate_validation=True)
116117
url = self.api.access_token_url
117118

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

245246
# https://github.com/jcgregorio/httplib2/issues/173
246247
# bug in httplib2 w/ Python 3 and disable_ssl_certificate_validation=True
247-
http_obj = Http() if six.PY3 else Http(disable_ssl_certificate_validation=True)
248+
if six.PY3:
249+
http_obj = Http(timeout=self.api.timeout)
250+
else:
251+
http_obj = Http(timeout=self.api.timeout, disable_ssl_certificate_validation=True)
248252

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

0 commit comments

Comments
 (0)