Skip to content

Commit 433e6f2

Browse files
Merge pull request #473 from dvonthenen/fix-manaage-null-response
Fix `get_usage_request` when Response Not Available
2 parents eb4ed92 + 382069e commit 433e6f2

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

deepgram/clients/manage/v1/async_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
import logging
66
from typing import Dict, Union, Optional
7+
import json
78

89
import httpx
910

1011
from ....utils import verboselogs
1112
from ....options import DeepgramClientOptions
12-
from ...common import AbstractAsyncRestClient
13+
from ...common import AbstractAsyncRestClient, DeepgramError
1314

1415
from .response import (
1516
Message,
@@ -1000,6 +1001,14 @@ async def get_usage_request(
10001001
url, timeout=timeout, addons=addons, headers=headers, **kwargs
10011002
)
10021003
self._logger.info("result: %s", result)
1004+
1005+
# convert str to JSON to check response field
1006+
json_result = json.loads(result)
1007+
if json_result.get("response") is None:
1008+
raise DeepgramError(
1009+
"Response is not available yet. Please try again later."
1010+
)
1011+
10031012
res = UsageRequest.from_json(result)
10041013
self._logger.verbose("result: %s", res)
10051014
self._logger.notice("get_usage_request succeeded")

deepgram/clients/manage/v1/client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
import logging
66
from typing import Dict, Union, Optional
7+
import json
78

89
import httpx
910

1011
from ....utils import verboselogs
1112
from ....options import DeepgramClientOptions
12-
from ...common import AbstractSyncRestClient
13+
from ...common import AbstractSyncRestClient, DeepgramError
1314

1415
from .response import (
1516
Message,
@@ -1003,6 +1004,14 @@ def get_usage_request(
10031004
url, timeout=timeout, addons=addons, headers=headers, **kwargs
10041005
)
10051006
self._logger.info("json: %s", result)
1007+
1008+
# convert str to JSON to check response field
1009+
json_result = json.loads(result)
1010+
if json_result.get("response") is None:
1011+
raise DeepgramError(
1012+
"Response is not available yet. Please try again later."
1013+
)
1014+
10061015
res = UsageRequest.from_json(result)
10071016
self._logger.verbose("result: %s", res)
10081017
self._logger.notice("get_usage_request succeeded")
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright 2024 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+
import os
6+
import sys
7+
from dotenv import load_dotenv
8+
import logging
9+
from deepgram.utils import verboselogs
10+
11+
import httpx
12+
13+
from deepgram import (
14+
DeepgramClient,
15+
DeepgramClientOptions,
16+
PrerecordedOptions,
17+
FileSource,
18+
)
19+
20+
load_dotenv()
21+
22+
AUDIO_FILE = "sample.mp3"
23+
24+
25+
def main():
26+
try:
27+
# Create a Deepgram client using the API key
28+
# config: DeepgramClientOptions = DeepgramClientOptions(verbose=verboselogs.SPAM)
29+
config: DeepgramClientOptions = DeepgramClientOptions()
30+
deepgram: DeepgramClient = DeepgramClient("", config)
31+
32+
# get projects
33+
projectResp = deepgram.manage.v("1").get_projects()
34+
if projectResp is None:
35+
print(f"ListProjects failed.")
36+
sys.exit(1)
37+
38+
myId = None
39+
myName = None
40+
for project in projectResp.projects:
41+
myId = project.project_id
42+
myName = project.name
43+
print(f"ListProjects() - ID: {myId}, Name: {myName}")
44+
break
45+
46+
with open(AUDIO_FILE, "rb") as file:
47+
buffer_data = file.read()
48+
49+
payload: FileSource = {
50+
"buffer": buffer_data,
51+
}
52+
53+
options: PrerecordedOptions = PrerecordedOptions(
54+
callback="http://example.com",
55+
model="nova-2",
56+
smart_format=True,
57+
utterances=True,
58+
punctuate=True,
59+
diarize=True,
60+
)
61+
62+
response = deepgram.listen.rest.v("1").transcribe_file(
63+
payload, options, timeout=httpx.Timeout(300.0, connect=10.0)
64+
)
65+
request_id = (
66+
response.request_id
67+
) # without callback: response.metadata.request_id
68+
print(f"request_id: {request_id}")
69+
70+
# get request
71+
getResp = deepgram.manage.v("1").get_usage_request(myId, request_id)
72+
if getResp is None:
73+
print("No request found")
74+
else:
75+
print(f"GetUsageRequest() - getResp: {getResp}")
76+
print("\n\n\n")
77+
78+
except Exception as e:
79+
print(f"Exception: {e}")
80+
81+
82+
if __name__ == "__main__":
83+
main()

0 commit comments

Comments
 (0)