compass_api_sdk 0.0.1__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.

Potentially problematic release.


This version of compass_api_sdk might be problematic. Click here for more details.

Files changed (137) hide show
  1. compass_api_sdk/__init__.py +17 -0
  2. compass_api_sdk/_hooks/__init__.py +5 -0
  3. compass_api_sdk/_hooks/registration.py +13 -0
  4. compass_api_sdk/_hooks/sdkhooks.py +76 -0
  5. compass_api_sdk/_hooks/types.py +106 -0
  6. compass_api_sdk/_version.py +15 -0
  7. compass_api_sdk/aave_v3.py +1903 -0
  8. compass_api_sdk/aerodrome_slipstream.py +1875 -0
  9. compass_api_sdk/basesdk.py +362 -0
  10. compass_api_sdk/errors/__init__.py +7 -0
  11. compass_api_sdk/errors/apierror.py +22 -0
  12. compass_api_sdk/errors/httpvalidationerror.py +21 -0
  13. compass_api_sdk/httpclient.py +136 -0
  14. compass_api_sdk/models/__init__.py +777 -0
  15. compass_api_sdk/models/aave_liquidity_changeop.py +108 -0
  16. compass_api_sdk/models/aave_token_priceop.py +98 -0
  17. compass_api_sdk/models/aave_user_position_per_tokenop.py +104 -0
  18. compass_api_sdk/models/aave_user_position_summaryop.py +35 -0
  19. compass_api_sdk/models/aaveborrowrequest.py +105 -0
  20. compass_api_sdk/models/aaveliquiditychangeresponse.py +26 -0
  21. compass_api_sdk/models/aaverepayrequest.py +105 -0
  22. compass_api_sdk/models/aavesupplyrequest.py +93 -0
  23. compass_api_sdk/models/aavetokenpriceresponse.py +15 -0
  24. compass_api_sdk/models/aaveuserpositionpertokenresponse.py +73 -0
  25. compass_api_sdk/models/aaveuserpositionsummaryresponse.py +50 -0
  26. compass_api_sdk/models/aavewithdrawrequest.py +58 -0
  27. compass_api_sdk/models/aerodrome_slipstream_liquidity_provision_positionsop.py +35 -0
  28. compass_api_sdk/models/aerodrome_slipstream_pool_priceop.py +173 -0
  29. compass_api_sdk/models/aerodromelppositionsresponse.py +21 -0
  30. compass_api_sdk/models/aerodromeposition.py +70 -0
  31. compass_api_sdk/models/aerodromeslipstreambuyexactlyrequest.py +92 -0
  32. compass_api_sdk/models/aerodromeslipstreamincreaseliquidityprovisionrequest.py +109 -0
  33. compass_api_sdk/models/aerodromeslipstreammintliquidityprovisionrequest.py +186 -0
  34. compass_api_sdk/models/aerodromeslipstreampoolpriceresponse.py +57 -0
  35. compass_api_sdk/models/aerodromeslipstreamsellexactlyrequest.py +96 -0
  36. compass_api_sdk/models/aerodromeslipstreamwithdrawliquidityprovisionrequest.py +50 -0
  37. compass_api_sdk/models/allowanceinforesponse.py +48 -0
  38. compass_api_sdk/models/balanceinforesponse.py +43 -0
  39. compass_api_sdk/models/chain.py +12 -0
  40. compass_api_sdk/models/chaininfo.py +53 -0
  41. compass_api_sdk/models/compass_api_backend_models_morpho_read_response_get_markets_asset.py +22 -0
  42. compass_api_sdk/models/compass_api_backend_models_morpho_read_response_get_vaults_asset.py +23 -0
  43. compass_api_sdk/models/ensnameinforesponse.py +24 -0
  44. compass_api_sdk/models/feeenum.py +16 -0
  45. compass_api_sdk/models/generic_allowanceop.py +121 -0
  46. compass_api_sdk/models/generic_balanceop.py +104 -0
  47. compass_api_sdk/models/generic_ensop.py +35 -0
  48. compass_api_sdk/models/generic_portfolioop.py +35 -0
  49. compass_api_sdk/models/generic_price_usdop.py +98 -0
  50. compass_api_sdk/models/generic_supported_tokensop.py +29 -0
  51. compass_api_sdk/models/generic_visualize_portfolioop.py +35 -0
  52. compass_api_sdk/models/image.py +15 -0
  53. compass_api_sdk/models/increaseallowancerequest.py +77 -0
  54. compass_api_sdk/models/interestratemode.py +14 -0
  55. compass_api_sdk/models/marketstate.py +35 -0
  56. compass_api_sdk/models/morpho_market_positionop.py +39 -0
  57. compass_api_sdk/models/morpho_marketsop.py +78 -0
  58. compass_api_sdk/models/morpho_vault_positionop.py +39 -0
  59. compass_api_sdk/models/morpho_vaultsop.py +72 -0
  60. compass_api_sdk/models/morphoborrowrequest.py +91 -0
  61. compass_api_sdk/models/morphocheckmarketpositionresponse.py +23 -0
  62. compass_api_sdk/models/morphocheckvaultpositionresponse.py +20 -0
  63. compass_api_sdk/models/morphodepositrequest.py +86 -0
  64. compass_api_sdk/models/morphogetmarketsresponse.py +17 -0
  65. compass_api_sdk/models/morphogetvaultsresponse.py +17 -0
  66. compass_api_sdk/models/morphomarket.py +80 -0
  67. compass_api_sdk/models/morphorepayrequest.py +79 -0
  68. compass_api_sdk/models/morphosetvaultallowancerequest.py +44 -0
  69. compass_api_sdk/models/morphosupplycollateralrequest.py +91 -0
  70. compass_api_sdk/models/morphovault.py +47 -0
  71. compass_api_sdk/models/morphowithdrawcollateralrequest.py +91 -0
  72. compass_api_sdk/models/morphowithdrawrequest.py +74 -0
  73. compass_api_sdk/models/portfolio.py +22 -0
  74. compass_api_sdk/models/priceresponse.py +15 -0
  75. compass_api_sdk/models/security.py +24 -0
  76. compass_api_sdk/models/token_addressop.py +98 -0
  77. compass_api_sdk/models/token_balanceop.py +49 -0
  78. compass_api_sdk/models/token_enum.py +57 -0
  79. compass_api_sdk/models/token_priceop.py +97 -0
  80. compass_api_sdk/models/tokenaddressresponse.py +15 -0
  81. compass_api_sdk/models/tokenbalance.py +49 -0
  82. compass_api_sdk/models/tokenbalanceresponse.py +44 -0
  83. compass_api_sdk/models/tokeninfo.py +17 -0
  84. compass_api_sdk/models/tokenpriceresponse.py +15 -0
  85. compass_api_sdk/models/tokentransferrequest.py +66 -0
  86. compass_api_sdk/models/transfererc20request.py +62 -0
  87. compass_api_sdk/models/transferethrequest.py +46 -0
  88. compass_api_sdk/models/uniswap_liquidity_provision_in_rangeop.py +34 -0
  89. compass_api_sdk/models/uniswap_liquidity_provision_positionsop.py +35 -0
  90. compass_api_sdk/models/uniswap_pool_priceop.py +193 -0
  91. compass_api_sdk/models/uniswap_quote_buy_exactlyop.py +209 -0
  92. compass_api_sdk/models/uniswap_quote_sell_exactlyop.py +209 -0
  93. compass_api_sdk/models/uniswapbuyexactlyrequest.py +104 -0
  94. compass_api_sdk/models/uniswapbuyquoteinforesponse.py +20 -0
  95. compass_api_sdk/models/uniswapcheckinrangeresponse.py +15 -0
  96. compass_api_sdk/models/uniswapincreaseliquidityprovisionrequest.py +99 -0
  97. compass_api_sdk/models/uniswaplppositionsinforesponse.py +24 -0
  98. compass_api_sdk/models/uniswapmintliquidityprovisionrequest.py +185 -0
  99. compass_api_sdk/models/uniswappoolpriceresponse.py +47 -0
  100. compass_api_sdk/models/uniswappositionssolidityresponse.py +46 -0
  101. compass_api_sdk/models/uniswapsellexactlyrequest.py +104 -0
  102. compass_api_sdk/models/uniswapsellquoteinforesponse.py +20 -0
  103. compass_api_sdk/models/uniswapwithdrawliquidityprovisionrequest.py +51 -0
  104. compass_api_sdk/models/unsignedtransaction.py +58 -0
  105. compass_api_sdk/models/unwrapwethrequest.py +41 -0
  106. compass_api_sdk/models/validationerror.py +26 -0
  107. compass_api_sdk/models/vaultstate.py +32 -0
  108. compass_api_sdk/models/weeklyapys.py +23 -0
  109. compass_api_sdk/models/wrapethrequest.py +41 -0
  110. compass_api_sdk/morpho.py +2577 -0
  111. compass_api_sdk/py.typed +1 -0
  112. compass_api_sdk/sdk.py +155 -0
  113. compass_api_sdk/sdkconfiguration.py +56 -0
  114. compass_api_sdk/token_sdk.py +861 -0
  115. compass_api_sdk/types/__init__.py +21 -0
  116. compass_api_sdk/types/basemodel.py +39 -0
  117. compass_api_sdk/uniswap_v3.py +2551 -0
  118. compass_api_sdk/universal.py +2625 -0
  119. compass_api_sdk/utils/__init__.py +100 -0
  120. compass_api_sdk/utils/annotations.py +55 -0
  121. compass_api_sdk/utils/datetimes.py +23 -0
  122. compass_api_sdk/utils/enums.py +74 -0
  123. compass_api_sdk/utils/eventstreaming.py +238 -0
  124. compass_api_sdk/utils/forms.py +202 -0
  125. compass_api_sdk/utils/headers.py +136 -0
  126. compass_api_sdk/utils/logger.py +22 -0
  127. compass_api_sdk/utils/metadata.py +118 -0
  128. compass_api_sdk/utils/queryparams.py +205 -0
  129. compass_api_sdk/utils/requestbodies.py +66 -0
  130. compass_api_sdk/utils/retries.py +217 -0
  131. compass_api_sdk/utils/security.py +174 -0
  132. compass_api_sdk/utils/serializers.py +248 -0
  133. compass_api_sdk/utils/url.py +155 -0
  134. compass_api_sdk/utils/values.py +137 -0
  135. compass_api_sdk-0.0.1.dist-info/METADATA +534 -0
  136. compass_api_sdk-0.0.1.dist-info/RECORD +137 -0
  137. compass_api_sdk-0.0.1.dist-info/WHEEL +4 -0
