Skip to content

Commit bd398fc

Browse files
Merge pull request #182 from dvonthenen/fill-out-all-examples
Implement All Examples for API Calls
2 parents 2ad31c9 + bc8cc35 commit bd398fc

File tree

27 files changed

+1093
-371
lines changed

27 files changed

+1093
-371
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ To quickly get started with examples for prerecorded and streaming, run the file
7474
Run the following command to install `pytest` and `pytest-cov` as dev dependencies.
7575

7676
```
77-
pip install -r requirements-dev.txt
77+
pip install -r requirements.txt
7878
```
7979

8080
## Run All Tests

deepgram/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
# entry point for the deepgram python sdk
99
from .client import DeepgramClient
1010
from .options import DeepgramClientOptions
11-
from .errors import DeepgramError, DeepgramApiError, DeepgramUnknownApiError, DeepgramUnknownError
11+
from .errors import DeepgramError, DeepgramApiError, DeepgramUnknownApiError
1212

1313
# live
1414
from .clients.live.enums import LiveTranscriptionEvents
1515
from .clients.live.client import LiveClient, LiveOptions
1616

1717
# prerecorded
18-
from .clients.prerecorded.client import PreRecordedClient, PrerecordedOptions
18+
from .clients.prerecorded.client import PreRecordedClient, PrerecordedOptions, PrerecordedSource, FileSource, UrlSource
1919

2020
# manage
2121
from .clients.manage.client import ManageClient, ProjectOptions, KeyOptions, ScopeOptions, InviteOptions, UsageRequestOptions, UsageSummaryOptions, UsageFieldsOptions

deepgram/clients/abstract_client.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import json
77
from typing import Dict, Any, Optional
88

9-
from ..errors import DeepgramApiError, DeepgramUnknownApiError, DeepgramUnknownError
9+
from ..errors import DeepgramApiError, DeepgramUnknownApiError
1010

1111
class AbstractRestfulClient:
1212
"""
@@ -27,9 +27,7 @@ class AbstractRestfulClient:
2727
Exceptions:
2828
DeepgramApiError: Raised for known API errors.
2929
DeepgramUnknownApiError: Raised for unknown API errors.
30-
DeepgramUnknownError: Raised for unexpected errors not specific to the API.
3130
"""
32-
3331
def __init__(self, url: Dict[str, str], headers: Optional[Dict[str, Any]]):
3432
self.url = url
3533
self.client = httpx.AsyncClient()
@@ -60,19 +58,18 @@ async def _handle_request(self, method, url, **kwargs):
6058
with httpx.Client() as client:
6159
response = client.request(method, url, **kwargs)
6260
response.raise_for_status()
63-
return response.json()
61+
return response.text
6462
except httpx._exceptions.HTTPError as e:
6563
if isinstance(e, httpx.HTTPStatusError):
6664
status_code = e.response.status_code or 500
67-
if is_json(e.response.text):
65+
try:
6866
json_object = json.loads(e.response.text)
69-
raise DeepgramApiError(json_object.get(
70-
'err_msg'), status_code, json.dumps(json_object)) from e
71-
else:
72-
raise DeepgramUnknownApiError(
73-
e.response.text, status_code) from e
67+
raise DeepgramApiError(json_object.get('message'), status_code, json.dumps(json_object)) from e
68+
except json.decoder.JSONDecodeError:
69+
raise DeepgramUnknownApiError(e.response.text, status_code) from e
70+
except ValueError as e:
71+
raise DeepgramUnknownApiError(e.response.text, status_code) from e
7472
else:
7573
raise
7674
except Exception as e:
77-
raise DeepgramUnknownError(
78-
"An unknown error occurred during the request.", e) from e
75+
raise

deepgram/clients/listen.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
from .live.client import LiveClient # FUTURE VERSIONINING:, LiveClientV1
77
from typing import Dict, Any, Optional
88

9-
109
class ListenClient:
1110
def __init__(self, url: str, api_key: str, headers: Optional[Dict[str, Any]]):
1211
self.url = url
1312
self.api_key = api_key
1413
self.headers = headers
15-
14+
1615
@property
1716
def prerecorded(self):
1817
return PreRecordedClient(self.url, self.headers)

deepgram/clients/manage/v1_client.py

Lines changed: 74 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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 .v1_response import Project, ProjectsResponse, Message, ProjectOptionsV1, KeysResponse, Key, KeyOptionsV1, CreateKeyResponse, MembersResponse, ScopesResponse, ScopeOptionsV1, InvitesResponse, InviteOptionsV1, UsageRequestsResponse, UsageRequestOptionsV1, UsageRequest, UsageSummaryOptionsV1, UsageSummaryResponse, UsageFieldsResponse, UsageFieldsOptionsV1, BalancesResponse, Balance
5+
from .v1_response import Project, ProjectsResponse, Message, ProjectOptionsV1, KeysResponse, KeyResponse, KeyOptionsV1, Key, MembersResponse, ScopesResponse, ScopeOptionsV1, InvitesResponse, InviteOptionsV1, UsageRequestsResponse, UsageRequestOptionsV1, UsageRequest, UsageSummaryOptionsV1, UsageSummaryResponse, UsageFieldsResponse, UsageFieldsOptionsV1, BalancesResponse, Balance
66

