33# SPDX-License-Identifier: MIT
44
55import logging , verboselogs
6- from typing import Dict
6+ from typing import Dict , Optional
77import re
88
99
@@ -31,15 +31,7 @@ def __init__(
3131 ):
3232 self .verbose = verbose
3333 self .api_key = api_key
34- if headers is None :
35- self .headers = {
36- "Accept" : "application/json" ,
37- "Authorization" : f"Token { self .api_key } " ,
38- }
39- else :
40- self .headers .update (
41- {"Accept" : "application/json" , "Authorization" : f"Token { self .api_key } " }
42- )
34+ self ._update_headers (headers = headers )
4335 if len (url ) == 0 :
4436 url = "api.deepgram.com"
4537 self .url = self ._get_url (url )
@@ -49,11 +41,21 @@ def __init__(
4941
5042 def set_apikey (self , api_key : str ):
5143 self .api_key = api_key
52- self .headers .update (
53- {"Accept" : "application/json" , "Authorization" : f"Token { self .api_key } " }
54- )
44+ self ._update_headers ()
5545
5646 def _get_url (self , url ):
5747 if not re .match (r"^https?://" , url , re .IGNORECASE ):
5848 url = "https://" + url
5949 return url .strip ("/" )
50+
51+ def _update_headers (self , headers : Optional [Dict [str , str ]] = None ):
52+ if not hasattr (self , "headers" ) or self .headers is None :
53+ self .headers = {}
54+ self .headers ["Accept" ] = "application/json"
55+ if self .api_key :
56+ self .headers ["Authorization" ] = f"Token { self .api_key } "
57+ elif "Authorization" in self .headers :
58+ del self .headers ["Authorization" ]
59+ # Overwrite / add any headers that were passed in
60+ if headers :
61+ self .headers .update (headers )
0 commit comments