Skip to content

Commit

Permalink
Dropping httplib in favor of requests (closes #113)
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Dec 22, 2012
1 parent 9f94502 commit 93eca00
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 82 deletions.
5 changes: 3 additions & 2 deletions allauth/socialaccount/providers/facebook/views.py
@@ -1,14 +1,15 @@
from django.utils.cache import patch_response_headers
from django.shortcuts import render

import requests

from allauth.socialaccount.models import SocialAccount, SocialLogin, SocialToken
from allauth.socialaccount.helpers import complete_social_login
from allauth.socialaccount.helpers import render_authentication_error
from allauth.socialaccount import providers
from allauth.socialaccount.providers.oauth2.views import (OAuth2Adapter,
OAuth2LoginView,
OAuth2CallbackView)
from allauth.socialaccount import requests

from forms import FacebookConnectForm
from provider import FacebookProvider
Expand All @@ -20,7 +21,7 @@
def fb_complete_login(app, token):
resp = requests.get('https://graph.facebook.com/me',
params={ 'access_token': token.token })
extra_data = resp.json
extra_data = resp.json()
email = valid_email_or_none(extra_data.get('email'))
uid = extra_data['id']
user = User(email=email)
Expand Down
5 changes: 3 additions & 2 deletions allauth/socialaccount/providers/github/views.py
@@ -1,7 +1,8 @@
import requests

from allauth.socialaccount.providers.oauth2.views import (OAuth2Adapter,
OAuth2LoginView,
OAuth2CallbackView)
from allauth.socialaccount import requests
from allauth.socialaccount.models import SocialAccount, SocialLogin
from allauth.utils import get_user_model

Expand All @@ -18,7 +19,7 @@ class GitHubOAuth2Adapter(OAuth2Adapter):
def complete_login(self, request, app, token):
resp = requests.get(self.profile_url,
params={ 'access_token': token.token })
extra_data = resp.json
extra_data = resp.json()
uid = str(extra_data['id'])
user = User(username=extra_data.get('login', ''),
email=extra_data.get('email', ''),
Expand Down
5 changes: 3 additions & 2 deletions allauth/socialaccount/providers/google/views.py
@@ -1,8 +1,9 @@
import requests

from allauth.socialaccount.providers.oauth2.views import (OAuth2Adapter,
OAuth2LoginView,
OAuth2CallbackView)

from allauth.socialaccount import requests
from allauth.socialaccount.models import SocialLogin, SocialAccount
from allauth.utils import get_user_model

Expand All @@ -20,7 +21,7 @@ def complete_login(self, request, app, token):
resp = requests.get(self.profile_url,
{ 'access_token': token.token,
'alt': 'json' })
extra_data = resp.json
extra_data = resp.json()
# extra_data is something of the form:
#
# {u'family_name': u'Penners', u'name': u'Raymond Penners',
Expand Down
4 changes: 2 additions & 2 deletions allauth/socialaccount/providers/oauth2/client.py
@@ -1,7 +1,7 @@
import urllib
import urlparse
import requests

from allauth.socialaccount import requests

class OAuth2Error(Exception):
pass
Expand Down Expand Up @@ -47,7 +47,7 @@ def get_access_token(self, code):
access_token = None
if resp.status_code == 200:
if resp.headers['content-type'].split(';')[0] == 'application/json':
data = resp.json
data = resp.json()
else:
data = dict(urlparse.parse_qsl(resp.content))
access_token = data.get('access_token')
Expand Down
7 changes: 4 additions & 3 deletions allauth/socialaccount/providers/persona/views.py
@@ -1,6 +1,7 @@
import requests

from allauth.socialaccount.helpers import complete_social_login
from allauth.socialaccount.helpers import render_authentication_error
from allauth.socialaccount import requests
from allauth.socialaccount.models import SocialAccount, SocialLogin
from allauth.utils import get_user_model

Expand All @@ -14,9 +15,9 @@ def persona_login(request):
resp = requests.post('https://verifier.login.persona.org/verify',
{ 'assertion': assertion,
'audience': audience })
if resp.json['status'] != 'okay':
if resp.json()['status'] != 'okay':
return render_authentication_error(request)
email = resp.json['email']
email = resp.json()['email']
user = User(email=email)
extra_data = resp.json
account = SocialAccount(uid=email,
Expand Down
5 changes: 3 additions & 2 deletions allauth/socialaccount/providers/soundcloud/views.py
@@ -1,7 +1,8 @@
import requests

from allauth.socialaccount.providers.oauth2.views import (OAuth2Adapter,
OAuth2LoginView,
OAuth2CallbackView)
from allauth.socialaccount import requests
from allauth.socialaccount.models import SocialAccount, SocialLogin
from allauth.utils import get_user_model

Expand All @@ -18,7 +19,7 @@ class SoundCloudOAuth2Adapter(OAuth2Adapter):
def complete_login(self, request, app, token):
resp = requests.get(self.profile_url,
params={ 'oauth_token': token.token })
extra_data = resp.json
extra_data = resp.json()
uid = str(extra_data['id'])
name_parts = extra_data.get('full_name', '').split(' ', 1)
if len(name_parts) == 2:
Expand Down
57 changes: 0 additions & 57 deletions allauth/socialaccount/requests.py

This file was deleted.

21 changes: 10 additions & 11 deletions allauth/socialaccount/tests.py
Expand Up @@ -7,15 +7,15 @@
from django.test.utils import override_settings

import providers
from allauth.socialaccount import requests
from allauth.tests import MockedResponse, mocked_response

from providers.oauth2.provider import OAuth2Provider

from models import SocialApp


mocked_oauth_responses = {
'google': requests.Response(200, """
'google': MockedResponse(200, """
{"family_name": "Penners", "name": "Raymond Penners",
"picture": "https://lh5.googleusercontent.com/-GOFYGBVOdBQ/AAAAAAAAAAI/AAAAAAAAAGM/WzRfPkv4xbo/photo.jpg",
"locale": "nl", "gender": "male",
Expand Down Expand Up @@ -48,15 +48,14 @@ def test_login(self):
warnings.warn("Cannot test provider %s, no oauth mock"
% self.provider.id)
return
requests.mock_next_request \
(requests.Response(200,
'{"access_token":"testac"}',
{'content-type':
'application/json'}))
requests.mock_next_request(resp_mock)
resp = self.client.get(complete_url,
{ 'code': 'test' })
self.assertRedirects(resp, reverse('socialaccount_signup'))
with mocked_response(MockedResponse(200,
'{"access_token":"testac"}',
{'content-type':
'application/json'}),
resp_mock):
resp = self.client.get(complete_url,
{ 'code': 'test' })
self.assertRedirects(resp, reverse('socialaccount_signup'))


impl = { 'setUp': setUp,
Expand Down
31 changes: 31 additions & 0 deletions allauth/tests.py
@@ -1,9 +1,40 @@
# -*- coding: utf-8 -*-

import requests
from django.test import TestCase

import utils

class MockedResponse(object):
def __init__(self, status_code, content, headers={}):
self.status_code = status_code
self.content = content
self.headers = headers

def json(self):
import json
return json.loads(self.content)

class mocked_response:
def __init__(self, *responses):
self.responses = list(responses)

def __enter__(self):
self.orig_get = requests.get
self.orig_post = requests.post

def mockable_request(f):
def new_f(*args, **kwargs):
if self.responses:
return self.responses.pop(0)
return f(*args, **kwargs)
return new_f
requests.get = mockable_request(requests.get)
requests.post = mockable_request(requests.post)

def __exit__(self, type, value, traceback):
requests.get = self.orig_get
requests.post = self.orig_post

class BasicTests(TestCase):

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,4 +1,4 @@
django>=1.4
oauth2
python-openid

requests>==1.0.3

0 comments on commit 93eca00

Please sign in to comment.