Skip to content

Commit

Permalink
tweak: small typing improvement (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpbeing authored Sep 11, 2024
1 parent 1b15f10 commit 069a602
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/ansys/simai/core/data/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@
"""


APIResponse = Union[Response, Dict[str, Any], List[Dict[str, Any]]]
JSON = Union[Dict[str, Any], List[Dict[str, Any]]]
APIResponse = Union[Response, JSON]


MonitorCallback = Callable[[int], None]
"""Callback used to monitor the download or upload of a file.
Expand Down
18 changes: 17 additions & 1 deletion src/ansys/simai/core/utils/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@

import logging
from json.decoder import JSONDecodeError
from typing import Literal, overload

import requests

from ansys.simai.core.data.types import APIResponse
from ansys.simai.core.data.types import JSON, APIResponse
from ansys.simai.core.errors import ApiClientError, NotFoundError

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -76,6 +77,21 @@ def handle_http_errors(response: requests.Response) -> None:
raise ApiClientError(f"{response.status_code}: {response.reason}", response) from e


@overload
def handle_response(response: requests.Response, return_json: Literal[True]) -> JSON:
...


@overload
def handle_response(response: requests.Response, return_json: Literal[False]) -> requests.Response:
...


@overload
def handle_response(response: requests.Response, return_json: bool) -> APIResponse:
...


def handle_response(response: requests.Response, return_json: bool = True) -> APIResponse:
"""Handle HTTP errors and return the relevant data from the response.
Expand Down

0 comments on commit 069a602

Please sign in to comment.