@@ -38,9 +38,11 @@ def __init__(
3838 self .verbose = verbose
3939 self .api_key = api_key
4040 self ._update_headers (headers = headers )
41+
4142 if len (url ) == 0 :
4243 url = "api.deepgram.com"
4344 self .url = self ._get_url (url )
45+
4446 if options is None :
4547 options = dict ()
4648 self .options = options
@@ -70,18 +72,93 @@ def _update_headers(self, headers: Optional[Dict[str, str]] = None):
7072class ClientOptionsFromEnv (DeepgramClientOptions ):
7173 def __init__ (
7274 self ,
75+ api_key : str = "" ,
76+ url : str = "" ,
7377 verbose : int = logging .WARNING ,
7478 headers : Dict [str , str ] = None ,
7579 options : Dict [str , str ] = None ,
7680 ):
77- apiKey = os .getenv ("DEEPGRAM_API_KEY" , None )
78- if apiKey is None :
79- raise DeepgramApiKeyError ("Deepgram API KEY is not set" )
81+ verboselogs .install ()
82+ self .logger = logging .getLogger (__name__ )
83+ self .logger .addHandler (logging .StreamHandler ())
84+ self .logger .setLevel (logging .WARNING ) # temporary set for setup
8085
81- url = os .getenv ("DEEPGRAM_URL" , None )
82- if url is None :
83- url = "api.deepgram.com"
86+ if api_key == "" :
87+ api_key = os .getenv ("DEEPGRAM_API_KEY" , None )
88+ if api_key is None :
89+ self .logger .critical ("Deepgram API KEY is not set" )
90+ raise DeepgramApiKeyError ("Deepgram API KEY is not set" )
91+
92+ if url == "" :
93+ url = os .getenv ("DEEPGRAM_HOST" , "api.deepgram.com" )
94+ self .logger .notice (f"Deepgram host is set to { url } " )
95+
96+ if verbose == logging .WARNING :
97+ verbose = os .getenv ("DEEPGRAM_LOGGING" , logging .WARNING )
98+ if type (verbose ) != int :
99+ match verbose :
100+ case "NOTSET" :
101+ self .logger .notice ("Logging level is set to NOTSET" )
102+ verbose = logging .NOTSET
103+ case "SPAM" :
104+ self .logger .notice ("Logging level is set to SPAM" )
105+ verbose = logging .SPAM
106+ case "DEBUG" :
107+ self .logger .notice ("Logging level is set to DEBUG" )
108+ verbose = logging .DEBUG
109+ case "VERBOSE" :
110+ self .logger .notice ("Logging level is set to VERBOSE" )
111+ verbose = logging .VERBOSE
112+ case "NOTICE" :
113+ self .logger .notice ("Logging level is set to NOTICE" )
114+ verbose = logging .NOTICE
115+ case "WARNING" :
116+ self .logger .notice ("Logging level is set to WARNING" )
117+ verbose = logging .WARNING
118+ case "SUCCESS" :
119+ self .logger .notice ("Logging level is set to SUCCESS" )
120+ verbose = logging .SUCCESS
121+ case "ERROR" :
122+ self .logger .notice ("Logging level is set to ERROR" )
123+ verbose = logging .ERROR
124+ case "CRITICAL" :
125+ self .logger .notice ("Logging level is set to CRITICAL" )
126+ verbose = logging .CRITICAL
127+ case _:
128+ self .logger .notice ("Logging level is set to WARNING" )
129+ verbose = logging .WARNING
130+ self .logger .notice (f"Logging level is set to { verbose } " )
131+
132+ if headers is None :
133+ headers = dict ()
134+ for x in range (0 , 20 ):
135+ header = os .getenv (f"DEEPGRAM_HEADER_{ x } " , None )
136+ if header is not None :
137+ headers [header ] = os .getenv (f"DEEPGRAM_HEADER_VALUE_{ x } " , None )
138+ self .logger .debug (
139+ f"Deepgram header { header } is set with value { headers [header ]} "
140+ )
141+ else :
142+ break
143+ if len (headers ) == 0 :
144+ self .logger .notice ("Deepgram headers are not set" )
145+ headers = None
146+
147+ if options is None :
148+ options = dict ()
149+ for x in range (0 , 20 ):
150+ param = os .getenv (f"DEEPGRAM_PARAM_{ x } " , None )
151+ if param is not None :
152+ options [param ] = os .getenv (f"DEEPGRAM_PARAM_VALUE_{ x } " , None )
153+ self .logger .debug (
154+ f"Deepgram option { param } is set with value { options [param ]} "
155+ )
156+ else :
157+ break
158+ if len (options ) == 0 :
159+ self .logger .notice ("Deepgram options are not set" )
160+ options = None
84161
85162 super ().__init__ (
86- api_key = apiKey , url = url , verbose = verbose , headers = headers , options = options
163+ api_key = api_key , url = url , verbose = verbose , headers = headers , options = options
87164 )
0 commit comments