generated from deepgram/oss-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 114
feat: support short-lived tokens endpoint #517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved. | ||
| # Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| from .client import AuthRESTClient | ||
| from .client import AsyncAuthRESTClient | ||
| from .client import ( | ||
| GrantTokenResponse, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved. | ||
| # Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| from .v1.client import AuthRESTClient as AuthRESTClientLatest | ||
| from .v1.async_client import AsyncAuthRESTClient as AsyncAuthRESTClientLatest | ||
| from .v1.response import GrantTokenResponse as GrantTokenResponseLatest | ||
|
|
||
| AuthRESTClient = AuthRESTClientLatest | ||
| AsyncAuthRESTClient = AsyncAuthRESTClientLatest | ||
| GrantTokenResponse = GrantTokenResponseLatest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Copyright 2024 Deepgram SDK contributors. All Rights Reserved. | ||
| # Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
| # SPDX-License-Identifier: MIT | ||
| from .client import AuthRESTClient | ||
| from .async_client import AsyncAuthRESTClient | ||
| from .response import ( | ||
| GrantTokenResponse, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Copyright 2024 Deepgram SDK contributors. All Rights Reserved. | ||
| # Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| import logging | ||
|
|
||
| from ....utils import verboselogs | ||
| from ....options import DeepgramClientOptions | ||
| from ...common import AbstractAsyncRestClient | ||
| from .response import GrantTokenResponse | ||
|
|
||
|
|
||
| class AsyncAuthRESTClient(AbstractAsyncRestClient): | ||
| """ | ||
| A client class for handling authentication endpoints. | ||
| Provides method for generating a temporary JWT token. | ||
| """ | ||
|
|
||
| _logger: verboselogs.VerboseLogger | ||
| _config: DeepgramClientOptions | ||
| _endpoint: str | ||
|
|
||
| def __init__(self, config: DeepgramClientOptions): | ||
| self._logger = verboselogs.VerboseLogger(__name__) | ||
| self._logger.addHandler(logging.StreamHandler()) | ||
| self._logger.setLevel(config.verbose) | ||
| self._config = config | ||
| self._endpoint = "v1/auth/grant" | ||
| super().__init__(config) | ||
|
|
||
| async def grant_token(self): | ||
| """ | ||
| 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. | ||
| """ | ||
| self._logger.debug("AuthRestClient.grant_token ENTER") | ||
|
|
||
| url = f"{self._config.url}/{self._endpoint}" | ||
| self._logger.info("url: %s", url) | ||
| result = await self.post(url, headers={"Authorization": f"Token {self._config.api_key}"}) | ||
| self._logger.info("json: %s", result) | ||
| res = GrantTokenResponse.from_json(result) | ||
| self._logger.verbose("result: %s", res) | ||
| self._logger.notice("grant_token succeeded") | ||
| self._logger.debug("AuthRestClient.grant_token LEAVE") | ||
| return res |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Copyright 2024 Deepgram SDK contributors. All Rights Reserved. | ||
| # Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| import logging | ||
|
|
||
| from ....utils import verboselogs | ||
| from ....options import DeepgramClientOptions | ||
| from ...common import AbstractSyncRestClient | ||
| from .response import GrantTokenResponse | ||
|
|
||
|
|
||
| class AuthRESTClient(AbstractSyncRestClient): | ||
| """ | ||
| A client class for handling authentication endpoints. | ||
| Provides method for generating a temporary JWT token. | ||
| """ | ||
|
|
||
| _logger: verboselogs.VerboseLogger | ||
| _config: DeepgramClientOptions | ||
| _endpoint: str | ||
|
|
||
| def __init__(self, config: DeepgramClientOptions): | ||
| self._logger = verboselogs.VerboseLogger(__name__) | ||
| self._logger.addHandler(logging.StreamHandler()) | ||
| self._logger.setLevel(config.verbose) | ||
| self._config = config | ||
| self._endpoint = "v1/auth/grant" | ||
| super().__init__(config) | ||
|
|
||
| def grant_token(self): | ||
| """ | ||
| 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. | ||
| """ | ||
| self._logger.debug("AuthRestClient.grant_token ENTER") | ||
|
|
||
| url = f"{self._config.url}/{self._endpoint}" | ||
| self._logger.info("url: %s", url) | ||
| result = self.post(url, headers={"Authorization": f"Token {self._config.api_key}"}) | ||
| self._logger.info("json: %s", result) | ||
| res = GrantTokenResponse.from_json(result) | ||
| self._logger.verbose("result: %s", res) | ||
| self._logger.notice("grant_token succeeded") | ||
| self._logger.debug("AuthRestClient.grant_token LEAVE") | ||
| return res |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved. | ||
| # Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from dataclasses_json import config as dataclass_config | ||
|
|
||
| from ...common import ( | ||
| BaseResponse, | ||
| ) | ||
|
|
||
| @dataclass | ||
| class GrantTokenResponse(BaseResponse): | ||
| """ | ||
| The response object for the authentication grant token endpoint. | ||
| """ | ||
| access_token: str = field( | ||
| metadata=dataclass_config(field_name='access_token'), | ||
| default="", | ||
| ) | ||
| expires_in: int = field( | ||
| metadata=dataclass_config(field_name='expires_in'), | ||
| default=30, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved. | ||
| # Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| import asyncio | ||
| import os | ||
| from dotenv import load_dotenv | ||
| from deepgram.utils import verboselogs | ||
|
|
||
| from deepgram import ( | ||
| DeepgramClient, | ||
| DeepgramClientOptions | ||
| ) | ||
|
|
||
| load_dotenv() | ||
|
|
||
| async def main(): | ||
| try: | ||
| # STEP 1 Create a Deepgram client using the DEEPGRAM_API_KEY from your environment variables | ||
| config = DeepgramClientOptions( | ||
| verbose=verboselogs.SPAM, | ||
| ) | ||
| deepgram: DeepgramClient = DeepgramClient(os.getenv("DEEPGRAM_API_KEY", ""), config) | ||
|
|
||
| # STEP 2 Call the grant_token method on the auth rest class | ||
| response = await deepgram.asyncauth.v("1").grant_token() | ||
| print(f"response: {response}\n\n") | ||
| except Exception as e: | ||
| print(f"Exception: {e}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved. | ||
| # Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| import os | ||
| from dotenv import load_dotenv | ||
| from deepgram.utils import verboselogs | ||
|
|
||
| from deepgram import ( | ||
| DeepgramClient, | ||
| DeepgramClientOptions | ||
| ) | ||
|
|
||
| load_dotenv() | ||
|
|
||
| def main(): | ||
| try: | ||
| # STEP 1 Create a Deepgram client using the DEEPGRAM_API_KEY from your environment variables | ||
| config = DeepgramClientOptions( | ||
| verbose=verboselogs.SPAM, | ||
| ) | ||
| deepgram: DeepgramClient = DeepgramClient(os.getenv("DEEPGRAM_API_KEY", ""), config) | ||
|
|
||
| # STEP 2 Call the grant_token method on the auth rest class | ||
| response = deepgram.auth.v("1").grant_token() | ||
| print(f"response: {response}\n\n") | ||
| except Exception as e: | ||
| print(f"Exception: {e}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.