Skip to content

Commit 3fbf383

Browse files
Merge pull request #199 from dvonthenen/sync-prerecorded-function
Separate Options from Responses, Favor Using Option Classes
2 parents de64413 + e36bb85 commit 3fbf383

File tree

22 files changed

+252
-202
lines changed

22 files changed

+252
-202
lines changed

deepgram/client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import logging, verboselogs
88
import os
99

10+
from .clients.live.client import LiveOptions
11+
from .clients.prerecorded.client import PrerecordedOptions
1012
from .clients.listen import ListenClient, PreRecordedClient
1113
from .clients.manage.client import ManageClient
1214
from .clients.onprem.client import OnPremClient
@@ -34,14 +36,18 @@ class DeepgramClient:
3436
onprem: Returns an OnPremClient instance for interacting with Deepgram's on-premises API.
3537
"""
3638

37-
def __init__(self, api_key: str, config: Optional[DeepgramClientOptions] = None):
39+
def __init__(
40+
self, api_key: str = "", config: Optional[DeepgramClientOptions] = None
41+
):
3842
verboselogs.install()
3943
self.logger = logging.getLogger(__name__)
4044
self.logger.addHandler(logging.StreamHandler())
4145

4246
if not api_key:
4347
# Default to `None` for on-prem instances where an API key is not required
4448
api_key = os.getenv("DEEPGRAM_API_KEY", None)
49+
if not api_key:
50+
self.logger.warning("WARNING: API key is missing")
4551

4652
self.api_key = api_key
4753
if config is None: # Use default configuration

deepgram/clients/live/helpers.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@
33
# SPDX-License-Identifier: MIT
44

55
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode
6+
from typing import Dict
67

8+
from .errors import DeepgramError
79

8-
def append_query_params(url, params=""):
10+
11+
def append_query_params(url, params):
912
parsed_url = urlparse(url)
1013
query_params = parse_qs(parsed_url.query)
1114

12-
if params:
13-
for key, value in params.items():
14-
if isinstance(value, bool):
15-
value = str(value).lower()
16-
if isinstance(value, list):
17-
for item in value:
18-
query_params[key] = query_params.get(key, []) + [str(item)]
19-
else:
20-
query_params[key] = [str(value)]
15+
for key, value in params.items():
16+
if value is None:
17+
continue
18+
if isinstance(value, bool):
19+
value = str(value).lower()
20+
if isinstance(value, list):
21+
for item in value:
22+
query_params[key] = query_params.get(key, []) + [str(item)]
23+
else:
24+
query_params[key] = [str(value)]
2125

2226
updated_query_string = urlencode(query_params, doseq=True)
2327
updated_url = parsed_url._replace(query=updated_query_string).geturl()

deepgram/clients/live/v1/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ def start(self, options: LiveOptions = None):
6060
self.logger.info("options: %s", options)
6161

6262
self.options = options
63+
if isinstance(options, LiveOptions):
64+
self.logger.info("LiveOptions switching class -> json")
65+
self.options = self.options.to_dict()
6366

6467
if self._socket is not None:
6568
self.logger.error("socket is already initialized")

deepgram/clients/live/v1/legacy_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ async def __call__(self, options: LiveOptions = None):
5555
self.logger.info("options: %s", options)
5656

5757
self.options = options
58+
if isinstance(options, LiveOptions):
59+
self.logger.info("LiveOptions switching class -> json")
60+
self.options = self.options.to_dict()
61+
5862
url_with_params = append_query_params(self.websocket_url, self.options)
5963
try:
6064
self._socket = await _socket_connect(url_with_params, self.config.headers)

deepgram/clients/live/v1/options.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,36 @@
22
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
33
# SPDX-License-Identifier: MIT
44

5-
from typing import List, TypedDict
5+
from dataclasses import dataclass
6+
from dataclasses_json import dataclass_json
7+
from typing import List, Optional
68

79

8-
class LiveOptions(TypedDict, total=False):
9-
callback: str
10-
channels: int
11-
diarize: bool
12-
encoding: str
13-
endpointing: int
14-
interim_results: bool
15-
keywords: str
16-
language: str
17-
model: str
18-
multichannel: bool
19-
numerals: bool
20-
punctuate: bool
21-
profanity_filter: bool
22-
redact: bool
23-
replace: str
24-
sample_rate: int
25-
search: str
26-
smart_format: bool
27-
tag: list
28-
tier: str
29-
version: str
10+
@dataclass_json
11+
@dataclass
12+
class LiveOptions:
13+
callback: Optional[str] = None
14+
channels: Optional[int] = None
15+
diarize: Optional[bool] = None
16+
encoding: Optional[str] = None
17+
endpointing: Optional[str] = None
18+
interim_results: Optional[bool] = None
19+
keywords: Optional[str] = None
20+
language: Optional[str] = None
21+
model: Optional[str] = None
22+
multichannel: Optional[bool] = None
23+
numerals: Optional[bool] = None
24+
punctuate: Optional[bool] = None
25+
profanity_filter: Optional[bool] = None
26+
redact: Optional[bool] = None
27+
replace: Optional[str] = None
28+
sample_rate: Optional[int] = None
29+
search: Optional[str] = None
30+
smart_format: Optional[bool] = None
31+
tag: Optional[list] = None
32+
tier: Optional[str] = None
33+
version: Optional[str] = None
34+
35+
def __getitem__(self, key):
36+
_dict = self.to_dict()
37+
return _dict[key]

deepgram/clients/manage/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SPDX-License-Identifier: MIT
44

55
from .v1.client import ManageClient as ManageClientLatest
6-
from .v1.response import (
6+
from .v1.options import (
77
ProjectOptions as ProjectOptionsLatest,
88
KeyOptions as KeyOptionsLatest,
99
ScopeOptions as ScopeOptionsLatest,

deepgram/clients/manage/v1/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
BalancesResponse,
2525
Balance,
2626
)
27-
from .response import (
27+
from .options import (
2828
ProjectOptions,
2929
KeyOptions,
3030
ScopeOptions,
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Copyright 2023 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+
5+
from dataclasses import dataclass
6+
from dataclasses_json import dataclass_json
7+
from datetime import datetime
8+
from typing import TypedDict, List, Optional
9+
10+
# Input
11+
12+
13+
@dataclass_json
14+
@dataclass
15+
class ProjectOptions:
16+
name: Optional[str] = ""
17+
18+
def __getitem__(self, key):
19+
_dict = self.to_dict()
20+
return _dict[key]
21+
22+
23+
@dataclass_json
24+
@dataclass
25+
class KeyOptions:
26+
comment: Optional[str] = ""
27+
time_to_live_in_seconds: Optional[int] = 0
28+
expiration_date: Optional[str] = ""
29+
scopes: Optional[List[str]] = None
30+
tags: Optional[List[str]] = None
31+
32+
def __getitem__(self, key):
33+
_dict = self.to_dict()
34+
if _dict["scopes"] is not None:
35+
_dict["scopes"] = [str(scopes) for scopes in _dict["scopes"]]
36+
if _dict["tags"] is not None:
37+
_dict["tags"] = [str(tags) for tags in _dict["tags"]]
38+
return _dict[key]
39+
40+
41+
@dataclass_json
42+
@dataclass
43+
class ScopeOptions:
44+
scope: Optional[str] = ""
45+
46+
def __getitem__(self, key):
47+
_dict = self.to_dict()
48+
return _dict[key]
49+
50+
51+
@dataclass_json
52+
@dataclass
53+
class InviteOptions:
54+
email: Optional[str] = ""
55+
scope: Optional[str] = ""
56+
57+
def __getitem__(self, key):
58+
_dict = self.to_dict()
59+
return _dict[key]
60+
61+
62+
@dataclass_json
63+
@dataclass
64+
class UsageRequestOptions:
65+
start: Optional[str] = ""
66+
end: Optional[str] = ""
67+
limit: Optional[int] = 0
68+
status: Optional[str] = ""
69+
70+
def __getitem__(self, key):
71+
_dict = self.to_dict()
72+
return _dict[key]
73+
74+
75+
@dataclass_json
76+
@dataclass
77+
class UsageSummaryOptions:
78+
start: Optional[str] = ""
79+
end: Optional[str] = ""
80+
accessor: Optional[str] = ""
81+
tag: Optional[str] = ""
82+
method: Optional[str] = ""
83+
model: Optional[str] = ""
84+
multichannel: Optional[bool] = False
85+
interim_results: Optional[bool] = False
86+
punctuate: Optional[bool] = False
87+
ner: Optional[bool] = False
88+
utterances: Optional[bool] = False
89+
replace: Optional[bool] = False
90+
profanity_filter: Optional[bool] = False
91+
keywords: Optional[bool] = False
92+
detect_topics: Optional[bool] = False
93+
diarize: Optional[bool] = False
94+
search: Optional[bool] = False
95+
redact: Optional[bool] = False
96+
alternatives: Optional[bool] = False
97+
numerals: Optional[bool] = False
98+
smart_format: Optional[bool] = False
99+
100+
def __getitem__(self, key):
101+
_dict = self.to_dict()
102+
return _dict[key]
103+
104+
105+
@dataclass_json
106+
@dataclass
107+
class UsageFieldsOptions:
108+
start: Optional[str] = ""
109+
end: Optional[str] = ""
110+
111+
def __getitem__(self, key):
112+
_dict = self.to_dict()
113+
if _dict["details"] is not None:
114+
_dict["details"] = Details.from_dict(_dict["details"])
115+
return _dict[key]

0 commit comments

Comments
 (0)