deepgram.clients.read_router
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 5from importlib import import_module 6import logging 7 8from ..utils import verboselogs 9from ..options import DeepgramClientOptions 10from .errors import DeepgramModuleError 11 12 13class ReadRouter: 14 """ 15 Represents a client for interacting with the Deepgram API. 16 17 This class provides a client for making requests to the Deepgram API with various configuration options. 18 19 Attributes: 20 config_options (DeepgramClientOptions): An optional configuration object specifying client options. 21 22 Raises: 23 DeepgramApiKeyError: If the API key is missing or invalid. 24 25 Methods: 26 read: (Preferred) Returns an Threaded AnalyzeClient instance for interacting with Deepgram's read transcription services. 27 asyncread: Returns an (Async) AnalyzeClient instance for interacting with Deepgram's read transcription services. 28 """ 29 30 _logger: verboselogs.VerboseLogger 31 _config: DeepgramClientOptions 32 33 def __init__(self, config: DeepgramClientOptions): 34 self._logger = verboselogs.VerboseLogger(__name__) 35 self._logger.addHandler(logging.StreamHandler()) 36 self._logger.setLevel(config.verbose) 37 self._config = config 38 39 @property 40 def analyze(self): 41 """ 42 Returns an AnalyzeClient instance for interacting with Deepgram's read services. 43 """ 44 return self.Version(self._config, "analyze") 45 46 @property 47 def asyncanalyze(self): 48 """ 49 Returns an AsyncAnalyzeClient instance for interacting with Deepgram's read services. 50 """ 51 return self.Version(self._config, "asyncanalyze") 52 53 # INTERNAL CLASSES 54 class Version: 55 """ 56 Represents a version of the Deepgram API. 57 """ 58 59 _logger: verboselogs.VerboseLogger 60 _config: DeepgramClientOptions 61 _parent: str 62 63 def __init__(self, config, parent: str): 64 self._logger = verboselogs.VerboseLogger(__name__) 65 self._logger.addHandler(logging.StreamHandler()) 66 self._logger.setLevel(config.verbose) 67 self._config = config 68 self._parent = parent 69 70 # FUTURE VERSIONING: 71 # When v2 or v1.1beta1 or etc. This allows easy access to the latest version of the API. 72 # @property 73 # def latest(self): 74 # match self._parent: 75 # case "analyze": 76 # return AnalyzeClient(self._config) 77 # case _: 78 # raise DeepgramModuleError("Invalid parent") 79 80 def v(self, version: str = ""): 81 """ 82 Returns a specific version of the Deepgram API. 83 """ 84 self._logger.debug("Version.v ENTER") 85 self._logger.info("version: %s", version) 86 if len(version) == 0: 87 self._logger.error("version is empty") 88 self._logger.debug("Version.v LEAVE") 89 raise DeepgramModuleError("Invalid module version") 90 91 parent = "" 92 file_name = "" 93 class_name = "" 94 match self._parent: 95 case "analyze": 96 parent = "analyze" 97 file_name = "client" 98 class_name = "AnalyzeClient" 99 case "asyncanalyze": 100 parent = "analyze" 101 file_name = "async_client" 102 class_name = "AsyncAnalyzeClient" 103 case _: 104 self._logger.error("parent unknown: %s", self._parent) 105 self._logger.debug("Version.v LEAVE") 106 raise DeepgramModuleError("Invalid parent type") 107 108 # create class path 109 path = f"deepgram.clients.{parent}.v{version}.{file_name}" 110 self._logger.info("path: %s", path) 111 self._logger.info("class_name: %s", class_name) 112 113 # import class 114 mod = import_module(path) 115 if mod is None: 116 self._logger.error("module path is None") 117 self._logger.debug("Version.v LEAVE") 118 raise DeepgramModuleError("Unable to find package") 119 120 my_class = getattr(mod, class_name) 121 if my_class is None: 122 self._logger.error("my_class is None") 123 self._logger.debug("Version.v LEAVE") 124 raise DeepgramModuleError("Unable to find class") 125 126 # instantiate class 127 my_class = my_class(self._config) 128 self._logger.notice("Version.v succeeded") 129 self._logger.debug("Version.v LEAVE") 130 return my_class
class
ReadRouter:
14class ReadRouter: 15 """ 16 Represents a client for interacting with the Deepgram API. 17 18 This class provides a client for making requests to the Deepgram API with various configuration options. 19 20 Attributes: 21 config_options (DeepgramClientOptions): An optional configuration object specifying client options. 22 23 Raises: 24 DeepgramApiKeyError: If the API key is missing or invalid. 25 26 Methods: 27 read: (Preferred) Returns an Threaded AnalyzeClient instance for interacting with Deepgram's read transcription services. 28 asyncread: Returns an (Async) AnalyzeClient instance for interacting with Deepgram's read transcription services. 29 """ 30 31 _logger: verboselogs.VerboseLogger 32 _config: DeepgramClientOptions 33 34 def __init__(self, config: DeepgramClientOptions): 35 self._logger = verboselogs.VerboseLogger(__name__) 36 self._logger.addHandler(logging.StreamHandler()) 37 self._logger.setLevel(config.verbose) 38 self._config = config 39 40 @property 41 def analyze(self): 42 """ 43 Returns an AnalyzeClient instance for interacting with Deepgram's read services. 44 """ 45 return self.Version(self._config, "analyze") 46 47 @property 48 def asyncanalyze(self): 49 """ 50 Returns an AsyncAnalyzeClient instance for interacting with Deepgram's read services. 51 """ 52 return self.Version(self._config, "asyncanalyze") 53 54 # INTERNAL CLASSES 55 class Version: 56 """ 57 Represents a version of the Deepgram API. 58 """ 59 60 _logger: verboselogs.VerboseLogger 61 _config: DeepgramClientOptions 62 _parent: str 63 64 def __init__(self, config, parent: str): 65 self._logger = verboselogs.VerboseLogger(__name__) 66 self._logger.addHandler(logging.StreamHandler()) 67 self._logger.setLevel(config.verbose) 68 self._config = config 69 self._parent = parent 70 71 # FUTURE VERSIONING: 72 # When v2 or v1.1beta1 or etc. This allows easy access to the latest version of the API. 73 # @property 74 # def latest(self): 75 # match self._parent: 76 # case "analyze": 77 # return AnalyzeClient(self._config) 78 # case _: 79 # raise DeepgramModuleError("Invalid parent") 80 81 def v(self, version: str = ""): 82 """ 83 Returns a specific version of the Deepgram API. 84 """ 85 self._logger.debug("Version.v ENTER") 86 self._logger.info("version: %s", version) 87 if len(version) == 0: 88 self._logger.error("version is empty") 89 self._logger.debug("Version.v LEAVE") 90 raise DeepgramModuleError("Invalid module version") 91 92 parent = "" 93 file_name = "" 94 class_name = "" 95 match self._parent: 96 case "analyze": 97 parent = "analyze" 98 file_name = "client" 99 class_name = "AnalyzeClient" 100 case "asyncanalyze": 101 parent = "analyze" 102 file_name = "async_client" 103 class_name = "AsyncAnalyzeClient" 104 case _: 105 self._logger.error("parent unknown: %s", self._parent) 106 self._logger.debug("Version.v LEAVE") 107 raise DeepgramModuleError("Invalid parent type") 108 109 # create class path 110 path = f"deepgram.clients.{parent}.v{version}.{file_name}" 111 self._logger.info("path: %s", path) 112 self._logger.info("class_name: %s", class_name) 113 114 # import class 115 mod = import_module(path) 116 if mod is None: 117 self._logger.error("module path is None") 118 self._logger.debug("Version.v LEAVE") 119 raise DeepgramModuleError("Unable to find package") 120 121 my_class = getattr(mod, class_name) 122 if my_class is None: 123 self._logger.error("my_class is None") 124 self._logger.debug("Version.v LEAVE") 125 raise DeepgramModuleError("Unable to find class") 126 127 # instantiate class 128 my_class = my_class(self._config) 129 self._logger.notice("Version.v succeeded") 130 self._logger.debug("Version.v LEAVE") 131 return my_class
Represents a client for interacting with the Deepgram API.
This class provides a client for making requests to the Deepgram API with various configuration options.
Attributes: config_options (DeepgramClientOptions): An optional configuration object specifying client options.
Raises: DeepgramApiKeyError: If the API key is missing or invalid.
Methods: read: (Preferred) Returns an Threaded AnalyzeClient instance for interacting with Deepgram's read transcription services. asyncread: Returns an (Async) AnalyzeClient instance for interacting with Deepgram's read transcription services.
ReadRouter(config: deepgram.options.DeepgramClientOptions)
class
ReadRouter.Version:
55 class Version: 56 """ 57 Represents a version of the Deepgram API. 58 """ 59 60 _logger: verboselogs.VerboseLogger 61 _config: DeepgramClientOptions 62 _parent: str 63 64 def __init__(self, config, parent: str): 65 self._logger = verboselogs.VerboseLogger(__name__) 66 self._logger.addHandler(logging.StreamHandler()) 67 self._logger.setLevel(config.verbose) 68 self._config = config 69 self._parent = parent 70 71 # FUTURE VERSIONING: 72 # When v2 or v1.1beta1 or etc. This allows easy access to the latest version of the API. 73 # @property 74 # def latest(self): 75 # match self._parent: 76 # case "analyze": 77 # return AnalyzeClient(self._config) 78 # case _: 79 # raise DeepgramModuleError("Invalid parent") 80 81 def v(self, version: str = ""): 82 """ 83 Returns a specific version of the Deepgram API. 84 """ 85 self._logger.debug("Version.v ENTER") 86 self._logger.info("version: %s", version) 87 if len(version) == 0: 88 self._logger.error("version is empty") 89 self._logger.debug("Version.v LEAVE") 90 raise DeepgramModuleError("Invalid module version") 91 92 parent = "" 93 file_name = "" 94 class_name = "" 95 match self._parent: 96 case "analyze": 97 parent = "analyze" 98 file_name = "client" 99 class_name = "AnalyzeClient" 100 case "asyncanalyze": 101 parent = "analyze" 102 file_name = "async_client" 103 class_name = "AsyncAnalyzeClient" 104 case _: 105 self._logger.error("parent unknown: %s", self._parent) 106 self._logger.debug("Version.v LEAVE") 107 raise DeepgramModuleError("Invalid parent type") 108 109 # create class path 110 path = f"deepgram.clients.{parent}.v{version}.{file_name}" 111 self._logger.info("path: %s", path) 112 self._logger.info("class_name: %s", class_name) 113 114 # import class 115 mod = import_module(path) 116 if mod is None: 117 self._logger.error("module path is None") 118 self._logger.debug("Version.v LEAVE") 119 raise DeepgramModuleError("Unable to find package") 120 121 my_class = getattr(mod, class_name) 122 if my_class is None: 123 self._logger.error("my_class is None") 124 self._logger.debug("Version.v LEAVE") 125 raise DeepgramModuleError("Unable to find class") 126 127 # instantiate class 128 my_class = my_class(self._config) 129 self._logger.notice("Version.v succeeded") 130 self._logger.debug("Version.v LEAVE") 131 return my_class
Represents a version of the Deepgram API.
def
v(self, version: str = ''):
81 def v(self, version: str = ""): 82 """ 83 Returns a specific version of the Deepgram API. 84 """ 85 self._logger.debug("Version.v ENTER") 86 self._logger.info("version: %s", version) 87 if len(version) == 0: 88 self._logger.error("version is empty") 89 self._logger.debug("Version.v LEAVE") 90 raise DeepgramModuleError("Invalid module version") 91 92 parent = "" 93 file_name = "" 94 class_name = "" 95 match self._parent: 96 case "analyze": 97 parent = "analyze" 98 file_name = "client" 99 class_name = "AnalyzeClient" 100 case "asyncanalyze": 101 parent = "analyze" 102 file_name = "async_client" 103 class_name = "AsyncAnalyzeClient" 104 case _: 105 self._logger.error("parent unknown: %s", self._parent) 106 self._logger.debug("Version.v LEAVE") 107 raise DeepgramModuleError("Invalid parent type") 108 109 # create class path 110 path = f"deepgram.clients.{parent}.v{version}.{file_name}" 111 self._logger.info("path: %s", path) 112 self._logger.info("class_name: %s", class_name) 113 114 # import class 115 mod = import_module(path) 116 if mod is None: 117 self._logger.error("module path is None") 118 self._logger.debug("Version.v LEAVE") 119 raise DeepgramModuleError("Unable to find package") 120 121 my_class = getattr(mod, class_name) 122 if my_class is None: 123 self._logger.error("my_class is None") 124 self._logger.debug("Version.v LEAVE") 125 raise DeepgramModuleError("Unable to find class") 126 127 # instantiate class 128 my_class = my_class(self._config) 129 self._logger.notice("Version.v succeeded") 130 self._logger.debug("Version.v LEAVE") 131 return my_class
Returns a specific version of the Deepgram API.