@@ -0,0 +1,217 @@
1
+ """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
2
+
3
+ import asyncio
4
+ import random
5
+ import time
6
+ from typing import List
7
+
8
+ import httpx
9
+
10
+
11
+ class BackoffStrategy:
12
+ initial_interval: int
13
+ max_interval: int
14
+ exponent: float
15
+ max_elapsed_time: int
16
+
17
+ def __init__(
18
+ self,
19
+ initial_interval: int,
20
+ max_interval: int,
21
+ exponent: float,
22
+ max_elapsed_time: int,
23
+ ):
24
+ self.initial_interval = initial_interval
25
+ self.max_interval = max_interval
26
+ self.exponent = exponent
27
+ self.max_elapsed_time = max_elapsed_time
28
+
29
+
30
+ class RetryConfig:
31
+ strategy: str
32
+ backoff: BackoffStrategy
33
+ retry_connection_errors: bool
34
+
35
+ def __init__(
36
+ self, strategy: str, backoff: BackoffStrategy, retry_connection_errors: bool
37
+ ):
38
+ self.strategy = strategy
39
+ self.backoff = backoff
40
+ self.retry_connection_errors = retry_connection_errors
41
+
42
+
43
+ class Retries:
44
+ config: RetryConfig
45
+ status_codes: List[str]
46
+
47
+ def __init__(self, config: RetryConfig, status_codes: List[str]):
48
+ self.config = config
49
+ self.status_codes = status_codes
50
+
51
+
52
+ class TemporaryError(Exception):
53
+ response: httpx.Response
54
+
55
+ def __init__(self, response: httpx.Response):
56
+ self.response = response
57
+
58
+
59
+ class PermanentError(Exception):
60
+ inner: Exception
61
+
62
+ def __init__(self, inner: Exception):
63
+ self.inner = inner
64
+
65
+
66
+ def retry(func, retries: Retries):
67
+ if retries.config.strategy == "backoff":
68
+
69
+ def do_request() -> httpx.Response:
70
+ res: httpx.Response
71
+ try:
72
+ res = func()
73
+
74
+ for code in retries.status_codes:
75
+ if "X" in code.upper():
76
+ code_range = int(code[0])
77
+
78
+ status_major = res.status_code / 100
79
+
80
+ if code_range <= status_major < code_range + 1:
81
+ raise TemporaryError(res)
82
+ else:
83
+ parsed_code = int(code)
84
+
85
+ if res.status_code == parsed_code:
86
+ raise TemporaryError(res)
87
+ except httpx.ConnectError as exception:
88
+ if retries.config.retry_connection_errors:
89
+ raise
90
+
91
+ raise PermanentError(exception) from exception
92
+ except httpx.TimeoutException as exception:
93
+ if retries.config.retry_connection_errors:
94
+ raise
95
+
96
+ raise PermanentError(exception) from exception
97
+ except TemporaryError:
98
+ raise
99
+ except Exception as exception:
100
+ raise PermanentError(exception) from exception
101
+
102
+ return res
103
+
104
+ return retry_with_backoff(
105
+ do_request,
106
+ retries.config.backoff.initial_interval,
107
+ retries.config.backoff.max_interval,
108
+ retries.config.backoff.exponent,
109
+ retries.config.backoff.max_elapsed_time,
110
+ )
111
+
112
+ return func()
113
+
114
+
115
+ async def retry_async(func, retries: Retries):
116
+ if retries.config.strategy == "backoff":
117
+
118
+ async def do_request() -> httpx.Response:
119
+ res: httpx.Response
120
+ try:
121
+ res = await func()
122
+
123
+ for code in retries.status_codes:
124
+ if "X" in code.upper():
125
+ code_range = int(code[0])
126
+
127
+ status_major = res.status_code / 100
128
+
129
+ if code_range <= status_major < code_range + 1:
130
+ raise TemporaryError(res)
131
+ else:
132
+ parsed_code = int(code)
133
+
134
+ if res.status_code == parsed_code:
135
+ raise TemporaryError(res)
136
+ except httpx.ConnectError as exception:
137
+ if retries.config.retry_connection_errors:
138
+ raise
139
+
140
+ raise PermanentError(exception) from exception
141
+ except httpx.TimeoutException as exception:
142
+ if retries.config.retry_connection_errors:
143
+ raise
144
+
145
+ raise PermanentError(exception) from exception
146
+ except TemporaryError:
147
+ raise
148
+ except Exception as exception:
149
+ raise PermanentError(exception) from exception
150
+
151
+ return res
152
+
153
+ return await retry_with_backoff_async(
154
+ do_request,
155
+ retries.config.backoff.initial_interval,
156
+ retries.config.backoff.max_interval,
157
+ retries.config.backoff.exponent,
158
+ retries.config.backoff.max_elapsed_time,
159
+ )
160
+
161
+ return await func()
162
+
163
+
164
+ def retry_with_backoff(
165
+ func,
166
+ initial_interval=500,
167
+ max_interval=60000,
168
+ exponent=1.5,
169
+ max_elapsed_time=3600000,
170
+ ):
171
+ start = round(time.time() * 1000)
172
+ retries = 0
173
+
174
+ while True:
175
+ try:
176
+ return func()
177
+ except PermanentError as exception:
178
+ raise exception.inner
179
+ except Exception as exception: # pylint: disable=broad-exception-caught
180
+ now = round(time.time() * 1000)
181
+ if now - start > max_elapsed_time:
182
+ if isinstance(exception, TemporaryError):
183
+ return exception.response
184
+
185
+ raise
186
+ sleep = (initial_interval / 1000) * exponent**retries + random.uniform(0, 1)
187
+ sleep = min(sleep, max_interval / 1000)
188
+ time.sleep(sleep)
189
+ retries += 1
190
+
191
+
192
+ async def retry_with_backoff_async(
193
+ func,
194
+ initial_interval=500,
195
+ max_interval=60000,
196
+ exponent=1.5,
197
+ max_elapsed_time=3600000,
198
+ ):
199
+ start = round(time.time() * 1000)
200
+ retries = 0
201
+
202
+ while True:
203
+ try:
204
+ return await func()
205
+ except PermanentError as exception:
206
+ raise exception.inner
207
+ except Exception as exception: # pylint: disable=broad-exception-caught
208
+ now = round(time.time() * 1000)
209
+ if now - start > max_elapsed_time:
210
+ if isinstance(exception, TemporaryError):
211
+ return exception.response
212
+
213
+ raise
214
+ sleep = (initial_interval / 1000) * exponent**retries + random.uniform(0, 1)
215
+ sleep = min(sleep, max_interval / 1000)
216
+ await asyncio.sleep(sleep)
217
+ retries += 1
@@ -0,0 +1,174 @@
1
+ """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
2
+
3
+ import base64
4
+ from typing import (
5
+ Any,
6
+ Dict,
7
+ List,
8
+ Tuple,
9
+ )
10
+ from pydantic import BaseModel
11
+ from pydantic.fields import FieldInfo
12
+
13
+ from .metadata import (
14
+ SecurityMetadata,
15
+ find_field_metadata,
16
+ )
17
+
18
+
19
+ def get_security(security: Any) -> Tuple[Dict[str, str], Dict[str, List[str]]]:
20
+ headers: Dict[str, str] = {}
21
+ query_params: Dict[str, List[str]] = {}
22
+
23
+ if security is None:
24
+ return headers, query_params
25
+
26
+ if not isinstance(security, BaseModel):
27
+ raise TypeError("security must be a pydantic model")
28
+
29
+ sec_fields: Dict[str, FieldInfo] = security.__class__.model_fields
30
+ for name in sec_fields:
31
+ sec_field = sec_fields[name]
32
+
33
+ value = getattr(security, name)
34
+ if value is None:
35
+ continue
36
+
37
+ metadata = find_field_metadata(sec_field, SecurityMetadata)
38
+ if metadata is None:
39
+ continue
40
+ if metadata.option:
41
+ _parse_security_option(headers, query_params, value)
42
+ return headers, query_params
43
+ if metadata.scheme:
44
+ # Special case for basic auth or custom auth which could be a flattened model
45
+ if metadata.sub_type in ["basic", "custom"] and not isinstance(
46
+ value, BaseModel
47
+ ):
48
+ _parse_security_scheme(headers, query_params, metadata, name, security)
49
+ else:
50
+ _parse_security_scheme(headers, query_params, metadata, name, value)
51
+
52
+ return headers, query_params
53
+
54
+
55
+ def _parse_security_option(
56
+ headers: Dict[str, str], query_params: Dict[str, List[str]], option: Any
57
+ ):
58
+ if not isinstance(option, BaseModel):
59
+ raise TypeError("security option must be a pydantic model")
60
+
61
+ opt_fields: Dict[str, FieldInfo] = option.__class__.model_fields
62
+ for name in opt_fields:
63
+ opt_field = opt_fields[name]
64
+
65
+ metadata = find_field_metadata(opt_field, SecurityMetadata)
66
+ if metadata is None or not metadata.scheme:
67
+ continue
68
+ _parse_security_scheme(
69
+ headers, query_params, metadata, name, getattr(option, name)
70
+ )
71
+
72
+
73
+ def _parse_security_scheme(
74
+ headers: Dict[str, str],
75
+ query_params: Dict[str, List[str]],
76
+ scheme_metadata: SecurityMetadata,
77
+ field_name: str,
78
+ scheme: Any,
79
+ ):
80
+ scheme_type = scheme_metadata.scheme_type
81
+ sub_type = scheme_metadata.sub_type
82
+
83
+ if isinstance(scheme, BaseModel):
84
+ if scheme_type == "http":
85
+ if sub_type == "basic":
86
+ _parse_basic_auth_scheme(headers, scheme)
87
+ return
88
+ if sub_type == "custom":
89
+ return
90
+
91
+ scheme_fields: Dict[str, FieldInfo] = scheme.__class__.model_fields
92
+ for name in scheme_fields:
93
+ scheme_field = scheme_fields[name]
94
+
95
+ metadata = find_field_metadata(scheme_field, SecurityMetadata)
96
+ if metadata is None or metadata.field_name is None:
97
+ continue
98
+
99
+ value = getattr(scheme, name)
100
+
101
+ _parse_security_scheme_value(
102
+ headers, query_params, scheme_metadata, metadata, name, value
103
+ )
104
+ else:
105
+ _parse_security_scheme_value(
106
+ headers, query_params, scheme_metadata, scheme_metadata, field_name, scheme
107
+ )
108
+
109
+
110
+ def _parse_security_scheme_value(
111
+ headers: Dict[str, str],
112
+ query_params: Dict[str, List[str]],
113
+ scheme_metadata: SecurityMetadata,
114
+ security_metadata: SecurityMetadata,
115
+ field_name: str,
116
+ value: Any,
117
+ ):
118
+ scheme_type = scheme_metadata.scheme_type
119
+ sub_type = scheme_metadata.sub_type
120
+
121
+ header_name = security_metadata.get_field_name(field_name)
122
+
123
+ if scheme_type == "apiKey":
124
+ if sub_type == "header":
125
+ headers[header_name] = value
126
+ elif sub_type == "query":
127
+ query_params[header_name] = [value]
128
+ else:
129
+ raise ValueError("sub type {sub_type} not supported")
130
+ elif scheme_type == "openIdConnect":
131
+ headers[header_name] = _apply_bearer(value)
132
+ elif scheme_type == "oauth2":
133
+ if sub_type != "client_credentials":
134
+ headers[header_name] = _apply_bearer(value)
135
+ elif scheme_type == "http":
136
+ if sub_type == "bearer":
137
+ headers[header_name] = _apply_bearer(value)
138
+ elif sub_type == "custom":
139
+ return
140
+ else:
141
+ raise ValueError("sub type {sub_type} not supported")
142
+ else:
143
+ raise ValueError("scheme type {scheme_type} not supported")
144
+
145
+
146
+ def _apply_bearer(token: str) -> str:
147
+ return token.lower().startswith("bearer ") and token or f"Bearer {token}"
148
+
149
+
150
+ def _parse_basic_auth_scheme(headers: Dict[str, str], scheme: Any):
151
+ username = ""
152
+ password = ""
153
+
154
+ if not isinstance(scheme, BaseModel):
155
+ raise TypeError("basic auth scheme must be a pydantic model")
156
+
157
+ scheme_fields: Dict[str, FieldInfo] = scheme.__class__.model_fields
158
+ for name in scheme_fields:
159
+ scheme_field = scheme_fields[name]
160
+
161
+ metadata = find_field_metadata(scheme_field, SecurityMetadata)
162
+ if metadata is None or metadata.field_name is None:
163
+ continue
164
+
165
+ field_name = metadata.field_name
166
+ value = getattr(scheme, name)
167
+
168
+ if field_name == "username":
169
+ username = value
170
+ if field_name == "password":
171
+ password = value
172
+
173
+ data = f"{username}:{password}".encode()
174
+ headers["Authorization"] = f"Basic {base64.b64encode(data).decode()}"
@@ -0,0 +1,248 @@
1
+ """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
2
+
3
+ from decimal import Decimal
4
+ import functools
5
+ import json
6
+ import typing
7
+ from typing import Any, Dict, List, Tuple, Union, get_args
8
+ import typing_extensions
9
+ from typing_extensions import get_origin
10
+
11
+ import httpx
12
+ from pydantic import ConfigDict, create_model
13
+ from pydantic_core import from_json
14
+
15
+ from ..types.basemodel import BaseModel, Nullable, OptionalNullable, Unset
16
+
17
+
18
+ def serialize_decimal(as_str: bool):
19
+ def serialize(d):
20
+ # Optional[T] is a Union[T, None]
21
+ if is_union(type(d)) and type(None) in get_args(type(d)) and d is None:
22
+ return None
23
+ if isinstance(d, Unset):
24
+ return d
25
+
26
+ if not isinstance(d, Decimal):
27
+ raise ValueError("Expected Decimal object")
28
+
29
+ return str(d) if as_str else float(d)
30
+
31
+ return serialize
32
+
33
+
34
+ def validate_decimal(d):
35
+ if d is None:
36
+ return None
37
+
38
+ if isinstance(d, (Decimal, Unset)):
39
+ return d
40
+
41
+ if not isinstance(d, (str, int, float)):
42
+ raise ValueError("Expected string, int or float")
43
+
44
+ return Decimal(str(d))
45
+
46
+
47
+ def serialize_float(as_str: bool):
48
+ def serialize(f):
49
+ # Optional[T] is a Union[T, None]
50
+ if is_union(type(f)) and type(None) in get_args(type(f)) and f is None:
51
+ return None
52
+ if isinstance(f, Unset):
53
+ return f
54
+
55
+ if not isinstance(f, float):
56
+ raise ValueError("Expected float")
57
+
58
+ return str(f) if as_str else f
59
+
60
+ return serialize
61
+
62
+
63
+ def validate_float(f):
64
+ if f is None:
65
+ return None
66
+
67
+ if isinstance(f, (float, Unset)):
68
+ return f
69
+
70
+ if not isinstance(f, str):
71
+ raise ValueError("Expected string")
72
+
73
+ return float(f)
74
+
75
+
76
+ def serialize_int(as_str: bool):
77
+ def serialize(i):
78
+ # Optional[T] is a Union[T, None]
79
+ if is_union(type(i)) and type(None) in get_args(type(i)) and i is None:
80
+ return None
81
+ if isinstance(i, Unset):
82
+ return i
83
+
84
+ if not isinstance(i, int):
85
+ raise ValueError("Expected int")
86
+
87
+ return str(i) if as_str else i
88
+
89
+ return serialize
90
+
91
+
92
+ def validate_int(b):
93
+ if b is None:
94
+ return None
95
+
96
+ if isinstance(b, (int, Unset)):
97
+ return b
98
+
99
+ if not isinstance(b, str):
100
+ raise ValueError("Expected string")
101
+
102
+ return int(b)
103
+
104
+
105
+ def validate_open_enum(is_int: bool):
106
+ def validate(e):
107
+ if e is None:
108
+ return None
109
+
110
+ if isinstance(e, Unset):
111
+ return e
112
+
113
+ if is_int:
114
+ if not isinstance(e, int):
115
+ raise ValueError("Expected int")
116
+ else:
117
+ if not isinstance(e, str):
118
+ raise ValueError("Expected string")
119
+
120
+ return e
121
+
122
+ return validate
123
+
124
+
125
+ def validate_const(v):
126
+ def validate(c):
127
+ # Optional[T] is a Union[T, None]
128
+ if is_union(type(c)) and type(None) in get_args(type(c)) and c is None:
129
+ return None
130
+
131
+ if v != c:
132
+ raise ValueError(f"Expected {v}")
133
+
134
+ return c
135
+
136
+ return validate
137
+
138
+
139
+ def unmarshal_json(raw, typ: Any) -> Any:
140
+ return unmarshal(from_json(raw), typ)
141
+
142
+
143
+ def unmarshal(val, typ: Any) -> Any:
144
+ unmarshaller = create_model(
145
+ "Unmarshaller",
146
+ body=(typ, ...),
147
+ __config__=ConfigDict(populate_by_name=True, arbitrary_types_allowed=True),
148
+ )
149
+
150
+ m = unmarshaller(body=val)
151
+
152
+ # pyright: ignore[reportAttributeAccessIssue]
153
+ return m.body # type: ignore
154
+
155
+
156
+ def marshal_json(val, typ):
157
+ if is_nullable(typ) and val is None:
158
+ return "null"
159
+
160
+ marshaller = create_model(
161
+ "Marshaller",
162
+ body=(typ, ...),
163
+ __config__=ConfigDict(populate_by_name=True, arbitrary_types_allowed=True),
164
+ )
165
+
166
+ m = marshaller(body=val)
167
+
168
+ d = m.model_dump(by_alias=True, mode="json", exclude_none=True)
169
+
170
+ if len(d) == 0:
171
+ return ""
172
+
173
+ return json.dumps(d[next(iter(d))], separators=(",", ":"))
174
+
175
+
176
+ def is_nullable(field):
177
+ origin = get_origin(field)
178
+ if origin is Nullable or origin is OptionalNullable:
179
+ return True
180
+
181
+ if not origin is Union or type(None) not in get_args(field):
182
+ return False
183
+
184
+ for arg in get_args(field):
185
+ if get_origin(arg) is Nullable or get_origin(arg) is OptionalNullable:
186
+ return True
187
+
188
+ return False
189
+
190
+
191
+ def is_union(obj: object) -> bool:
192
+ """
193
+ Returns True if the given object is a typing.Union or typing_extensions.Union.
194
+ """
195
+ return any(obj is typing_obj for typing_obj in _get_typing_objects_by_name_of("Union"))
196
+
197
+
198
+ def stream_to_text(stream: httpx.Response) -> str:
199
+ return "".join(stream.iter_text())
200
+
201
+
202
+ async def stream_to_text_async(stream: httpx.Response) -> str:
203
+ return "".join([chunk async for chunk in stream.aiter_text()])
204
+
205
+
206
+ def stream_to_bytes(stream: httpx.Response) -> bytes:
207
+ return stream.content
208
+
209
+
210
+ async def stream_to_bytes_async(stream: httpx.Response) -> bytes:
211
+ return await stream.aread()
212
+
213
+
214
+ def get_pydantic_model(data: Any, typ: Any) -> Any:
215
+ if not _contains_pydantic_model(data):
216
+ return unmarshal(data, typ)
217
+
218
+ return data
219
+
220
+
221
+ def _contains_pydantic_model(data: Any) -> bool:
222
+ if isinstance(data, BaseModel):
223
+ return True
224
+ if isinstance(data, List):
225
+ return any(_contains_pydantic_model(item) for item in data)
226
+ if isinstance(data, Dict):
227
+ return any(_contains_pydantic_model(value) for value in data.values())
228
+
229
+ return False
230
+
231
+
232
+ @functools.cache
233
+ def _get_typing_objects_by_name_of(name: str) -> Tuple[Any, ...]:
234
+ """
235
+ Get typing objects by name from typing and typing_extensions.
236
+ Reference: https://typing-extensions.readthedocs.io/en/latest/#runtime-use-of-types
237
+ """
238
+ result = tuple(
239
+ getattr(module, name)
240
+ for module in (typing, typing_extensions)
241
+ if hasattr(module, name)
242
+ )
243
+ if not result:
244
+ raise ValueError(
245
+ f"Neither typing nor typing_extensions has an object called {name!r}"
246
+ )
247
+ return result
248
+