diff --git a/deepgram/clients/abstract_async_client.py b/deepgram/clients/abstract_async_client.py index ef55cb34..3655b311 100644 --- a/deepgram/clients/abstract_async_client.py +++ b/deepgram/clients/abstract_async_client.py @@ -5,6 +5,7 @@ import httpx import json +from .helpers import append_query_params from ..options import DeepgramClientOptions from .errors import DeepgramError, DeepgramApiError, DeepgramUnknownApiError from .helpers import append_query_params @@ -20,6 +21,8 @@ class AbstractAsyncRestClient: Args: url (Dict[str, str]): The base URL for the RESTful API, including any path segments. headers (Optional[Dict[str, Any]]): Optional HTTP headers to include in requests. + params (Optional[Dict[str, Any]]): Optional query parameters to include in requests. + timeout (Optional[httpx.Timeout]): Optional timeout configuration for requests. Exceptions: DeepgramApiError: Raised for known API errors. @@ -29,70 +32,80 @@ class AbstractAsyncRestClient: def __init__(self, config: DeepgramClientOptions): if config is None: raise DeepgramError("Config are required") - self.config = config - self.client = httpx.AsyncClient() - async def get(self, url: str, options=None, addons=None, **kwargs): + async def get(self, url: str, options=None, addons=None, timeout=None, **kwargs): return await self._handle_request( "GET", url, params=options, addons=addons, + timeout=timeout, headers=self.config.headers, **kwargs ) - async def post(self, url: str, options=None, addons=None, **kwargs): + async def post(self, url: str, options=None, addons=None, timeout=None, **kwargs): return await self._handle_request( "POST", url, params=options, addons=addons, + timeout=timeout, headers=self.config.headers, **kwargs ) - async def put(self, url: str, options=None, addons=None, **kwargs): + async def put(self, url: str, options=None, addons=None, timeout=None, **kwargs): return await self._handle_request( "PUT", url, params=options, addons=addons, + timeout=timeout, headers=self.config.headers, **kwargs ) - async def patch(self, url: str, options=None, addons=None, **kwargs): + async def patch(self, url: str, options=None, addons=None, timeout=None, **kwargs): return await self._handle_request( "PATCH", url, params=options, addons=addons, + timeout=timeout, headers=self.config.headers, **kwargs ) - async def delete(self, url: str, options=None, addons=None, **kwargs): + async def delete(self, url: str, options=None, addons=None, timeout=None, **kwargs): return await self._handle_request( "DELETE", url, params=options, addons=addons, + timeout=timeout, headers=self.config.headers, **kwargs ) - async def _handle_request(self, method, url, params, addons, headers, **kwargs): + async def _handle_request( + self, method, url, params, addons, timeout, headers, **kwargs + ): new_url = url if params is not None: new_url = append_query_params(new_url, params) if addons is not None: new_url = append_query_params(new_url, addons) + if timeout is None: + timeout = httpx.Timeout(30.0, connect=10.0) + try: - with httpx.Client() as client: - response = client.request(method, new_url, headers=headers, **kwargs) + async with httpx.AsyncClient(timeout=timeout) as client: + response = await client.request( + method, new_url, headers=headers, **kwargs + ) response.raise_for_status() return response.text except httpx._exceptions.HTTPError as e: diff --git a/deepgram/clients/abstract_sync_client.py b/deepgram/clients/abstract_sync_client.py index ef87603c..ff9fd119 100644 --- a/deepgram/clients/abstract_sync_client.py +++ b/deepgram/clients/abstract_sync_client.py @@ -31,7 +31,6 @@ class AbstractSyncRestClient: def __init__(self, config: DeepgramClientOptions): if config is None: raise DeepgramError("Config are required") - self.config = config def get(self, url: str, options=None, addons=None, timeout=None, **kwargs): @@ -97,7 +96,7 @@ def _handle_request(self, method, url, params, addons, headers, timeout, **kwarg new_url = append_query_params(new_url, addons) if timeout is None: - timeout = httpx.Timeout(10.0, connect=10.0) + timeout = httpx.Timeout(30.0, connect=10.0) try: with httpx.Client(timeout=timeout) as client: diff --git a/deepgram/clients/prerecorded/v1/async_client.py b/deepgram/clients/prerecorded/v1/async_client.py index 6cfbbfa0..bf0073da 100644 --- a/deepgram/clients/prerecorded/v1/async_client.py +++ b/deepgram/clients/prerecorded/v1/async_client.py @@ -2,6 +2,8 @@ # Use of this source code is governed by a MIT license that can be found in the LICENSE file. # SPDX-License-Identifier: MIT +import httpx +import logging, verboselogs import json import logging, verboselogs @@ -47,6 +49,7 @@ async def transcribe_url( source: UrlSource, options: PrerecordedOptions = None, addons: dict = None, + timeout: httpx.Timeout = None, endpoint: str = "v1/listen", ) -> PrerecordedResponse: self.logger.debug("PreRecordedClient.transcribe_url ENTER") @@ -54,7 +57,7 @@ async def transcribe_url( if options is not None and options.callback is not None: self.logger.debug("PreRecordedClient.transcribe_url LEAVE") return await self.transcribe_url_callback( - source, options["callback"], options, addons, endpoint + source, options["callback"], options, addons, timeout, endpoint ) url = f"{self.config.url}/{endpoint}" @@ -72,7 +75,9 @@ async def transcribe_url( options = json.loads(options.to_json()) self.logger.info("options: %s", options) self.logger.info("addons: %s", addons) - result = await self.post(url, options=options, addons=addons, json=body) + result = await self.post( + url, options=options, addons=addons, json=body, timeout=timeout + ) self.logger.info("json: %s", result) res = PrerecordedResponse.from_json(result) self.logger.verbose("result: %s", res) @@ -102,6 +107,7 @@ async def transcribe_url_callback( callback: str, options: PrerecordedOptions = None, addons: dict = None, + timeout: httpx.Timeout = None, endpoint: str = "v1/listen", ) -> AsyncPrerecordedResponse: self.logger.debug("PreRecordedClient.transcribe_url_callback ENTER") @@ -124,7 +130,9 @@ async def transcribe_url_callback( options = json.loads(options.to_json()) self.logger.info("options: %s", options) self.logger.info("addons: %s", addons) - result = await self.post(url, options=options, addons=addons, json=body) + result = await self.post( + url, options=options, addons=addons, json=body, timeout=timeout + ) self.logger.info("json: %s", result) res = AsyncPrerecordedResponse.from_json(result) self.logger.verbose("result: %s", res) @@ -152,6 +160,7 @@ async def transcribe_file( source: FileSource, options: PrerecordedOptions = None, addons: dict = None, + timeout: httpx.Timeout = None, endpoint: str = "v1/listen", ) -> PrerecordedResponse: self.logger.debug("PreRecordedClient.transcribe_file ENTER") @@ -159,7 +168,7 @@ async def transcribe_file( if options is not None and options.callback is not None: self.logger.debug("PreRecordedClient.transcribe_file LEAVE") return await self.transcribe_file_callback( - source, options["callback"], options, addons, endpoint + source, options["callback"], options, addons, timeout, endpoint ) url = f"{self.config.url}/{endpoint}" @@ -178,7 +187,9 @@ async def transcribe_file( options = json.loads(options.to_json()) self.logger.info("options: %s", options) self.logger.info("addons: %s", addons) - result = await self.post(url, options=options, addons=addons, content=body) + result = await self.post( + url, options=options, addons=addons, content=body, timeout=timeout + ) self.logger.info("json: %s", result) res = PrerecordedResponse.from_json(result) self.logger.verbose("result: %s", res) @@ -208,6 +219,7 @@ async def transcribe_file_callback( callback: str, options: PrerecordedOptions = None, addons: dict = None, + timeout: httpx.Timeout = None, endpoint: str = "v1/listen", ) -> AsyncPrerecordedResponse: self.logger.debug("PreRecordedClient.transcribe_file_callback ENTER") @@ -231,7 +243,9 @@ async def transcribe_file_callback( options = json.loads(options.to_json()) self.logger.info("options: %s", options) self.logger.info("addons: %s", addons) - result = await self.post(url, options=options, addons=addons, json=body) + result = await self.post( + url, options=options, addons=addons, json=body, timeout=timeout + ) self.logger.info("json: %s", result) res = AsyncPrerecordedResponse.from_json(result) self.logger.verbose("result: %s", res) diff --git a/deepgram/clients/prerecorded/v1/client.py b/deepgram/clients/prerecorded/v1/client.py index 7e96cd10..79c0d9bf 100644 --- a/deepgram/clients/prerecorded/v1/client.py +++ b/deepgram/clients/prerecorded/v1/client.py @@ -56,7 +56,7 @@ def transcribe_url( if options is not None and options.callback is not None: self.logger.debug("PreRecordedClient.transcribe_url LEAVE") return self.transcribe_url_callback( - source, options["callback"], options, addons, endpoint + source, options["callback"], options, addons, timeout, endpoint ) url = f"{self.config.url}/{endpoint}" @@ -167,7 +167,7 @@ def transcribe_file( if options is not None and options.callback is not None: self.logger.debug("PreRecordedClient.transcribe_file LEAVE") return self.transcribe_file_callback( - source, options["callback"], options, addons, endpoint + source, options["callback"], options, addons, timeout, endpoint ) url = f"{self.config.url}/{endpoint}" diff --git a/examples/prerecorded/file/main.py b/examples/prerecorded/file/main.py index 72cccd29..8e1d66d2 100644 --- a/examples/prerecorded/file/main.py +++ b/examples/prerecorded/file/main.py @@ -5,6 +5,7 @@ import os from dotenv import load_dotenv import logging, verboselogs +from datetime import datetime, timedelta from deepgram import ( DeepgramClientOptions, @@ -42,8 +43,15 @@ def main(): punctuate=True, diarize=True, ) + + before = datetime.now() response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options) + after = datetime.now() + print(response.to_json(indent=4)) + print("") + difference = after - before + print(f"time: {difference.seconds}") except Exception as e: print(f"Exception: {e}")