isaacus 0.1.0a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
isaacus/_exceptions.py ADDED
@@ -0,0 +1,108 @@
1
+ # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing_extensions import Literal
6
+
7
+ import httpx
8
+
9
+ __all__ = [
10
+ "BadRequestError",
11
+ "AuthenticationError",
12
+ "PermissionDeniedError",
13
+ "NotFoundError",
14
+ "ConflictError",
15
+ "UnprocessableEntityError",
16
+ "RateLimitError",
17
+ "InternalServerError",
18
+ ]
19
+
20
+
21
+ class IsaacusError(Exception):
22
+ pass
23
+
24
+
25
+ class APIError(IsaacusError):
26
+ message: str
27
+ request: httpx.Request
28
+
29
+ body: object | None
30
+ """The API response body.
31
+
32
+ If the API responded with a valid JSON structure then this property will be the
33
+ decoded result.
34
+
35
+ If it isn't a valid JSON structure then this will be the raw response.
36
+
37
+ If there was no response associated with this error then it will be `None`.
38
+ """
39
+
40
+ def __init__(self, message: str, request: httpx.Request, *, body: object | None) -> None: # noqa: ARG002
41
+ super().__init__(message)
42
+ self.request = request
43
+ self.message = message
44
+ self.body = body
45
+
46
+
47
+ class APIResponseValidationError(APIError):
48
+ response: httpx.Response
49
+ status_code: int
50
+
51
+ def __init__(self, response: httpx.Response, body: object | None, *, message: str | None = None) -> None:
52
+ super().__init__(message or "Data returned by API invalid for expected schema.", response.request, body=body)
53
+ self.response = response
54
+ self.status_code = response.status_code
55
+
56
+
57
+ class APIStatusError(APIError):
58
+ """Raised when an API response has a status code of 4xx or 5xx."""
59
+
60
+ response: httpx.Response
61
+ status_code: int
62
+
63
+ def __init__(self, message: str, *, response: httpx.Response, body: object | None) -> None:
64
+ super().__init__(message, response.request, body=body)
65
+ self.response = response
66
+ self.status_code = response.status_code
67
+
68
+
69
+ class APIConnectionError(APIError):
70
+ def __init__(self, *, message: str = "Connection error.", request: httpx.Request) -> None:
71
+ super().__init__(message, request, body=None)
72
+
73
+
74
+ class APITimeoutError(APIConnectionError):
75
+ def __init__(self, request: httpx.Request) -> None:
76
+ super().__init__(message="Request timed out.", request=request)
77
+
78
+
79
+ class BadRequestError(APIStatusError):
80
+ status_code: Literal[400] = 400 # pyright: ignore[reportIncompatibleVariableOverride]
81
+
82
+
83
+ class AuthenticationError(APIStatusError):
84
+ status_code: Literal[401] = 401 # pyright: ignore[reportIncompatibleVariableOverride]
85
+
86
+
87
+ class PermissionDeniedError(APIStatusError):
88
+ status_code: Literal[403] = 403 # pyright: ignore[reportIncompatibleVariableOverride]
89
+
90
+
91
+ class NotFoundError(APIStatusError):
92
+ status_code: Literal[404] = 404 # pyright: ignore[reportIncompatibleVariableOverride]
93
+
94
+
95
+ class ConflictError(APIStatusError):
96
+ status_code: Literal[409] = 409 # pyright: ignore[reportIncompatibleVariableOverride]
97
+
98
+
99
+ class UnprocessableEntityError(APIStatusError):
100
+ status_code: Literal[422] = 422 # pyright: ignore[reportIncompatibleVariableOverride]
101
+
102
+
103
+ class RateLimitError(APIStatusError):
104
+ status_code: Literal[429] = 429 # pyright: ignore[reportIncompatibleVariableOverride]
105
+
106
+
107
+ class InternalServerError(APIStatusError):
108
+ pass
isaacus/_files.py ADDED
@@ -0,0 +1,123 @@
1
+ from __future__ import annotations
2
+
3
+ import io
4
+ import os
5
+ import pathlib
6
+ from typing import overload
7
+ from typing_extensions import TypeGuard
8
+
9
+ import anyio
10
+
11
+ from ._types import (
12
+ FileTypes,
13
+ FileContent,
14
+ RequestFiles,
15
+ HttpxFileTypes,
16
+ Base64FileInput,
17
+ HttpxFileContent,
18
+ HttpxRequestFiles,
19
+ )
20
+ from ._utils import is_tuple_t, is_mapping_t, is_sequence_t
21
+
22
+
23
+ def is_base64_file_input(obj: object) -> TypeGuard[Base64FileInput]:
24
+ return isinstance(obj, io.IOBase) or isinstance(obj, os.PathLike)
25
+
26
+
27
+ def is_file_content(obj: object) -> TypeGuard[FileContent]:
28
+ return (
29
+ isinstance(obj, bytes) or isinstance(obj, tuple) or isinstance(obj, io.IOBase) or isinstance(obj, os.PathLike)
30
+ )
31
+
32
+
33
+ def assert_is_file_content(obj: object, *, key: str | None = None) -> None:
34
+ if not is_file_content(obj):
35
+ prefix = f"Expected entry at `{key}`" if key is not None else f"Expected file input `{obj!r}`"
36
+ raise RuntimeError(
37
+ f"{prefix} to be bytes, an io.IOBase instance, PathLike or a tuple but received {type(obj)} instead."
38
+ ) from None
39
+
40
+
41
+ @overload
42
+ def to_httpx_files(files: None) -> None: ...
43
+
44
+
45
+ @overload
46
+ def to_httpx_files(files: RequestFiles) -> HttpxRequestFiles: ...
47
+
48
+
49
+ def to_httpx_files(files: RequestFiles | None) -> HttpxRequestFiles | None:
50
+ if files is None:
51
+ return None
52
+
53
+ if is_mapping_t(files):
54
+ files = {key: _transform_file(file) for key, file in files.items()}
55
+ elif is_sequence_t(files):
56
+ files = [(key, _transform_file(file)) for key, file in files]
57
+ else:
58
+ raise TypeError(f"Unexpected file type input {type(files)}, expected mapping or sequence")
59
+
60
+ return files
61
+
62
+
63
+ def _transform_file(file: FileTypes) -> HttpxFileTypes:
64
+ if is_file_content(file):
65
+ if isinstance(file, os.PathLike):
66
+ path = pathlib.Path(file)
67
+ return (path.name, path.read_bytes())
68
+
69
+ return file
70
+
71
+ if is_tuple_t(file):
72
+ return (file[0], _read_file_content(file[1]), *file[2:])
73
+
74
+ raise TypeError(f"Expected file types input to be a FileContent type or to be a tuple")
75
+
76
+
77
+ def _read_file_content(file: FileContent) -> HttpxFileContent:
78
+ if isinstance(file, os.PathLike):
79
+ return pathlib.Path(file).read_bytes()
80
+ return file
81
+
82
+
83
+ @overload
84
+ async def async_to_httpx_files(files: None) -> None: ...
85
+
86
+
87
+ @overload
88
+ async def async_to_httpx_files(files: RequestFiles) -> HttpxRequestFiles: ...
89
+
90
+
91
+ async def async_to_httpx_files(files: RequestFiles | None) -> HttpxRequestFiles | None:
92
+ if files is None:
93
+ return None
94
+
95
+ if is_mapping_t(files):
96
+ files = {key: await _async_transform_file(file) for key, file in files.items()}
97
+ elif is_sequence_t(files):
98
+ files = [(key, await _async_transform_file(file)) for key, file in files]
99
+ else:
100
+ raise TypeError("Unexpected file type input {type(files)}, expected mapping or sequence")
101
+
102
+ return files
103
+
104
+
105
+ async def _async_transform_file(file: FileTypes) -> HttpxFileTypes:
106
+ if is_file_content(file):
107
+ if isinstance(file, os.PathLike):
108
+ path = anyio.Path(file)
109
+ return (path.name, await path.read_bytes())
110
+
111
+ return file
112
+
113
+ if is_tuple_t(file):
114
+ return (file[0], await _async_read_file_content(file[1]), *file[2:])
115
+
116
+ raise TypeError(f"Expected file types input to be a FileContent type or to be a tuple")
117
+
118
+
119
+ async def _async_read_file_content(file: FileContent) -> HttpxFileContent:
120
+ if isinstance(file, os.PathLike):
121
+ return await anyio.Path(file).read_bytes()
122
+
123
+ return file