google.auth.transport.aiohttp_requests module

Transport adapter for Async HTTP (aiohttp).

NOTE: This async support is experimental and marked internal. This surface may change in minor releases.

class Request(session=None)[source]

Bases: google.auth.transport.Request

Requests request adapter.

This class is used internally for making requests using asyncio transports in a consistent way. If you use AuthorizedSession you do not need to construct or use this class directly.

This class can be useful if you want to manually refresh a Credentials instance:

import google.auth.transport.aiohttp_requests

request = google.auth.transport.aiohttp_requests.Request()

credentials.refresh(request)
Parameters

session (aiohttp.ClientSession) – An instance :class: aiohttp.ClientSession used to make HTTP requests. If not specified, a session will be created.

async __call__(url, method='GET', body=None, headers=None, timeout=180, **kwargs)[source]

Make an HTTP request using aiohttp.

Parameters
  • url (str) – The URL 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 requests default timeout will be used.

  • kwargs – Additional arguments passed through to the underlying requests request() method.

Returns

The HTTP response.

Return type

google.auth.transport.Response

Raises

google.auth.exceptions.TransportError – If any exception occurred.

class AuthorizedSession(credentials, refresh_status_codes=(<HTTPStatus.UNAUTHORIZED: 401>, ), max_refresh_attempts=2, refresh_timeout=None, auth_request=None, auto_decompress=False)[source]

Bases: aiohttp.client.ClientSession

This is an async implementation of the Authorized Session class. We utilize an aiohttp transport instance, and the interface mirrors the google.auth.transport.requests Authorized Session class, except for the change in the transport used in the async use case.

A Requests Session class with credentials.

This class is used to perform requests to API endpoints that require authorization:

from google.auth.transport import aiohttp_requests

async with aiohttp_requests.AuthorizedSession(credentials) as authed_session:
    response = await authed_session.request(
        'GET', 'https://www.googleapis.com/storage/v1/b')

The underlying request() implementation handles adding the credentials’ headers to the request and refreshing credentials as needed.

Parameters
  • credentials (google.auth._credentials_async.Credentials) – The credentials to add to the request.

  • refresh_status_codes (Sequence [ int ]) – Which HTTP status codes indicate that credentials should be refreshed and the request should be retried.

  • max_refresh_attempts (int) – The maximum number of times to attempt to refresh the credentials and retry the request.

  • refresh_timeout (Optional [ int ]) – The timeout value in seconds for credential refresh HTTP requests.

  • auth_request (google.auth.transport.aiohttp_requests.Request) – (Optional) An instance of Request used when refreshing credentials. If not passed, an instance of Request is created.

async request(method, url, data=None, headers=None, max_allowed_time=None, timeout=180, auto_decompress=False, **kwargs)[source]

Implementation of Authorized Session aiohttp request.

Parameters
  • method – The http request method used (e.g. GET, PUT, DELETE)

  • url – The url at which the http request is sent.

  • data – These fields parallel the associated data and headers

  • headers – These fields parallel the associated data and headers

  • of a regular http request. Using the aiohttp client session to (fields) –

  • the http request allows us to use this parallel corresponding structure (send) –

  • our Authorized Session class. (in) –

  • timeout (Optional [ Union [ float, aiohttp.ClientTimeout ] ]) –

    The amount of time in seconds to wait for the server response with each individual request.

    Can also be passed as an aiohttp.ClientTimeout object.

  • max_allowed_time (Optional [ float ]) –

    If the method runs longer than this, a Timeout exception is automatically raised. Unlike the ``timeout` parameter, this value applies to the total method execution time, even if multiple requests are made under the hood.

    Mind that it is not guaranteed that the timeout error is raised at ``max_allowed_time`. It might take longer, for example, if an underlying request takes a lot of time, but the request itself does not timeout, e.g. if a large file is being transmitted. The timout error will be raised after such request completes.

async close()None

Close underlying connector.

Release all acquired resources.

property closed

Is client session closed.

A readonly property.

property connector

Connector instance used for the session.

property cookie_jar

The session cookies.

delete(url: Union[str, yarl.URL], **kwargs: Any) → aiohttp.client._RequestContextManager

Perform HTTP DELETE request.

detach()None

Detach connector from session without closing the former.

Session is switched to closed state anyway.

get(url: Union[str, yarl.URL], *, allow_redirects: bool = True, **kwargs: Any) → aiohttp.client._RequestContextManager

Perform HTTP GET request.

head(url: Union[str, yarl.URL], *, allow_redirects: bool = False, **kwargs: Any) → aiohttp.client._RequestContextManager

Perform HTTP HEAD request.

property loop

Session’s loop.

options(url: Union[str, yarl.URL], *, allow_redirects: bool = True, **kwargs: Any) → aiohttp.client._RequestContextManager

Perform HTTP OPTIONS request.

patch(url: Union[str, yarl.URL], *, data: Any = None, **kwargs: Any) → aiohttp.client._RequestContextManager

Perform HTTP PATCH request.

post(url: Union[str, yarl.URL], *, data: Any = None, **kwargs: Any) → aiohttp.client._RequestContextManager

Perform HTTP POST request.

put(url: Union[str, yarl.URL], *, data: Any = None, **kwargs: Any) → aiohttp.client._RequestContextManager

Perform HTTP PUT request.

property requote_redirect_url

Do URL requoting on redirection handling.

property version

The session HTTP protocol version.

ws_connect(url: Union[str, yarl.URL], *, method: str = 'GET', protocols: Iterable[str] = (), timeout: float = 10.0, receive_timeout: Optional[float] = None, autoclose: bool = True, autoping: bool = True, heartbeat: Optional[float] = None, auth: Optional[aiohttp.helpers.BasicAuth] = None, origin: Optional[str] = None, headers: Optional[Union[Mapping[Union[str, multidict._multidict.istr], str], multidict._multidict.CIMultiDict, multidict._multidict.CIMultiDictProxy]] = None, proxy: Optional[Union[str, yarl.URL]] = None, proxy_auth: Optional[aiohttp.helpers.BasicAuth] = None, ssl: Union[ssl.SSLContext, bool, None, aiohttp.client_reqrep.Fingerprint] = None, verify_ssl: Optional[bool] = None, fingerprint: Optional[bytes] = None, ssl_context: Optional[ssl.SSLContext] = None, proxy_headers: Optional[Union[Mapping[Union[str, multidict._multidict.istr], str], multidict._multidict.CIMultiDict, multidict._multidict.CIMultiDictProxy]] = None, compress: int = 0, max_msg_size: int = 4194304) → aiohttp.client._WSRequestContextManager

Initiate websocket connection.