cirro_api_client.cirro_auth

 1import typing
 2from abc import ABC
 3
 4from attr import define
 5from httpx import Auth, Request, Response
 6
 7
 8class AuthMethod(Auth, ABC):
 9    """
10    Defines the method used for authenticating with Cirro
11    """
12
13
14@define
15class TokenAuth(AuthMethod):
16    token: str
17
18    def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:
19        request.headers["Authorization"] = f"Bearer {self.token}"
20        yield request
21
22
23@define
24class RefreshableTokenAuth(AuthMethod):
25    token_getter: typing.Callable[[], str]
26
27    def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:
28        request.headers["Authorization"] = f"Bearer {self.token_getter()}"
29        yield request
class AuthMethod(httpx.Auth, abc.ABC):
 9class AuthMethod(Auth, ABC):
10    """
11    Defines the method used for authenticating with Cirro
12    """

Defines the method used for authenticating with Cirro

@define
class TokenAuth(AuthMethod):
15@define
16class TokenAuth(AuthMethod):
17    token: str
18
19    def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:
20        request.headers["Authorization"] = f"Bearer {self.token}"
21        yield request

Defines the method used for authenticating with Cirro

TokenAuth(token: str)
23def __init__(self, token):
24    self.token = token

Method generated by attrs for class TokenAuth.

token: str
def auth_flow( self, request: httpx.Request) -> Generator[httpx.Request, httpx.Response, NoneType]:
19    def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:
20        request.headers["Authorization"] = f"Bearer {self.token}"
21        yield request

Execute the authentication flow.

To dispatch a request, yield it:

yield request

The client will .send() the response back into the flow generator. You can access it like so:

response = yield request

A return (or reaching the end of the generator) will result in the client returning the last response obtained from the server.

You can dispatch as many requests as is necessary.

@define
class RefreshableTokenAuth(AuthMethod):
24@define
25class RefreshableTokenAuth(AuthMethod):
26    token_getter: typing.Callable[[], str]
27
28    def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:
29        request.headers["Authorization"] = f"Bearer {self.token_getter()}"
30        yield request

Defines the method used for authenticating with Cirro

RefreshableTokenAuth(token_getter: Callable[[], str])
23def __init__(self, token_getter):
24    self.token_getter = token_getter

Method generated by attrs for class RefreshableTokenAuth.

token_getter: Callable[[], str]
def auth_flow( self, request: httpx.Request) -> Generator[httpx.Request, httpx.Response, NoneType]:
28    def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:
29        request.headers["Authorization"] = f"Bearer {self.token_getter()}"
30        yield request

Execute the authentication flow.

To dispatch a request, yield it:

yield request

The client will .send() the response back into the flow generator. You can access it like so:

response = yield request

A return (or reaching the end of the generator) will result in the client returning the last response obtained from the server.

You can dispatch as many requests as is necessary.