File: async_auth0.py

package info (click to toggle)
auth0-python 4.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,280 kB
  • sloc: python: 8,933; makefile: 15; sh: 2
file content (67 lines) | stat: -rw-r--r-- 2,067 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from __future__ import annotations

from typing import TYPE_CHECKING

import aiohttp

from ..asyncify import asyncify
from .auth0 import Auth0

if TYPE_CHECKING:
    from types import TracebackType

    from auth0.rest import RestClientOptions


class AsyncAuth0:
    """Provides easy access to all endpoint classes

    Args:
        domain (str): Your Auth0 domain, for example 'username.auth0.com'

        token (str): Management API v2 Token

        rest_options (RestClientOptions): Pass an instance of
            RestClientOptions to configure additional RestClient
            options, such as rate-limit retries.
            (defaults to None)
    """

    def __init__(
        self, domain: str, token: str, rest_options: RestClientOptions | None = None
    ) -> None:
        self._services = []
        for name, attr in vars(Auth0(domain, token, rest_options=rest_options)).items():
            cls = asyncify(attr.__class__)
            service = cls(domain=domain, token=token, rest_options=rest_options)
            self._services.append(service)
            setattr(
                self,
                name,
                service,
            )

    def set_session(self, session: aiohttp.ClientSession) -> None:
        """Set Client Session to improve performance by reusing session.

        Args:
            session (aiohttp.ClientSession): The client session which should be closed
                manually or within context manager.
        """
        self._session = session
        for service in self._services:
            service.set_session(self._session)

    async def __aenter__(self) -> AsyncAuth0:
        """Automatically create and set session within context manager."""
        self.set_session(aiohttp.ClientSession())
        return self

    async def __aexit__(
        self,
        exc_type: type[BaseException] | None,
        exc_val: BaseException | None,
        exc_tb: TracebackType | None,
    ) -> None:
        """Automatically close session within context manager."""
        await self._session.close()