Repository navigation
Expand file tree
/
Copy pathhosts.py
More file actions
114 lines (94 loc) · 4.6 KB
/
Copy pathhosts.py
File metadata and controls
114 lines (94 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""Static host resolution for the Bybit v5 plugin.
Bybit has two independent environment axes: a live/demo axis and a regional
legal-entity axis (global, EU/MiCA, and several country domains). The
``(region, demo)`` pair resolves to a REST + public-WS + private-WS host
triple. This module holds only that static table plus the pure resolver — no
network I/O.
Only documented/verified hosts are listed. Where a regional host family has
no documented WebSocket endpoint, its entry is ``None`` and the caller must
supply an explicit override in the config (see the ``*_host`` fields of
:class:`pynecore_bybit.config.BybitConfig`); the resolver never guesses a
host by symmetry with another region.
Demo has no public stream of its own — demo runs on mainnet prices, so the
demo public-WS host is the region's live (mainnet) public stream, while REST
and the private stream use the dedicated ``*-demo`` hosts.
Verification status (2026-07-15):
* ``api.bybit.eu``, ``api-demo.bybit.eu``, ``api-demo.bybit.com`` returned
``retCode:0`` on ``/v5/market/time``.
* ``stream.bybit.eu`` (public+private), ``stream-demo.bybit.com/v5/private``
and ``stream-demo.bybit.eu/v5/private`` completed the WS handshake (HTTP
101).
* ``api.bybit.com`` / ``stream.bybit.com`` are the official global hosts.
* Regional REST domains and the ``.tr``/``.id``/``.kz``/``.ge`` stream
domains are from Bybit's published host list; regions without a documented
stream carry ``None``.
"""
from typing import NamedTuple
class BybitHosts(NamedTuple):
"""Resolved host triple (bare domains, no scheme or path)."""
rest: str
ws_public: str | None
ws_private: str | None
#: Live (mainnet) host triples per region.
_LIVE: dict[str, BybitHosts] = {
"global": BybitHosts("api.bybit.com", "stream.bybit.com", "stream.bybit.com"),
"eu": BybitHosts("api.bybit.eu", "stream.bybit.eu", "stream.bybit.eu"),
"nl": BybitHosts("api.bybit.nl", None, None),
"tr": BybitHosts("api.bybit.tr", "stream.bybit.tr", "stream.bybit.tr"),
"kz": BybitHosts("api.bybit.kz", "stream.bybit.kz", "stream.bybit.kz"),
"ge": BybitHosts("api.bybitgeorgia.ge", "stream.bybitgeorgia.ge", "stream.bybitgeorgia.ge"),
"ae": BybitHosts("api.bybit.ae", None, None),
"id": BybitHosts("api.bybit.id", "stream.bybit.id", "stream.bybit.id"),
"testnet": BybitHosts("api-testnet.bybit.com", None, None),
}
#: Demo host triples. Only regions with a documented/verified demo host are
#: listed; the public stream points at the region's live (mainnet) stream
#: because demo has no public stream of its own.
_DEMO: dict[str, BybitHosts] = {
"global": BybitHosts("api-demo.bybit.com", "stream.bybit.com", "stream-demo.bybit.com"),
"eu": BybitHosts("api-demo.bybit.eu", "stream.bybit.eu", "stream-demo.bybit.eu"),
}
#: Region keys accepted in the config, in documentation order.
REGIONS: tuple[str, ...] = (
"global", "eu", "nl", "tr", "kz", "ge", "ae", "id", "testnet",
)
def resolve_hosts(
region: str,
*,
demo: bool,
rest_host: str = "",
ws_public_host: str = "",
ws_private_host: str = "",
) -> BybitHosts:
"""Resolve the REST + WS host triple for a ``(region, demo)`` pair.
Non-empty override arguments win over the table (escape hatch for
unlisted domains). A region absent from the selected mode's table with no
override raises :class:`ValueError` — the resolver never guesses a host.
:param region: One of :data:`REGIONS`.
:param demo: Select the demo environment instead of live.
:param rest_host: Optional REST host override.
:param ws_public_host: Optional public-WS host override.
:param ws_private_host: Optional private-WS host override.
:return: The resolved :class:`BybitHosts`.
:raises ValueError: On an unknown region, or a region unavailable in the
selected mode with no matching override.
"""
if region not in REGIONS:
raise ValueError(
f"Unknown Bybit region {region!r}; expected one of {', '.join(REGIONS)}"
)
table = _DEMO if demo else _LIVE
base = table.get(region)
if base is None and not (rest_host and ws_public_host and ws_private_host):
mode = "demo" if demo else "live"
raise ValueError(
f"Bybit region {region!r} has no known {mode} host; set rest_host, "
f"ws_public_host and ws_private_host overrides in the config to use it"
)
if base is None:
return BybitHosts(rest_host, ws_public_host, ws_private_host)
return BybitHosts(
rest=rest_host or base.rest,
ws_public=ws_public_host or base.ws_public,
ws_private=ws_private_host or base.ws_private,
)