| 13 | |
| 14 | |
| 15 | class AsyncAssertionClient(_AssertionClient, httpx2.AsyncClient): |
| 16 | token_auth_class = OAuth2Auth |
| 17 | oauth_error_class = OAuthError |
| 18 | JWT_BEARER_GRANT_TYPE = JWTBearerGrant.GRANT_TYPE |
| 19 | ASSERTION_METHODS = { |
| 20 | JWT_BEARER_GRANT_TYPE: JWTBearerGrant.sign, |
| 21 | } |
| 22 | DEFAULT_GRANT_TYPE = JWT_BEARER_GRANT_TYPE |
| 23 | |
| 24 | def __init__( |
| 25 | self, |
| 26 | token_endpoint, |
| 27 | issuer, |
| 28 | subject, |
| 29 | audience=None, |
| 30 | grant_type=None, |
| 31 | claims=None, |
| 32 | token_placement="header", |
| 33 | scope=None, |
| 34 | client_id=None, |
| 35 | **kwargs, |
| 36 | ): |
| 37 | client_kwargs = extract_client_kwargs(kwargs) |
| 38 | httpx2.AsyncClient.__init__(self, **client_kwargs) |
| 39 | |
| 40 | _AssertionClient.__init__( |
| 41 | self, |
| 42 | session=None, |
| 43 | token_endpoint=token_endpoint, |
| 44 | issuer=issuer, |
| 45 | subject=subject, |
| 46 | audience=audience, |
| 47 | grant_type=grant_type, |
| 48 | claims=claims, |
| 49 | token_placement=token_placement, |
| 50 | scope=scope, |
| 51 | client_id=client_id, |
| 52 | **kwargs, |
| 53 | ) |
| 54 | |
| 55 | async def request( |
| 56 | self, method, url, withhold_token=False, auth=USE_CLIENT_DEFAULT, **kwargs |
| 57 | ) -> Response: |
| 58 | """Send request with auto refresh token feature.""" |
| 59 | if not withhold_token and auth is USE_CLIENT_DEFAULT: |
| 60 | if not self.token or self.token.is_expired(): |
| 61 | await self.refresh_token() |
| 62 | |
| 63 | auth = self.token_auth |
| 64 | return await super().request(method, url, auth=auth, **kwargs) |
| 65 | |
| 66 | async def _refresh_token(self, data): |
| 67 | resp = await self.request( |
| 68 | "POST", self.token_endpoint, data=data, withhold_token=True |
| 69 | ) |
| 70 | |
| 71 | return self.parse_response_token(resp) |
| 72 |
no outgoing calls