Skip to content

Commit

Permalink
pep8 formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
wkoot committed Feb 27, 2017
1 parent cb65d88 commit bbca28d
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions instagram/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ def exchange_user_id_for_access_token(self, user_id):
def exchange_xauth_login_for_access_token(self, username, password, scope=None):
""" scope should be a tuple or list of requested scope access levels """
req = OAuth2AuthExchangeRequest(self)
return req.exchange_for_access_token(username=username, password=password,
scope=scope)
return req.exchange_for_access_token(username=username, password=password, scope=scope)


class OAuth2AuthExchangeRequest(object):
Expand All @@ -68,8 +67,10 @@ def _url_for_authorize(self, scope=None):
"response_type": "code",
"redirect_uri": self.api.redirect_uri
}

if scope:
client_params.update(scope=' '.join(scope))

url_params = urlencode(client_params)
return "%s?%s" % (self.api.authorize_url, url_params)

Expand All @@ -80,6 +81,7 @@ def _data_for_exchange(self, code=None, username=None, password=None, scope=None
"redirect_uri": self.api.redirect_uri,
"grant_type": "authorization_code"
}

if code:
client_params.update(code=code)
elif username and password:
Expand All @@ -88,8 +90,10 @@ def _data_for_exchange(self, code=None, username=None, password=None, scope=None
grant_type="password")
if scope:
client_params.update(scope=' '.join(scope))

elif user_id:
client_params.update(user_id=user_id)

return urlencode(client_params)

def get_authorize_url(self, scope=None):
Expand All @@ -102,18 +106,22 @@ def get_authorize_login_url(self, scope=None):
response, content = http_object.request(url)
if response['status'] != '200':
raise OAuth2AuthExchangeError("The server returned a non-200 response for URL %s" % url)
redirected_to = response['content-location']

redirected_to = response['Content-Location']
return redirected_to

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)
url = self.api.access_token_url
headers={"Content-Type":"application/x-www-form-urlencoded"}

headers = {"Content-Type": "application/x-www-form-urlencoded"}
response, content = http_object.request(url, method="POST", body=data, headers=headers)
parsed_content = simplejson.loads(content.decode())

if int(response['status']) != 200:
raise OAuth2AuthExchangeError(parsed_content.get("error_message", ""))

return parsed_content['access_token'], parsed_content['user']


Expand All @@ -137,15 +145,12 @@ def post_request(self, path, **kwargs):
return self.make_request(self.prepare_request("POST", path, kwargs))

def _full_url(self, path, include_secret=False, include_signed_request=True):
return "%s://%s%s%s%s%s" % (self.api.protocol,
self.api.host,
self.api.base_path,
path,
self._auth_query(include_secret),
self._signed_request(path, {}, include_signed_request, include_secret))
return "%s://%s%s%s%s%s" % (self.api.protocol, self.api.host, self.api.base_path, path,
self._auth_query(include_secret),
self._signed_request(path, {}, include_signed_request, include_secret))

def _full_url_with_params(self, path, params, include_secret=False, include_signed_request=True):
return (self._full_url(path, include_secret) +
return (self._full_url(path, include_secret) +
self._full_query_with_params(params) +
self._signed_request(path, params, include_signed_request, include_secret))

Expand All @@ -168,8 +173,10 @@ def _signed_request(self, path, params, include_signed_request, include_secret):
params['access_token'] = self.api.access_token
elif self.api.client_id:
params['client_id'] = self.api.client_id

if include_secret and self.api.client_secret:
params['client_secret'] = self.api.client_secret

return "&sig=%s" % self._generate_sig(path, params, self.api.client_secret)
else:
return ''
Expand Down Expand Up @@ -200,6 +207,7 @@ def encode_file(field_name):
lines.extend(encode_field(field))
for field in files:
lines.extend(encode_file(field))

lines.extend(("--%s--" % (boundary), ""))
body = "\r\n".join(lines)

Expand All @@ -219,7 +227,7 @@ def prepare_request(self, method, path, params, include_secret=False):
if not params.get('files'):
if method == "POST":
body = self._post_body(params)
headers = {'Content-type': 'application/x-www-form-urlencoded'}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
url = self._full_url(path, include_secret)
else:
url = self._full_url_with_params(path, params, include_secret)
Expand All @@ -231,9 +239,11 @@ def prepare_request(self, method, path, params, include_secret=False):

def make_request(self, url, method="GET", body=None, headers=None):
headers = headers or {}
if not 'User-Agent' in headers:
if 'User-Agent' not in headers:
headers.update({"User-Agent": "%s Python Client" % self.api.api_name})

# 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)
http_obj = Http() if six.PY3 else Http(disable_ssl_certificate_validation=True)

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

0 comments on commit bbca28d

Please sign in to comment.