deepgram.clients.auth.v1.async_client

 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
 5import logging
 6
 7from ....utils import verboselogs
 8from ....options import DeepgramClientOptions
 9from ...common import AbstractAsyncRestClient
10from .response import GrantTokenResponse
11
12
13class 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
14class AsyncAuthRESTClient(AbstractAsyncRestClient):
15    """
16    A client class for handling authentication endpoints.
17    Provides method for generating a temporary JWT token.
18    """
19
20    _logger: verboselogs.VerboseLogger
21    _config: DeepgramClientOptions
22    _endpoint: str
23
24    def __init__(self, config: DeepgramClientOptions):
25        self._logger = verboselogs.VerboseLogger(__name__)
26        self._logger.addHandler(logging.StreamHandler())
27        self._logger.setLevel(config.verbose)
28        self._config = config
29        self._endpoint = "v1/auth/grant"
30        super().__init__(config)
31
32    async def grant_token(self):
33        """
34        Generates a temporary JWT with a 30 second TTL.
35
36        Returns:
37            GrantTokenResponse: An object containing the authentication token and its expiration time.
38
39        Raises:
40            DeepgramTypeError: Raised for known API errors.
41        """
42        self._logger.debug("AuthRestClient.grant_token ENTER")
43
44        url = f"{self._config.url}/{self._endpoint}"
45        self._logger.info("url: %s", url)
46        result = await self.post(url, headers={"Authorization": f"Token {self._config.api_key}"})
47        self._logger.info("json: %s", result)
48        res = GrantTokenResponse.from_json(result)
49        self._logger.verbose("result: %s", res)
50        self._logger.notice("grant_token succeeded")
51        self._logger.debug("AuthRestClient.grant_token LEAVE")
52        return res

A client class for handling authentication endpoints. Provides method for generating a temporary JWT token.

AsyncAuthRESTClient(config: deepgram.options.DeepgramClientOptions)
24    def __init__(self, config: DeepgramClientOptions):
25        self._logger = verboselogs.VerboseLogger(__name__)
26        self._logger.addHandler(logging.StreamHandler())
27        self._logger.setLevel(config.verbose)
28        self._config = config
29        self._endpoint = "v1/auth/grant"
30        super().__init__(config)
async def grant_token(self):
32    async def grant_token(self):
33        """
34        Generates a temporary JWT with a 30 second TTL.
35
36        Returns:
37            GrantTokenResponse: An object containing the authentication token and its expiration time.
38
39        Raises:
40            DeepgramTypeError: Raised for known API errors.
41        """
42        self._logger.debug("AuthRestClient.grant_token ENTER")
43
44        url = f"{self._config.url}/{self._endpoint}"
45        self._logger.info("url: %s", url)
46        result = await self.post(url, headers={"Authorization": f"Token {self._config.api_key}"})
47        self._logger.info("json: %s", result)
48        res = GrantTokenResponse.from_json(result)
49        self._logger.verbose("result: %s", res)
50        self._logger.notice("grant_token succeeded")
51        self._logger.debug("AuthRestClient.grant_token LEAVE")
52        return res

Generates a temporary JWT with a 30 second TTL.

Returns: GrantTokenResponse: An object containing the authentication token and its expiration time.

Raises: DeepgramTypeError: Raised for known API errors.