77
from ..abstract_client import AbstractRestfulClient
88

@@ -29,99 +29,127 @@ class ManageClientV1(AbstractRestfulClient):
2929
Raises:
3030
DeepgramApiError: Raised for known API errors.
3131
DeepgramUnknownApiError: Raised for unknown API errors.
32-
DeepgramUnknownError: Raised for unexpected errors not specific to the API.
3332
Exception: For any other unexpected exceptions.
3433
"""
3534
def __init__(self, url, headers):
3635
self.url = url
3736
self.headers = headers
3837
self.endpoint = "v1/projects"
3938
super().__init__(url, headers)
40-
41-
async def get_projects(self) -> ProjectsResponse:
39+
40+
# projects
41+
async def list_projects(self):
42+
return self.get_projects()
43+
async def get_projects(self):
4244
url = f"{self.url}/{self.endpoint}"
43-
return await self.get(url)
45+
return ProjectsResponse.from_json(await self.get(url))
4446

45-
async def get_project(self, project_id: str) -> Project:
47+
async def get_project(self, project_id: str):
4648
url = f"{self.url}/{self.endpoint}/{project_id}"
47-
return await self.get(url)
49+
return Project.from_json(await self.get(url))
4850

49-
async def update_project(self, project_id: str, options: ProjectOptionsV1) -> Message:
51+
async def update_project_option(self, project_id: str, options: ProjectOptionsV1):
52+
url = f"{self.url}/{self.endpoint}/{project_id}"
53+
return Message.from_json(await self.patch(url, json=options))
54+
async def update_project(self, project_id: str, name=""):
5055
url = f"{self.url}/{self.endpoint}/{project_id}"
51-
return await self.patch(url, json=options)
56+
options: ProjectOptionsV1 = {
57+
"name": name,
58+
}
59+
return Message.from_json(await self.patch(url, json=options))
5260

5361
async def delete_project(self, project_id: str) -> None:
5462
url = f"{self.url}/{self.endpoint}/{project_id}"
55-
return await self.delete(url)
63+
return Message.from_json(await self.delete(url))
5664

57-
async def get_project_keys(self, project_id: str) -> KeysResponse:
65+
# keys
66+
async def list_keys(self, project_id: str):
67+
return self.get_keys(project_id)
68+
async def get_keys(self, project_id: str):
5869
url = f"{self.url}/{self.endpoint}/{project_id}/keys"
59-
return await self.get(url)
70+
result = await self.get(url)
71+
return KeysResponse.from_json(result)
6072

61-
async def get_project_key(self, project_id: str, key_id: str) -> Key:
73+
async def get_key(self, project_id: str, key_id: str):
6274
url = f"{self.url}/{self.endpoint}/{project_id}/keys/{key_id}"
63-
return await self.get(url)
75+
return KeyResponse.from_json(await self.get(url))
6476

65-
async def create_project_key(self, project_id: str, options: KeyOptionsV1) -> CreateKeyResponse:
77+
async def create_key(self, project_id: str, options: KeyOptionsV1):
6678
url = f"{self.url}/{self.endpoint}/{project_id}/keys"
67-
return await self.post(url, json=options)
79+
return Key.from_json(await self.post(url, json=options))
6880

69-
async def delete_project_key(self, project_id: str, key_id: str) -> None:
81+
async def delete_key(self, project_id: str, key_id: str) -> None:
7082
url = f"{self.url}/{self.endpoint}/{project_id}/keys/{key_id}"
71-
return await self.delete(url)
83+
return Message.from_json(await self.delete(url))
7284

73-
async def get_project_members(self, project_id: str) -> MembersResponse:
85+
# members
86+
async def get_members(self, project_id: str):
7487
url = f"{self.url}/{self.endpoint}/{project_id}/members"
75-
return await self.get(url)
88+
return MembersResponse.from_json(await self.get(url))
7689

77-
async def remove_project_member(self, project_id: str, member_id: str) -> None:
90+
async def remove_member(self, project_id: str, member_id: str) -> None:
7891
url = f"{self.url}/{self.endpoint}/{project_id}/members/{member_id}"
79-
return await self.delete(url)
92+
return Message.from_json(await self.delete(url))
8093

81-
async def get_project_member_scopes(self, project_id: str, member_id: str) -> ScopesResponse:
94+
# scopes
95+
async def get_member_scopes(self, project_id: str, member_id: str):
8296
url = f"{self.url}/{self.endpoint}/{project_id}/members/{member_id}/scopes"
83-
return await self.get(url)
97+
return ScopesResponse.from_json(await self.get(url))
8498

85-
async def update_project_member_scope(self, project_id: str, member_id: str, options: ScopeOptionsV1) -> Message:
99+
async def update_member_scope(self, project_id: str, member_id: str, options: ScopeOptionsV1):
86100
url = f"{self.url}/{self.endpoint}/{project_id}/members/{member_id}/scopes"
87-
return await self.put(url, json=options)
101+
return Message.from_json(await self.put(url, json=options))
88102

89-
async def get_project_invites(self, project_id: str) -> InvitesResponse:
103+
# invites
104+
async def list_invites(self, project_id: str):
105+
return self.get_invites(project_id)
106+
async def get_invites(self, project_id: str):
90107
url = f"{self.url}/{self.endpoint}/{project_id}/invites"
91-
return await self.get(url)
108+
return InvitesResponse.from_json(await self.get(url))
92109

93-
async def send_project_invite(self, project_id: str, options: InviteOptionsV1) -> Message:
110+
async def send_invite_options(self, project_id: str, options: InviteOptionsV1):
111+
url = f"{self.url}/{self.endpoint}/{project_id}/invites"
112+
return Message.from_json(await self.post(url, json=options))
113+
async def send_invite(self, project_id: str, email: str, scope="member"):
94114
url = f"{self.url}/{self.endpoint}/{project_id}/invites"
95-
return await self.post(url, json=options)
115+
options: InviteOptionsV1 = {
116+
"email": email,
117+
"scope": scope,
118+
}
119+
return Message.from_json(await self.post(url, json=options))
96120

97-
async def delete_project_invite(self, project_id: str, email: str) -> Message:
121+
async def delete_invite(self, project_id: str, email: str):
98122
url = f"{self.url}/{self.endpoint}/{project_id}/invites/{email}"
99-
return await self.delete(url)
123+
return Message.from_json(await self.delete(url))
100124

101-
async def leave_project(self, project_id: str) -> Message:
125+
async def leave_project(self, project_id: str):
102126
url = f"{self.url}/{self.endpoint}/{project_id}/leave"
103-
return await self.delete(url)
127+
return Message.from_json(await self.delete(url))
104128

105-
async def get_project_usage_requests(self, project_id: str, options: UsageRequestOptionsV1) -> UsageRequestsResponse:
129+
# usage
130+
async def get_usage_requests(self, project_id: str, options: UsageRequestOptionsV1):
106131
url = f"{self.url}/{self.endpoint}/{project_id}/requests"
107-
return await self.get(url, options)
132+
return UsageRequestsResponse.from_json(await self.get(url, options))
108133

109-
async def get_project_usage_request(self, project_id: str, request_id: str) -> UsageRequest:
134+
async def get_usage_request(self, project_id: str, request_id: str):
110135
url = f"{self.url}/{self.endpoint}/{project_id}/requests/{request_id}"
111-
return await self.get(url)
136+
return UsageRequest.from_json(await self.get(url))
112137

113-
async def get_project_usage_summary(self, project_id: str, options: UsageSummaryOptionsV1) -> UsageSummaryResponse:
138+
async def get_usage_summary(self, project_id: str, options: UsageSummaryOptionsV1):
114139
url = f"{self.url}/{self.endpoint}/{project_id}/usage"
115-
return await self.get(url, options)
140+
return UsageSummaryResponse.from_json(await self.get(url, options))
116141

117-
async def get_project_usage_fields(self, project_id: str, options: UsageFieldsOptionsV1) -> UsageFieldsResponse:
142+
async def get_usage_fields(self, project_id: str, options: UsageFieldsOptionsV1):
118143
url = f"{self.url}/{self.endpoint}/{project_id}/usage/fields"
119-
return await self.get(url, options)
144+
return UsageFieldsResponse.from_json(await self.get(url, options))
120145

121-
async def get_project_balances(self, project_id: str) -> BalancesResponse:
146+
# balances
147+
async def list_balances(self, project_id: str):
148+
return self.get_balances(project_id)
149+
async def get_balances(self, project_id: str):
122150
url = f"{self.url}/{self.endpoint}/{project_id}/balances"
123-
return await self.get(url)
151+
return BalancesResponse.from_json(await self.get(url))
124152

125-
async def get_project_balance(self, project_id: str, balance_id: str) -> Balance:
153+
async def get_balance(self, project_id: str, balance_id: str):
126154
url = f"{self.url}/{self.endpoint}/{project_id}/balances/{balance_id}"
127-
return await self.get(url)
155+
return Balance.from_json(await self.get(url))

0 commit comments

Comments
 (0)