From 2f5ae35fef43f7d847a1d0d6fc589c5b22c52867 Mon Sep 17 00:00:00 2001 From: Jason Maldonis Date: Wed, 24 May 2023 12:32:14 -0500 Subject: [PATCH] added timeout parameter for prerecorded requests --- deepgram/_utils.py | 15 +++++++++++---- deepgram/transcription.py | 26 +++++++++++++++++--------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/deepgram/_utils.py b/deepgram/_utils.py index f3b68015..ca31ff42 100644 --- a/deepgram/_utils.py +++ b/deepgram/_utils.py @@ -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: @@ -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 = {} @@ -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 diff --git a/deepgram/transcription.py b/deepgram/transcription.py index e0930582..764d460b 100644 --- a/deepgram/transcription.py +++ b/deepgram/transcription.py @@ -32,7 +32,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 @@ -43,7 +43,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. """ @@ -59,7 +60,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 ) @@ -83,7 +85,7 @@ def __init__(self, options: Options, self.transcription_options = transcription_options def __call__( - self, source: TranscriptionSource + self, source: TranscriptionSource, timeout: float = None ) -> PrerecordedTranscriptionResponse: """ @@ -95,6 +97,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. """ @@ -111,7 +114,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 ) @@ -331,7 +335,9 @@ def __init__(self, options: Options) -> None: async def prerecorded( self, source: TranscriptionSource, - options: PrerecordedOptions = None, **kwargs + options: PrerecordedOptions = None, + timeout: float = None, + **kwargs ) -> PrerecordedTranscriptionResponse: """Retrieves a transcription for an already-existing audio file, local or web-hosted.""" @@ -340,12 +346,14 @@ async def prerecorded( full_options = cast(PrerecordedOptions, {**options, **kwargs}) return await PrerecordedTranscription( self.options, full_options - )(source) + )(source, timeout=timeout) def sync_prerecorded( self, source: TranscriptionSource, - options: PrerecordedOptions = None, **kwargs + options: PrerecordedOptions = None, + timeout: float = None, + **kwargs ) -> PrerecordedTranscriptionResponse: """Retrieves a transcription for an already-existing audio file, local or web-hosted.""" @@ -354,7 +362,7 @@ def sync_prerecorded( full_options = cast(PrerecordedOptions, {**options, **kwargs}) return SyncPrerecordedTranscription( self.options, full_options - )(source) + )(source, timeout=timeout) async def live(