-
Notifications
You must be signed in to change notification settings - Fork 309
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ Subpackages | |
|
||
.. toctree:: | ||
|
||
google.auth.compute_engine | ||
google.auth.transport | ||
|
||
Submodules | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.