Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #577 from dhermes/towards-554-part3
Browse files Browse the repository at this point in the history
Remove httplib2 imports from non-transport modules.
  • Loading branch information
dhermes committed Aug 1, 2016
2 parents eb019c2 + 80fff4b commit cd825cc
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 17 deletions.
6 changes: 3 additions & 3 deletions oauth2client/contrib/_metadata.py
Expand Up @@ -20,7 +20,6 @@
import datetime
import json

import httplib2
from six.moves import http_client
from six.moves.urllib import parse as urlparse

Expand Down Expand Up @@ -51,7 +50,8 @@ def get(http_request, path, root=METADATA_ROOT, recursive=None):
A dictionary if the metadata server returns JSON, otherwise a string.
Raises:
httplib2.Httplib2Error if an error corrured while retrieving metadata.
http_client.HTTPException if an error corrured while
retrieving metadata.
"""
url = urlparse.urljoin(root, path)
url = util._add_query_parameter(url, 'recursive', recursive)
Expand All @@ -68,7 +68,7 @@ def get(http_request, path, root=METADATA_ROOT, recursive=None):
else:
return decoded
else:
raise httplib2.HttpLib2Error(
raise http_client.HTTPException(
'Failed to retrieve {0} from the Google Compute Engine'
'metadata service. Response:\n{1}'.format(url, response))

Expand Down
5 changes: 3 additions & 2 deletions oauth2client/contrib/appengine.py
Expand Up @@ -29,12 +29,12 @@
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext.webapp.util import login_required
import httplib2
import webapp2 as webapp

import oauth2client
from oauth2client import client
from oauth2client import clientsecrets
from oauth2client import transport
from oauth2client import util
from oauth2client.contrib import xsrfutil

Expand Down Expand Up @@ -742,7 +742,8 @@ def http(self, *args, **kwargs):
*args: Positional arguments passed to httplib2.Http constructor.
**kwargs: Positional arguments passed to httplib2.Http constructor.
"""
return self.credentials.authorize(httplib2.Http(*args, **kwargs))
return self.credentials.authorize(
transport.get_http_object(*args, **kwargs))

@property
def callback_path(self):
Expand Down
7 changes: 3 additions & 4 deletions oauth2client/contrib/django_util/__init__.py
Expand Up @@ -228,10 +228,10 @@ class MyModel(models.Model):
import django.conf
from django.core import exceptions
from django.core import urlresolvers
import httplib2
from six.moves.urllib import parse

from oauth2client import clientsecrets
from oauth2client import transport
from oauth2client.contrib import dictionary_storage
from oauth2client.contrib.django_util import storage

Expand Down Expand Up @@ -470,8 +470,7 @@ def credentials(self):

@property
def http(self):
"""Helper method to create an HTTP client authorized with OAuth2
credentials."""
"""Helper: create HTTP client authorized with OAuth2 credentials."""
if self.has_credentials():
return self.credentials.authorize(httplib2.Http())
return self.credentials.authorize(transport.get_http_object())
return None
5 changes: 3 additions & 2 deletions oauth2client/contrib/flask_util.py
Expand Up @@ -179,11 +179,11 @@ def requires_calendar():
except ImportError: # pragma: NO COVER
raise ImportError('The flask utilities require flask 0.9 or newer.')

import httplib2
import six.moves.http_client as httplib

from oauth2client import client
from oauth2client import clientsecrets
from oauth2client import transport
from oauth2client.contrib import dictionary_storage


Expand Down Expand Up @@ -553,4 +553,5 @@ def http(self, *args, **kwargs):
"""
if not self.credentials:
raise ValueError('No credentials available.')
return self.credentials.authorize(httplib2.Http(*args, **kwargs))
return self.credentials.authorize(
transport.get_http_object(*args, **kwargs))
6 changes: 3 additions & 3 deletions oauth2client/contrib/gce.py
Expand Up @@ -20,7 +20,7 @@
import logging
import warnings

import httplib2
from six.moves import http_client

from oauth2client import client
from oauth2client.contrib import _metadata
Expand Down Expand Up @@ -134,8 +134,8 @@ def _refresh(self, http_request):
self._retrieve_info(http_request)
self.access_token, self.token_expiry = _metadata.get_token(
http_request, service_account=self.service_account_email)
except httplib2.HttpLib2Error as e:
raise client.HttpAccessTokenRefreshError(str(e))
except http_client.HTTPException as err:
raise client.HttpAccessTokenRefreshError(str(err))

@property
def serialization_data(self):
Expand Down
10 changes: 8 additions & 2 deletions oauth2client/transport.py
Expand Up @@ -58,13 +58,19 @@ def get_cached_http():
return _CACHED_HTTP


def get_http_object():
def get_http_object(*args, **kwargs):
"""Return a new HTTP object.
Args:
*args: tuple, The positional arguments to be passed when
contructing a new HTTP object.
**kwargs: dict, The keyword arguments to be passed when
contructing a new HTTP object.
Returns:
httplib2.Http, an HTTP object.
"""
return httplib2.Http()
return httplib2.Http(*args, **kwargs)


def _initialize_headers(headers):
Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/test_metadata.py
Expand Up @@ -62,7 +62,7 @@ def test_get_success_string(self):
def test_get_failure(self):
http_request = request_mock(
http_client.NOT_FOUND, 'text/html', '<p>Error</p>')
with self.assertRaises(httplib2.HttpLib2Error):
with self.assertRaises(http_client.HTTPException):
_metadata.get(http_request, PATH)

http_request.assert_called_once_with(EXPECTED_URL, **EXPECTED_KWARGS)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_transport.py
Expand Up @@ -52,6 +52,13 @@ class Test_get_http_object(unittest2.TestCase):
def test_it(self, http_klass):
result = transport.get_http_object()
self.assertEqual(result, http_klass.return_value)
http_klass.assert_called_once_with()

@mock.patch.object(httplib2, 'Http', return_value=object())
def test_with_args(self, http_klass):
result = transport.get_http_object(1, 2, foo='bar')
self.assertEqual(result, http_klass.return_value)
http_klass.assert_called_once_with(1, 2, foo='bar')


class Test__initialize_headers(unittest2.TestCase):
Expand Down

0 comments on commit cd825cc

Please sign in to comment.