-
Notifications
You must be signed in to change notification settings - Fork 312
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
feat: add support for async rest #1529
Draft
ohmayr
wants to merge
1
commit into
main
Choose a base branch
from
support-async-rest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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,171 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# 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. | ||
|
||
|
||
"""Interfaces for credentials.""" | ||
|
||
import abc | ||
import inspect | ||
|
||
from google.auth import credentials | ||
|
||
|
||
class Credentials(credentials.Credentials, metaclass=abc.ABCMeta): | ||
"""Async inherited credentials class from google.auth.credentials. | ||
The added functionality is the before_request call which requires | ||
async/await syntax. | ||
All credentials have a :attr:`token` that is used for authentication and | ||
may also optionally set an :attr:`expiry` to indicate when the token will | ||
no longer be valid. | ||
|
||
Most credentials will be :attr:`invalid` until :meth:`refresh` is called. | ||
Credentials can do this automatically before the first HTTP request in | ||
:meth:`before_request`. | ||
|
||
Although the token and expiration will change as the credentials are | ||
:meth:`refreshed <refresh>` and used, credentials should be considered | ||
immutable. Various credentials will accept configuration such as private | ||
keys, scopes, and other options. These options are not changeable after | ||
construction. Some classes will provide mechanisms to copy the credentials | ||
with modifications such as :meth:`ScopedCredentials.with_scopes`. | ||
""" | ||
|
||
async def before_request(self, request, method, url, headers): | ||
"""Performs credential-specific before request logic. | ||
|
||
Refreshes the credentials if necessary, then calls :meth:`apply` to | ||
apply the token to the authentication header. | ||
|
||
Args: | ||
request (google.auth.transport.Request): The object used to make | ||
HTTP requests. | ||
method (str): The request's HTTP method or the RPC method being | ||
invoked. | ||
url (str): The request's URI or the RPC service's URI. | ||
headers (Mapping): The request's headers. | ||
""" | ||
# pylint: disable=unused-argument | ||
# (Subclasses may use these arguments to ascertain information about | ||
# the http request.) | ||
|
||
if not self.valid: | ||
if inspect.iscoroutinefunction(self.refresh): | ||
await self.refresh(request) | ||
else: | ||
self.refresh(request) | ||
self.apply(headers) | ||
|
||
|
||
class CredentialsWithQuotaProject(credentials.CredentialsWithQuotaProject): | ||
"""Abstract base for credentials supporting ``with_quota_project`` factory""" | ||
|
||
|
||
class AnonymousCredentials(credentials.AnonymousCredentials, Credentials): | ||
"""Credentials that do not provide any authentication information. | ||
|
||
These are useful in the case of services that support anonymous access or | ||
local service emulators that do not use credentials. This class inherits | ||
from the sync anonymous credentials file, but is kept if async credentials | ||
is initialized and we would like anonymous credentials. | ||
""" | ||
|
||
|
||
class ReadOnlyScoped(credentials.ReadOnlyScoped, metaclass=abc.ABCMeta): | ||
"""Interface for credentials whose scopes can be queried. | ||
|
||
OAuth 2.0-based credentials allow limiting access using scopes as described | ||
in `RFC6749 Section 3.3`_. | ||
If a credential class implements this interface then the credentials either | ||
use scopes in their implementation. | ||
|
||
Some credentials require scopes in order to obtain a token. You can check | ||
if scoping is necessary with :attr:`requires_scopes`:: | ||
|
||
if credentials.requires_scopes: | ||
# Scoping is required. | ||
credentials = _credentials_async.with_scopes(scopes=['one', 'two']) | ||
|
||
Credentials that require scopes must either be constructed with scopes:: | ||
|
||
credentials = SomeScopedCredentials(scopes=['one', 'two']) | ||
|
||
Or must copy an existing instance using :meth:`with_scopes`:: | ||
|
||
scoped_credentials = _credentials_async.with_scopes(scopes=['one', 'two']) | ||
|
||
Some credentials have scopes but do not allow or require scopes to be set, | ||
these credentials can be used as-is. | ||
|
||
.. _RFC6749 Section 3.3: https://tools.ietf.org/html/rfc6749#section-3.3 | ||
""" | ||
|
||
|
||
class Scoped(credentials.Scoped): | ||
"""Interface for credentials whose scopes can be replaced while copying. | ||
|
||
OAuth 2.0-based credentials allow limiting access using scopes as described | ||
in `RFC6749 Section 3.3`_. | ||
If a credential class implements this interface then the credentials either | ||
use scopes in their implementation. | ||
|
||
Some credentials require scopes in order to obtain a token. You can check | ||
if scoping is necessary with :attr:`requires_scopes`:: | ||
|
||
if credentials.requires_scopes: | ||
# Scoping is required. | ||
credentials = _credentials_async.create_scoped(['one', 'two']) | ||
|
||
Credentials that require scopes must either be constructed with scopes:: | ||
|
||
credentials = SomeScopedCredentials(scopes=['one', 'two']) | ||
|
||
Or must copy an existing instance using :meth:`with_scopes`:: | ||
|
||
scoped_credentials = credentials.with_scopes(scopes=['one', 'two']) | ||
|
||
Some credentials have scopes but do not allow or require scopes to be set, | ||
these credentials can be used as-is. | ||
|
||
.. _RFC6749 Section 3.3: https://tools.ietf.org/html/rfc6749#section-3.3 | ||
""" | ||
|
||
|
||
def with_scopes_if_required(credentials, scopes): | ||
"""Creates a copy of the credentials with scopes if scoping is required. | ||
|
||
This helper function is useful when you do not know (or care to know) the | ||
specific type of credentials you are using (such as when you use | ||
:func:`google.auth.default`). This function will call | ||
:meth:`Scoped.with_scopes` if the credentials are scoped credentials and if | ||
the credentials require scoping. Otherwise, it will return the credentials | ||
as-is. | ||
|
||
Args: | ||
credentials (google.auth.credentials.Credentials): The credentials to | ||
scope if necessary. | ||
scopes (Sequence[str]): The list of scopes to use. | ||
|
||
Returns: | ||
google.auth._credentials_async.Credentials: Either a new set of scoped | ||
credentials, or the passed in credentials instance if no scoping | ||
was required. | ||
""" | ||
if isinstance(credentials, Scoped) and credentials.requires_scopes: | ||
return credentials.with_scopes(scopes) | ||
else: | ||
return credentials | ||
|
||
|
||
class Signing(credentials.Signing, metaclass=abc.ABCMeta): | ||
"""Interface for credentials that can cryptographically sign messages.""" |
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,46 @@ | ||
import time | ||
import numbers | ||
import requests.exceptions # pylint: disable=ungrouped-imports | ||
|
||
class TimeoutGuard(object): | ||
"""A context manager raising an error if the suite execution took too long. | ||
|
||
Args: | ||
timeout (Union[None, Union[float, Tuple[float, float]]]): | ||
The maximum number of seconds a suite can run without the context | ||
manager raising a timeout exception on exit. If passed as a tuple, | ||
the smaller of the values is taken as a timeout. If ``None``, a | ||
timeout error is never raised. | ||
timeout_error_type (Optional[Exception]): | ||
The type of the error to raise on timeout. Defaults to | ||
:class:`requests.exceptions.Timeout`. | ||
""" | ||
|
||
def __init__(self, timeout, timeout_error_type=requests.exceptions.Timeout): | ||
self._timeout = timeout | ||
self.remaining_timeout = timeout | ||
self._timeout_error_type = timeout_error_type | ||
|
||
def __enter__(self): | ||
self._start = time.time() | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
if exc_value: | ||
return # let the error bubble up automatically | ||
|
||
if self._timeout is None: | ||
return # nothing to do, the timeout was not specified | ||
|
||
elapsed = time.time() - self._start | ||
deadline_hit = False | ||
|
||
if isinstance(self._timeout, numbers.Number): | ||
self.remaining_timeout = self._timeout - elapsed | ||
deadline_hit = self.remaining_timeout <= 0 | ||
else: | ||
self.remaining_timeout = tuple(x - elapsed for x in self._timeout) | ||
deadline_hit = min(self.remaining_timeout) <= 0 | ||
|
||
if deadline_hit: | ||
raise self._timeout_error_type() |
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,103 @@ | ||
# Copyright 2016 Google LLC | ||
# | ||
# 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 - HTTP client library support. | ||
|
||
:mod:`google.auth` is designed to work with various HTTP client libraries such | ||
as urllib3 and requests. In order to work across these libraries with different | ||
interfaces some abstraction is needed. | ||
|
||
This module provides two interfaces that are implemented by transport adapters | ||
to support HTTP libraries. :class:`Request` defines the interface expected by | ||
:mod:`google.auth` to make requests. :class:`Response` defines the interface | ||
for the return value of :class:`Request`. | ||
""" | ||
|
||
import abc | ||
import http.client as http_client | ||
|
||
DEFAULT_RETRYABLE_STATUS_CODES = ( | ||
http_client.INTERNAL_SERVER_ERROR, | ||
http_client.SERVICE_UNAVAILABLE, | ||
http_client.REQUEST_TIMEOUT, | ||
http_client.TOO_MANY_REQUESTS, | ||
) | ||
"""Sequence[int]: HTTP status codes indicating a request can be retried. | ||
""" | ||
|
||
|
||
DEFAULT_REFRESH_STATUS_CODES = (http_client.UNAUTHORIZED,) | ||
"""Sequence[int]: Which HTTP status code indicate that credentials should be | ||
refreshed. | ||
""" | ||
|
||
DEFAULT_MAX_REFRESH_ATTEMPTS = 2 | ||
"""int: How many times to refresh the credentials and retry a request.""" | ||
|
||
|
||
class Response(metaclass=abc.ABCMeta): | ||
"""HTTP Response data.""" | ||
|
||
@abc.abstractproperty | ||
def status(self): | ||
"""int: The HTTP status code.""" | ||
raise NotImplementedError("status must be implemented.") | ||
|
||
@abc.abstractproperty | ||
def headers(self): | ||
"""Mapping[str, str]: The HTTP response headers.""" | ||
raise NotImplementedError("headers must be implemented.") | ||
|
||
@abc.abstractproperty | ||
def data(self): | ||
"""bytes: The response body.""" | ||
raise NotImplementedError("data must be implemented.") | ||
|
||
|
||
class Request(metaclass=abc.ABCMeta): | ||
"""Interface for a callable that makes HTTP requests. | ||
|
||
Specific transport implementations should provide an implementation of | ||
this that adapts their specific request / response API. | ||
|
||
.. automethod:: __call__ | ||
""" | ||
|
||
@abc.abstractmethod | ||
def __call__( | ||
self, url, method="GET", body=None, headers=None, timeout=None, **kwargs | ||
): | ||
"""Make an HTTP request. | ||
|
||
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[str, str]): Request headers. | ||
timeout (Optional[int]): The number of seconds to wait for a | ||
response from the server. If not specified or if None, the | ||
transport-specific default timeout will be used. | ||
kwargs: Additionally arguments passed on to the transport's | ||
request method. | ||
|
||
Returns: | ||
Response: The HTTP response. | ||
|
||
Raises: | ||
google.auth.exceptions.TransportError: If any exception occurred. | ||
""" | ||
# pylint: disable=redundant-returns-doc, missing-raises-doc | ||
# (pylint doesn't play well with abstract docstrings.) | ||
raise NotImplementedError("__call__ must be implemented.") |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, inheriting from the sync classes is a poor design choice and will make the async code unwieldy.