trd-utils 0.0.26__py3-none-any.whl → 0.0.28__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 trd-utils might be problematic. Click here for more details.

trd_utils/__init__.py CHANGED
@@ -1,3 +1,3 @@
1
1
 
2
- __version__ = "0.0.26"
2
+ __version__ = "0.0.28"
3
3
 
@@ -82,13 +82,12 @@ class ExchangeBase(ABC):
82
82
  headers=headers,
83
83
  params=params,
84
84
  )
85
- if raw_data:
86
- return response.content
87
-
88
- if not model_type:
89
- return response.json()
90
-
91
- return model_type.deserialize(response.json(parse_float=parse_float))
85
+ return self._handle_response(
86
+ response=response,
87
+ model_type=model_type,
88
+ parse_float=parse_float,
89
+ raw_data=raw_data,
90
+ )
92
91
 
93
92
  async def invoke_post(
94
93
  self,
@@ -113,14 +112,62 @@ class ExchangeBase(ABC):
113
112
  params=params,
114
113
  content=content,
115
114
  )
115
+ return self._handle_response(
116
+ response=response,
117
+ model_type=model_type,
118
+ parse_float=parse_float,
119
+ raw_data=raw_data,
120
+ )
121
+
122
+ def _handle_response(
123
+ self,
124
+ response: httpx.Response,
125
+ model_type: Type[BaseModel] | None = None,
126
+ parse_float=Decimal,
127
+ raw_data: bool = False,
128
+ ) -> "BaseModel":
116
129
  if raw_data:
117
130
  return response.content
118
131
 
132
+ j_obj = self._resp_to_json(
133
+ response=response,
134
+ parse_float=parse_float,
135
+ )
119
136
  if not model_type:
120
- return response.json()
137
+ return j_obj
121
138
 
122
- return model_type.deserialize(response.json(parse_float=parse_float))
139
+ return model_type.deserialize(j_obj)
123
140
 
141
+ def _resp_to_json(
142
+ self,
143
+ response: httpx.Response,
144
+ parse_float=None,
145
+ ):
146
+ try:
147
+ return response.json(parse_float=parse_float)
148
+ except UnicodeDecodeError:
149
+ # try to decompress manually
150
+ import gzip
151
+ import brotli
152
+
153
+ content_encoding = response.headers.get("Content-Encoding", "").lower()
154
+ content = response.content
155
+
156
+ if "gzip" in content_encoding:
157
+ content = gzip.decompress(content)
158
+ elif "br" in content_encoding:
159
+ content = brotli.decompress(content)
160
+ elif "deflate" in content_encoding:
161
+ import zlib
162
+
163
+ content = zlib.decompress(content, -zlib.MAX_WBITS)
164
+ else:
165
+ raise ValueError(
166
+ f"failed to detect content encoding: {content_encoding}"
167
+ )
168
+
169
+ # Now parse the decompressed content
170
+ return json.loads(content.decode("utf-8"), parse_float=parse_float)
124
171
 
125
172
  async def aclose(self) -> None:
126
173
  await self.httpx_client.aclose()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: trd_utils
3
- Version: 0.0.26
3
+ Version: 0.0.28
4
4
  Summary: Common Basic Utils for Python3. By ALiwoto.
5
5
  Keywords: utils,trd_utils,basic-utils,common-utils
6
6
  Author: ALiwoto
@@ -1,4 +1,4 @@
1
- trd_utils/__init__.py,sha256=Wy_DfSBoQg4JGnSX4I-RPMxCXnRcYWQW07O41KIS8LM,25
1
+ trd_utils/__init__.py,sha256=66Y_sF6wGqRS2D6AE1d9RI5UDSNDIy9_d8fnOFNzILE,25
2
2
  trd_utils/cipher/__init__.py,sha256=V05KNuzQwCic-ihMVHlC8sENaJGc3I8MCb4pg4849X8,1765
3
3
  trd_utils/common_utils/float_utils.py,sha256=W-jv7nzjl88xwGB6gsEXmDDhF6DseOrrVT2qx7OvyCo,266
4
4
  trd_utils/common_utils/wallet_utils.py,sha256=OX9q2fymP0VfIWTRIRBP8W33cfyjLXimxMgPOsZe-3g,727
@@ -14,7 +14,7 @@ trd_utils/exchanges/bx_ultra/__init__.py,sha256=8Ssy-eOemQR32Nv1-FoPHm87nRqRO4Fm
14
14
  trd_utils/exchanges/bx_ultra/bx_types.py,sha256=muHLa9lEjIOIDLQCb6xdyxSoQiIMSHErVe0NCoAS-RI,31017
15
15
  trd_utils/exchanges/bx_ultra/bx_ultra_client.py,sha256=AjaKbUFPWLTO_KwyCAB7DVbmJKUjSQqZs5A1NfSUCBo,25110
16
16
  trd_utils/exchanges/bx_ultra/bx_utils.py,sha256=PwapomwDW33arVmKIDj6cL-aP0ptu4BYy_lOCqSAPOo,1392
17
- trd_utils/exchanges/exchange_base.py,sha256=uxR5HWzProFjWVrYJDhxAdeHnp0pw9yB7P0NXfeQshA,4504
17
+ trd_utils/exchanges/exchange_base.py,sha256=QDWzltlGq4k2KsdAGMoLw_6_lZ7k3rPOP64cZKet0ng,5987
18
18
  trd_utils/exchanges/hyperliquid/README.md,sha256=-qaxmDt_9NTus2xRuzyFGkKgYDWgWk7ufHVTSkyn3t4,105
19
19
  trd_utils/exchanges/hyperliquid/__init__.py,sha256=QhwGRcneGFHREM-MMdYpbcx-aWdsWsu2WznHzx7LaUM,92
20
20
  trd_utils/exchanges/hyperliquid/hyperliquid_client.py,sha256=dE-WIj1_H_5qHnO2Uu4DBVl1qEPdLdQFJW0IIwia1UM,6804
@@ -29,7 +29,7 @@ trd_utils/tradingview/tradingview_client.py,sha256=g_eWYaCRQAL8Kvd-r6AnAdbH7Jha6
29
29
  trd_utils/tradingview/tradingview_types.py,sha256=z21MXPVdWHAduEl3gSeMIRhxtBN9yK-jPYHfZSMIbSA,6144
30
30
  trd_utils/types_helper/__init__.py,sha256=lLbUiW1jUV1gjzTMFLthwkvF0hwauH-F_J2JZq--1U0,67
31
31
  trd_utils/types_helper/base_model.py,sha256=Q2KK0r4UXP9PlWeIl6nxdAeCGB5InU5IkTNGAfJasfM,11808
32
- trd_utils-0.0.26.dist-info/LICENSE,sha256=J1EP2xt87RjjmsTV1jTjHDQMLIM9FjdwEftTpw8hyv4,1067
33
- trd_utils-0.0.26.dist-info/METADATA,sha256=CorynlnCD_2-PAjDTKSRYOdkt61sShVt-KqZlSQxSKo,1095
34
- trd_utils-0.0.26.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
35
- trd_utils-0.0.26.dist-info/RECORD,,
32
+ trd_utils-0.0.28.dist-info/LICENSE,sha256=J1EP2xt87RjjmsTV1jTjHDQMLIM9FjdwEftTpw8hyv4,1067
33
+ trd_utils-0.0.28.dist-info/METADATA,sha256=jPMNi7OsWV2GIUlYE-YaR4Vog9xGvh6WPgrq6goTtdk,1095
34
+ trd_utils-0.0.28.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
35
+ trd_utils-0.0.28.dist-info/RECORD,,