Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add urllib3 transport #14

Merged
merged 2 commits into from
Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/reference/google.auth.compute_engine.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
google.auth.compute_engine package
==================================

.. automodule:: google.auth.compute_engine
:members:
:undoc-members:
:show-inheritance:

1 change: 1 addition & 0 deletions docs/reference/google.auth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Subpackages

.. toctree::

google.auth.compute_engine
google.auth.transport

Submodules
Expand Down
7 changes: 7 additions & 0 deletions docs/reference/google.auth.transport.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ google.auth.transport package
:undoc-members:
:show-inheritance:

Submodules
----------

.. toctree::

google.auth.transport.urllib3

7 changes: 7 additions & 0 deletions docs/reference/google.auth.transport.urllib3.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
google.auth.transport.urllib3 module
====================================

.. automodule:: google.auth.transport.urllib3
:members:
:undoc-members:
:show-inheritance:
2 changes: 1 addition & 1 deletion google/auth/transport/_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


class Response(transport.Response):
"""http.client transport request adapter.
"""http.client transport response adapter.

Args:
response (http.client.HTTPResponse): The raw http client response.
Expand Down
91 changes: 91 additions & 0 deletions google/auth/transport/urllib3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Transport adapter for urllib3."""

from __future__ import absolute_import

import urllib3
import urllib3.exceptions

from google.auth import exceptions
from google.auth import transport


class Response(transport.Response):
"""urllib3 transport response adapter.

Args:
response (urllib3.response.HTTPResponse): The raw urllib3 response.
"""
def __init__(self, response):
self._response = response

@property
def status(self):
return self._response.status

@property
def headers(self):
return self._response.headers

@property
def data(self):
return self._response.data


class Request(transport.Request):
"""urllib3 request adapter

Args:
http (urllib3.requests.RequestMethods): An instance of any urllib3
class that implements :class:`~urllib3.requests.RequestMethods`,
usually :class:`urllib3.PoolManager`.
"""
def __init__(self, http):
self.http = http

def __call__(self, url, method='GET', body=None, headers=None,
timeout=None, **kwargs):
"""Make an HTTP request using urllib3.

Args:
url (str): The URI to be requested.
method (str): The HTTP method to use for the request. Defaults
to 'GET'.
body (bytes): The payload / body in HTTP request.
headers (Mapping): Request headers.
timeout (Optional(int)): The number of seconds to wait for a
response from the server. If not specified or if None, the
urllib3 default timeout will be used.
kwargs: Additional arguments passed throught to the underlying
urllib3 :meth:`urlopen` method.

Returns:
Response: The HTTP response.

Raises:
google.auth.exceptions.TransportError: If any exception occurred.
"""
# urllib3 uses a sentinel default value for timeout, so only set it if
# specified.

This comment was marked as spam.

This comment was marked as spam.

if timeout is not None:
kwargs['timeout'] = timeout

try:
response = self.http.request(
method, url, body=body, headers=headers, **kwargs)
return Response(response)
except urllib3.exceptions.HTTPError as exc:
raise exceptions.TransportError(exc)
2 changes: 1 addition & 1 deletion tests/transport/compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

class RequestResponseTests(object):

@pytest.fixture
@pytest.fixture(scope='module')
def server(self):
"""Provides a test HTTP server.

Expand Down
33 changes: 33 additions & 0 deletions tests/transport/test_urllib3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import mock
import urllib3

import google.auth.transport.urllib3
from tests.transport import compliance


class TestRequestResponse(compliance.RequestResponseTests):
def make_request(self):
http = urllib3.PoolManager()
return google.auth.transport.urllib3.Request(http)


def test_timeout():
http = mock.Mock()
request = google.auth.transport.urllib3.Request(http)
request(url='http://example.com', method='GET', timeout=5)

assert http.request.call_args[1]['timeout'] == 5