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

credentials.refresh(request)
Parameters:

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.

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

  • method (Optional [ str ]) – The HTTP method to use for the request. Defaults to ‘GET’.

  • body (Optional [ bytes ]) – The payload or body in HTTP request.

  • headers (Optional [ 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 requests.Session.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: 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 (str) – The http request method used (e.g. GET, PUT, DELETE)

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

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

  • headers (Optional [ dict ]) – Dictionary of HTTP Headers to send with the Request.

  • 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.

property auth: Optional[BasicAuth]

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: Optional[BaseConnector]

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: Union[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: Union[str, URL], *, allow_redirects: bool = True, **kwargs: Any) _RequestContextManager

Perform HTTP GET request.

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

Perform HTTP HEAD request.

property headers: CIMultiDict

The default headers of the client session.

property json_serialize: Callable[[Any], str]

Json serializer callable

property loop: AbstractEventLoop

Session’s loop.

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

Perform HTTP OPTIONS request.

patch(url: Union[str, URL], *, data: Optional[Any] = None, **kwargs: Any) _RequestContextManager

Perform HTTP PATCH request.

post(url: Union[str, URL], *, data: Optional[Any] = None, **kwargs: Any) _RequestContextManager

Perform HTTP POST request.

put(url: Union[str, URL], *, data: Optional[Any] = None, **kwargs: Any) _RequestContextManager

Perform HTTP PUT request.

property raise_for_status: Union[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: Union[object, 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: Union[str, 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[BasicAuth] = None, origin: Optional[str] = None, params: Optional[Mapping[str, str]] = None, headers: Optional[Union[Mapping[Union[str, istr], str], CIMultiDict, CIMultiDictProxy]] = None, proxy: Optional[Union[str, URL]] = None, proxy_auth: Optional[BasicAuth] = None, ssl: Union[SSLContext, bool, None, Fingerprint] = None, verify_ssl: Optional[bool] = None, fingerprint: Optional[bytes] = None, ssl_context: Optional[SSLContext] = None, proxy_headers: Optional[Union[Mapping[Union[str, istr], str], CIMultiDict, CIMultiDictProxy]] = None, compress: int = 0, max_msg_size: int = 4194304) _WSRequestContextManager

Initiate websocket connection.