Skip to content

Commit de64413

Browse files
authored
Merge pull request deepgram#198 from deepgram/optional-api-key
made the api key optional for on-prem
2 parents 0c47c3d + 0220adf commit de64413

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

deepgram/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Optional
66
from importlib import import_module
77
import logging, verboselogs
8+
import os
89

910
from .clients.listen import ListenClient, PreRecordedClient
1011
from .clients.manage.client import ManageClient
@@ -39,7 +40,8 @@ def __init__(self, api_key: str, config: Optional[DeepgramClientOptions] = None)
3940
self.logger.addHandler(logging.StreamHandler())
4041

4142
if not api_key:
42-
raise DeepgramApiKeyError("Deepgram API key is required")
43+
# Default to `None` for on-prem instances where an API key is not required
44+
api_key = os.getenv("DEEPGRAM_API_KEY", None)
4345

4446
self.api_key = api_key
4547
if config is None: # Use default configuration

deepgram/options.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SPDX-License-Identifier: MIT
44

55
import logging, verboselogs
6-
from typing import Dict
6+
from typing import Dict, Optional
77
import 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

Comments
 (0)