|
| 1 | +# Copyright 2024 Deepgram SDK contributors. All Rights Reserved. |
| 2 | +# Use of this source code is governed by a MIT license that can be found in the LICENSE file. |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import logging |
| 6 | + |
| 7 | +from ....utils import verboselogs |
| 8 | +from ....options import DeepgramClientOptions |
| 9 | +from ...common import AbstractAsyncRestClient |
| 10 | +from .response import GrantTokenResponse |
| 11 | + |
| 12 | + |
| 13 | +class AsyncAuthRESTClient(AbstractAsyncRestClient): |
| 14 | + """ |
| 15 | + A client class for handling authentication endpoints. |
| 16 | + Provides method for generating a temporary JWT token. |
| 17 | + """ |
| 18 | + |
| 19 | + _logger: verboselogs.VerboseLogger |
| 20 | + _config: DeepgramClientOptions |
| 21 | + _endpoint: str |
| 22 | + |
| 23 | + def __init__(self, config: DeepgramClientOptions): |
| 24 | + self._logger = verboselogs.VerboseLogger(__name__) |
| 25 | + self._logger.addHandler(logging.StreamHandler()) |
| 26 | + self._logger.setLevel(config.verbose) |
| 27 | + self._config = config |
| 28 | + self._endpoint = "v1/auth/grant" |
| 29 | + super().__init__(config) |
| 30 | + |
| 31 | + async def grant_token(self): |
| 32 | + """ |
| 33 | + Generates a temporary JWT with a 30 second TTL. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + GrantTokenResponse: An object containing the authentication token and its expiration time. |
| 37 | +
|
| 38 | + Raises: |
| 39 | + DeepgramTypeError: Raised for known API errors. |
| 40 | + """ |
| 41 | + self._logger.debug("AuthRestClient.grant_token ENTER") |
| 42 | + |
| 43 | + url = f"{self._config.url}/{self._endpoint}" |
| 44 | + self._logger.info("url: %s", url) |
| 45 | + result = await self.post(url, headers={"Authorization": f"Token {self._config.api_key}"}) |
| 46 | + self._logger.info("json: %s", result) |
| 47 | + res = GrantTokenResponse.from_json(result) |
| 48 | + self._logger.verbose("result: %s", res) |
| 49 | + self._logger.notice("grant_token succeeded") |
| 50 | + self._logger.debug("AuthRestClient.grant_token LEAVE") |
| 51 | + return res |
0 commit comments