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/_qs.py ADDED
@@ -0,0 +1,150 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any, List, Tuple, Union, Mapping, TypeVar
4
+ from urllib.parse import parse_qs, urlencode
5
+ from typing_extensions import Literal, get_args
6
+
7
+ from ._types import NOT_GIVEN, NotGiven, NotGivenOr
8
+ from ._utils import flatten
9
+
10
+ _T = TypeVar("_T")
11
+
12
+
13
+ ArrayFormat = Literal["comma", "repeat", "indices", "brackets"]
14
+ NestedFormat = Literal["dots", "brackets"]
15
+
16
+ PrimitiveData = Union[str, int, float, bool, None]
17
+ # this should be Data = Union[PrimitiveData, "List[Data]", "Tuple[Data]", "Mapping[str, Data]"]
18
+ # https://github.com/microsoft/pyright/issues/3555
19
+ Data = Union[PrimitiveData, List[Any], Tuple[Any], "Mapping[str, Any]"]
20
+ Params = Mapping[str, Data]
21
+
22
+
23
+ class Querystring:
24
+ array_format: ArrayFormat
25
+ nested_format: NestedFormat
26
+
27
+ def __init__(
28
+ self,
29
+ *,
30
+ array_format: ArrayFormat = "repeat",
31
+ nested_format: NestedFormat = "brackets",
32
+ ) -> None:
33
+ self.array_format = array_format
34
+ self.nested_format = nested_format
35
+
36
+ def parse(self, query: str) -> Mapping[str, object]:
37
+ # Note: custom format syntax is not supported yet
38
+ return parse_qs(query)
39
+
40
+ def stringify(
41
+ self,
42
+ params: Params,
43
+ *,
44
+ array_format: NotGivenOr[ArrayFormat] = NOT_GIVEN,
45
+ nested_format: NotGivenOr[NestedFormat] = NOT_GIVEN,
46
+ ) -> str:
47
+ return urlencode(
48
+ self.stringify_items(
49
+ params,
50
+ array_format=array_format,
51
+ nested_format=nested_format,
52
+ )
53
+ )
54
+
55
+ def stringify_items(
56
+ self,
57
+ params: Params,
58
+ *,
59
+ array_format: NotGivenOr[ArrayFormat] = NOT_GIVEN,
60
+ nested_format: NotGivenOr[NestedFormat] = NOT_GIVEN,
61
+ ) -> list[tuple[str, str]]:
62
+ opts = Options(
63
+ qs=self,
64
+ array_format=array_format,
65
+ nested_format=nested_format,
66
+ )
67
+ return flatten([self._stringify_item(key, value, opts) for key, value in params.items()])
68
+
69
+ def _stringify_item(
70
+ self,
71
+ key: str,
72
+ value: Data,
73
+ opts: Options,
74
+ ) -> list[tuple[str, str]]:
75
+ if isinstance(value, Mapping):
76
+ items: list[tuple[str, str]] = []
77
+ nested_format = opts.nested_format
78
+ for subkey, subvalue in value.items():
79
+ items.extend(
80
+ self._stringify_item(
81
+ # TODO: error if unknown format
82
+ f"{key}.{subkey}" if nested_format == "dots" else f"{key}[{subkey}]",
83
+ subvalue,
84
+ opts,
85
+ )
86
+ )
87
+ return items
88
+
89
+ if isinstance(value, (list, tuple)):
90
+ array_format = opts.array_format
91
+ if array_format == "comma":
92
+ return [
93
+ (
94
+ key,
95
+ ",".join(self._primitive_value_to_str(item) for item in value if item is not None),
96
+ ),
97
+ ]
98
+ elif array_format == "repeat":
99
+ items = []
100
+ for item in value:
101
+ items.extend(self._stringify_item(key, item, opts))
102
+ return items
103
+ elif array_format == "indices":
104
+ raise NotImplementedError("The array indices format is not supported yet")
105
+ elif array_format == "brackets":
106
+ items = []
107
+ key = key + "[]"
108
+ for item in value:
109
+ items.extend(self._stringify_item(key, item, opts))
110
+ return items
111
+ else:
112
+ raise NotImplementedError(
113
+ f"Unknown array_format value: {array_format}, choose from {', '.join(get_args(ArrayFormat))}"
114
+ )
115
+
116
+ serialised = self._primitive_value_to_str(value)
117
+ if not serialised:
118
+ return []
119
+ return [(key, serialised)]
120
+
121
+ def _primitive_value_to_str(self, value: PrimitiveData) -> str:
122
+ # copied from httpx
123
+ if value is True:
124
+ return "true"
125
+ elif value is False:
126
+ return "false"
127
+ elif value is None:
128
+ return ""
129
+ return str(value)
130
+
131
+
132
+ _qs = Querystring()
133
+ parse = _qs.parse
134
+ stringify = _qs.stringify
135
+ stringify_items = _qs.stringify_items
136
+
137
+
138
+ class Options:
139
+ array_format: ArrayFormat
140
+ nested_format: NestedFormat
141
+
142
+ def __init__(
143
+ self,
144
+ qs: Querystring = _qs,
145
+ *,
146
+ array_format: NotGivenOr[ArrayFormat] = NOT_GIVEN,
147
+ nested_format: NotGivenOr[NestedFormat] = NOT_GIVEN,
148
+ ) -> None:
149
+ self.array_format = qs.array_format if isinstance(array_format, NotGiven) else array_format
150
+ self.nested_format = qs.nested_format if isinstance(nested_format, NotGiven) else nested_format
isaacus/_resource.py ADDED
@@ -0,0 +1,43 @@
1
+ # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ from __future__ import annotations
4
+
5
+ import time
6
+ from typing import TYPE_CHECKING
7
+
8
+ import anyio
9
+
10
+ if TYPE_CHECKING:
11
+ from ._client import Isaacus, AsyncIsaacus
12
+
13
+
14
+ class SyncAPIResource:
15
+ _client: Isaacus
16
+
17
+ def __init__(self, client: Isaacus) -> None:
18
+ self._client = client
19
+ self._get = client.get
20
+ self._post = client.post
21
+ self._patch = client.patch
22
+ self._put = client.put
23
+ self._delete = client.delete
24
+ self._get_api_list = client.get_api_list
25
+
26
+ def _sleep(self, seconds: float) -> None:
27
+ time.sleep(seconds)
28
+
29
+
30
+ class AsyncAPIResource:
31
+ _client: AsyncIsaacus
32
+
33
+ def __init__(self, client: AsyncIsaacus) -> None:
34
+ self._client = client
35
+ self._get = client.get
36
+ self._post = client.post
37
+ self._patch = client.patch
38
+ self._put = client.put
39
+ self._delete = client.delete
40
+ self._get_api_list = client.get_api_list
41
+
42
+ async def _sleep(self, seconds: float) -> None:
43
+ await anyio.sleep(seconds)