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: 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()


session (aiohttp.ClientSession) – An instance 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.

  • url (str) – The URL to be requested.

  • method (Optionalstr) – The HTTP method to use for the request. Defaults to ‘GET’.

  • body (Optionalbytes) – The payload or body in HTTP request.

  • headers (OptionalMappingstr, str) – Request headers.

  • timeout (Optionalint) – 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 requests.Session.request() method.


The HTTP response.

Return type:



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

class AuthorizedSession(credentials, refresh_status_codes=(HTTPStatus.UNAUTHORIZED,), max_refresh_attempts=2, refresh_timeout=None, auth_request=None, auto_decompress=False, **kwargs)[source]

Bases: 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', '')

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

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

  • refresh_status_codes (Sequenceint) – 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 (Optionalint) – 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.

  • kwargs – Additional arguments passed through to the underlying ClientSession aiohttp.ClientSession() object.

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.

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

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

  • data (Optionaldict) – Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • headers (Optionaldict) – Dictionary of HTTP Headers to send with the Request.

  • timeout (OptionalUnionfloat, 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 (Optionalfloat) –

    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.

property auth: BasicAuth | None

An object that represents HTTP Basic Authorization

property auto_decompress: bool

Should the body response be automatically decompressed.

async close() None

Close underlying connector.

Release all acquired resources.

property closed: bool

Is client session closed.

A readonly property.

property connector: BaseConnector | None

Connector instance used for the session.

property connector_owner: bool

Should connector be closed on session closing

property cookie_jar: AbstractCookieJar

The session cookies.

delete(url: str | URL, **kwargs: Any) _RequestContextManager

Perform HTTP DELETE request.

detach() None

Detach connector from session without closing the former.

Session is switched to closed state anyway.

get(url: str | URL, *, allow_redirects: bool = True, **kwargs: Any) _RequestContextManager

Perform HTTP GET request.

head(url: str | URL, *, allow_redirects: bool = False, **kwargs: Any) _RequestContextManager

Perform HTTP HEAD request.

property headers: CIMultiDict[str]

The default headers of the client session.

property json_serialize: Callable[[Any], str]

Json serializer callable

property loop: AbstractEventLoop

Session’s loop.

options(url: str | URL, *, allow_redirects: bool = True, **kwargs: Any) _RequestContextManager

Perform HTTP OPTIONS request.

patch(url: str | URL, *, data: Any | None = None, **kwargs: Any) _RequestContextManager

Perform HTTP PATCH request.

post(url: str | URL, *, data: Any | None = None, **kwargs: Any) _RequestContextManager

Perform HTTP POST request.

put(url: str | URL, *, data: Any | None = None, **kwargs: Any) _RequestContextManager

Perform HTTP PUT request.

property raise_for_status: bool | Callable[[ClientResponse], Awaitable[None]]

Should ClientResponse.raise_for_status() be called for each response.

property requote_redirect_url: bool

Do URL requoting on redirection handling.

property skip_auto_headers: FrozenSet[istr]

Headers for which autogeneration should be skipped

property timeout: ClientTimeout

Timeout for the session.

property trace_configs: List[TraceConfig]

A list of TraceConfig instances used for client tracing

property trust_env: bool

Should proxies information from environment or netrc be trusted.

Information is from HTTP_PROXY / HTTPS_PROXY environment variables or ~/.netrc file if present.

property version: Tuple[int, int]

The session HTTP protocol version.

ws_connect(url: str | URL, *, method: str = 'GET', protocols: Iterable[str] = (), timeout: float = 10.0, receive_timeout: float | None = None, autoclose: bool = True, autoping: bool = True, heartbeat: float | None = None, auth: BasicAuth | None = None, origin: str | None = None, params: Mapping[str, str] | None = None, headers: Mapping[str | istr, str] | CIMultiDict | CIMultiDictProxy | None = None, proxy: str | URL | None = None, proxy_auth: BasicAuth | None = None, ssl: SSLContext | bool | None | Fingerprint = None, verify_ssl: bool | None = None, fingerprint: bytes | None = None, ssl_context: SSLContext | None = None, proxy_headers: Mapping[str | istr, str] | CIMultiDict | CIMultiDictProxy | None = None, compress: int = 0, max_msg_size: int = 4194304) _WSRequestContextManager

Initiate websocket connection.