Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions deepgram/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,24 @@ def elem_decomposer(key: str, value: Any) -> List[Tuple[str, str]]:
async def _request(
path: str, options: Options,
method: str = 'GET', payload: Payload = None,
headers: Optional[Mapping[str, str]] = None
headers: Optional[Mapping[str, str]] = None,
timeout: float = None,
) -> Any:
if headers is None:
headers = {}
destination = cast(str, options.get('api_url', DEFAULT_ENDPOINT)) + path
updated_headers = _prepare_headers(options, headers)

if timeout is None:
timeout = aiohttp.client.DEFAULT_TIMEOUT
else:
timeout = aiohttp.ClientTimeout(total=timeout)

async def attempt():
try:
async with aiohttp.request(
method, destination, data=_normalize_payload(payload),
headers=updated_headers, raise_for_status=True
headers=updated_headers, raise_for_status=True, timeout=timeout
) as resp:
content = (await resp.text()).strip()
if not content:
Expand Down Expand Up @@ -122,7 +128,8 @@ async def attempt():
def _sync_request(
path: str, options: Options,
method: str = 'GET', payload: Payload = None,
headers: Optional[Mapping[str, str]] = None
headers: Optional[Mapping[str, str]] = None,
timeout: float = None
) -> Any:
if headers is None:
headers = {}
Expand All @@ -136,7 +143,7 @@ def attempt():
headers=updated_headers,
method=method)
try:
with urllib.request.urlopen(req) as resp:
with urllib.request.urlopen(req, timeout=timeout) as resp:
content = resp.read().strip()
if not content:
return None
Expand Down
28 changes: 19 additions & 9 deletions deepgram/transcription.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, options: Options,
self.transcription_options = transcription_options

async def __call__(
self, source: TranscriptionSource
self, source: TranscriptionSource, timeout: float = None
) -> PrerecordedTranscriptionResponse:
"""
The __call__ function is a special method that allows the class to be called
Expand All @@ -44,7 +44,8 @@ async def __call__(
prerecorded_transcription = PrerecordedTranscription(...)

:param source:TranscriptionSource: Used to Pass in the audio file.
:return: A `prerecordedtranscriptionresponse` object, which contains the transcription results.
:param timeout:float: (optional) The request timeout (if not set, defaults to `aiohttp`'s default timeout)
:return: A `PrerecordedTranscriptionResponse` object, which contains the transcription results.

"""

Expand All @@ -60,7 +61,8 @@ async def __call__(
return await _request(
f'{self._root}{_make_query_string(self.transcription_options)}',
self.options, method='POST', payload=payload,
headers={'Content-Type': content_type}
headers={'Content-Type': content_type},
timeout=timeout
)


Expand All @@ -86,7 +88,7 @@ def __init__(self, options: Options,
self.transcription_options = transcription_options

def __call__(
self, source: TranscriptionSource
self, source: TranscriptionSource, timeout: float = None
) -> PrerecordedTranscriptionResponse:

"""
Expand All @@ -98,6 +100,7 @@ def __call__(
sync_prerecorded_transcription = SyncPrerecordedTranscription(...)

:param source:TranscriptionSource: Used to Pass in the audio file.
:param timeout:float: (optional) The request timeout, excluding the upload time of the audio file.
:return: A `prerecordedtranscriptionresponse` object, which contains the transcription results.

"""
Expand All @@ -114,7 +117,8 @@ def __call__(
return _sync_request(
f'{self._root}{_make_query_string(self.transcription_options)}',
self.options, method='POST', payload=payload,
headers={'Content-Type': content_type}
headers={'Content-Type': content_type},
timeout=timeout
)


Expand Down Expand Up @@ -336,7 +340,10 @@ def __init__(self, options: Options) -> None:

async def prerecorded(
self, source: TranscriptionSource,
options: PrerecordedOptions = None, endpoint = "/listen", **kwargs
options: PrerecordedOptions = None,
endpoint = "/listen",
timeout: float = None,
**kwargs
) -> PrerecordedTranscriptionResponse:
"""Retrieves a transcription for an already-existing audio file,
local or web-hosted."""
Expand All @@ -345,12 +352,15 @@ async def prerecorded(
full_options = cast(PrerecordedOptions, {**options, **kwargs})
return await PrerecordedTranscription(
self.options, full_options, endpoint
)(source)
)(source, timeout=timeout)


def sync_prerecorded(
self, source: TranscriptionSource,
options: PrerecordedOptions = None, endpoint = "/listen", **kwargs
options: PrerecordedOptions = None,
endpoint = "/listen",
timeout: float = None,
**kwargs
) -> PrerecordedTranscriptionResponse:
"""Retrieves a transcription for an already-existing audio file,
local or web-hosted."""
Expand All @@ -359,7 +369,7 @@ def sync_prerecorded(
full_options = cast(PrerecordedOptions, {**options, **kwargs})
return SyncPrerecordedTranscription(
self.options, full_options, endpoint
)(source)
)(source, timeout=timeout)


async def live(
Expand Down