|
| 1 | +# Copyright 2023 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 httpx |
| 6 | +import json |
| 7 | + |
| 8 | +from ..options import DeepgramClientOptions |
| 9 | +from .errors import DeepgramError, DeepgramApiError, DeepgramUnknownApiError |
| 10 | + |
| 11 | + |
| 12 | +class AbstractSyncRestClient: |
| 13 | + """ |
| 14 | + An abstract base class for a RESTful HTTP client. |
| 15 | +
|
| 16 | + This class provides common HTTP methods (GET, POST, PUT, PATCH, DELETE) for making asynchronous HTTP requests. |
| 17 | + It handles error responses and provides basic JSON parsing. |
| 18 | +
|
| 19 | + Args: |
| 20 | + url (Dict[str, str]): The base URL for the RESTful API, including any path segments. |
| 21 | + headers (Optional[Dict[str, Any]]): Optional HTTP headers to include in requests. |
| 22 | +
|
| 23 | + Attributes: |
| 24 | + url (Dict[str, str]): The base URL for the RESTful API. |
| 25 | + client (httpx.AsyncClient): An asynchronous HTTP client for making requests. |
| 26 | + headers (Optional[Dict[str, Any]]): Optional HTTP headers to include in requests. |
| 27 | +
|
| 28 | + Exceptions: |
| 29 | + DeepgramApiError: Raised for known API errors. |
| 30 | + DeepgramUnknownApiError: Raised for unknown API errors. |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__(self, config: DeepgramClientOptions): |
| 34 | + if config is None: |
| 35 | + raise DeepgramError("Config are required") |
| 36 | + |
| 37 | + self.config = config |
| 38 | + |
| 39 | + def get(self, url: str, options=None): |
| 40 | + return self._handle_request( |
| 41 | + "GET", url, params=options, headers=self.config.headers |
| 42 | + ) |
| 43 | + |
| 44 | + def post(self, url: str, options=None, **kwargs): |
| 45 | + return self._handle_request( |
| 46 | + "POST", url, params=options, headers=self.config.headers, **kwargs |
| 47 | + ) |
| 48 | + |
| 49 | + def put(self, url: str, options=None, **kwargs): |
| 50 | + return self._handle_request( |
| 51 | + "PUT", url, params=options, headers=self.config.headers, **kwargs |
| 52 | + ) |
| 53 | + |
| 54 | + def patch(self, url: str, options=None, **kwargs): |
| 55 | + return self._handle_request( |
| 56 | + "PATCH", url, params=options, headers=self.config.headers, **kwargs |
| 57 | + ) |
| 58 | + |
| 59 | + def delete(self, url: str): |
| 60 | + return self._handle_request("DELETE", url, headers=self.config.headers) |
| 61 | + |
| 62 | + def _handle_request(self, method, url, **kwargs): |
| 63 | + try: |
| 64 | + with httpx.Client() as client: |
| 65 | + response = client.request(method, url, **kwargs) |
| 66 | + response.raise_for_status() |
| 67 | + return response.text |
| 68 | + except httpx._exceptions.HTTPError as e: |
| 69 | + if isinstance(e, httpx.HTTPStatusError): |
| 70 | + status_code = e.response.status_code or 500 |
| 71 | + try: |
| 72 | + json_object = json.loads(e.response.text) |
| 73 | + raise DeepgramApiError( |
| 74 | + json_object.get("message"), status_code, json.dumps(json_object) |
| 75 | + ) from e |
| 76 | + except json.decoder.JSONDecodeError: |
| 77 | + raise DeepgramUnknownApiError(e.response.text, status_code) from e |
| 78 | + except ValueError as e: |
| 79 | + raise DeepgramUnknownApiError(e.response.text, status_code) from e |
| 80 | + else: |
| 81 | + raise |
| 82 | + except Exception as e: |
| 83 | + raise |
0 commit comments