hyperliquid-python-sdk-async 0.24.6__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.
- hyperliquid/__init__.py +0 -0
- hyperliquid/api.py +69 -0
- hyperliquid/exchange.py +888 -0
- hyperliquid/info.py +288 -0
- hyperliquid/utils/__init__.py +0 -0
- hyperliquid/utils/constants.py +3 -0
- hyperliquid/utils/error.py +17 -0
- hyperliquid/utils/signing.py +527 -0
- hyperliquid/utils/types.py +220 -0
- hyperliquid/websocket_manager.py +197 -0
- hyperliquid_python_sdk_async-0.24.6.dist-info/LICENSE.md +21 -0
- hyperliquid_python_sdk_async-0.24.6.dist-info/METADATA +162 -0
- hyperliquid_python_sdk_async-0.24.6.dist-info/RECORD +15 -0
- hyperliquid_python_sdk_async-0.24.6.dist-info/WHEEL +4 -0
- hyperliquid_python_sdk_async-0.24.6.dist-info/entry_points.txt +3 -0
hyperliquid/__init__.py
ADDED
|
File without changes
|
hyperliquid/api.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import logging
|
|
3
|
+
from json import JSONDecodeError
|
|
4
|
+
|
|
5
|
+
import aiohttp
|
|
6
|
+
|
|
7
|
+
from hyperliquid.utils.constants import MAINNET_API_URL
|
|
8
|
+
from hyperliquid.utils.error import ClientError, ServerError
|
|
9
|
+
from hyperliquid.utils.types import Any, Optional
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class API:
|
|
13
|
+
def __init__(self, base_url: Optional[str] = None, timeout: Optional[float] = None, session=None):
|
|
14
|
+
self.base_url = base_url or MAINNET_API_URL
|
|
15
|
+
self.session: Optional[aiohttp.ClientSession] = session
|
|
16
|
+
self._owns_session = session is None
|
|
17
|
+
self._logger = logging.getLogger(__name__)
|
|
18
|
+
self.timeout = timeout
|
|
19
|
+
|
|
20
|
+
if self.session is not None:
|
|
21
|
+
self.session.headers.update({"Content-Type": "application/json"})
|
|
22
|
+
|
|
23
|
+
async def _ensure_session(self) -> aiohttp.ClientSession:
|
|
24
|
+
if self.session is None or self.session.closed:
|
|
25
|
+
timeout = aiohttp.ClientTimeout(total=self.timeout) if self.timeout is not None else None
|
|
26
|
+
self.session = aiohttp.ClientSession(timeout=timeout, headers={"Content-Type": "application/json"})
|
|
27
|
+
self._owns_session = True
|
|
28
|
+
return self.session
|
|
29
|
+
|
|
30
|
+
async def aclose(self) -> None:
|
|
31
|
+
if self.session is not None and not self.session.closed and self._owns_session:
|
|
32
|
+
await self.session.close()
|
|
33
|
+
|
|
34
|
+
async def __aenter__(self):
|
|
35
|
+
await self._ensure_session()
|
|
36
|
+
return self
|
|
37
|
+
|
|
38
|
+
async def __aexit__(self, exc_type, exc, tb) -> None:
|
|
39
|
+
await self.aclose()
|
|
40
|
+
|
|
41
|
+
async def post(self, url_path: str, payload: Any = None) -> Any:
|
|
42
|
+
payload = payload or {}
|
|
43
|
+
url = self.base_url + url_path
|
|
44
|
+
session = await self._ensure_session()
|
|
45
|
+
|
|
46
|
+
async with session.post(url, json=payload) as response:
|
|
47
|
+
await self._handle_exception(response)
|
|
48
|
+
try:
|
|
49
|
+
return await response.json()
|
|
50
|
+
except (ValueError, aiohttp.ContentTypeError):
|
|
51
|
+
return {"error": f"Could not parse JSON: {await response.text()}"}
|
|
52
|
+
|
|
53
|
+
async def _handle_exception(self, response: aiohttp.ClientResponse) -> None:
|
|
54
|
+
status_code = response.status
|
|
55
|
+
if status_code < 400:
|
|
56
|
+
return
|
|
57
|
+
|
|
58
|
+
text = await response.text()
|
|
59
|
+
if 400 <= status_code < 500:
|
|
60
|
+
try:
|
|
61
|
+
err = json.loads(text)
|
|
62
|
+
except JSONDecodeError as exc:
|
|
63
|
+
raise ClientError(status_code, None, text, None, response.headers) from exc
|
|
64
|
+
if err is None:
|
|
65
|
+
raise ClientError(status_code, None, text, None, response.headers)
|
|
66
|
+
error_data = err.get("data")
|
|
67
|
+
raise ClientError(status_code, err["code"], err["msg"], response.headers, error_data)
|
|
68
|
+
|
|
69
|
+
raise ServerError(status_code, text)
|