Skip to content

Commit f61adb2

Browse files
ADD: Public client test suite
1 parent 616866a commit f61adb2

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

examples/live_smoke_test.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#!/usr/bin/python3
2+
3+
import argparse
4+
import os
5+
import typing
6+
7+
from databento import Dataset
8+
from databento import Live
9+
from databento import RecordFlags
10+
from databento import Schema
11+
from databento import SType
12+
from databento_dbn import ErrorMsg
13+
from databento_dbn import MBOMsg
14+
from databento_dbn import RType
15+
from databento_dbn import SymbolMappingMsg
16+
17+
18+
def parse_args() -> argparse.Namespace:
19+
parser = argparse.ArgumentParser(prog="Python client")
20+
parser.add_argument("--gateway", type=str, help="Gateway to connect")
21+
parser.add_argument("--port", type=int, default=13000, help="Gatewat port to connect")
22+
parser.add_argument(
23+
"--api-key-env-var",
24+
type=str,
25+
help="Gateway to connect as Gateway::port",
26+
default="DATABENTO_API_KEY",
27+
)
28+
parser.add_argument("--dataset", type=Dataset, help="Dataset")
29+
parser.add_argument("--schema", type=Schema, help="Schema")
30+
parser.add_argument("--stype", type=SType, help="SType")
31+
parser.add_argument("--symbols", type=str, help="Symbols")
32+
parser.add_argument("--start", type=str, default=None, help="Start time (rfc-339)")
33+
parser.add_argument(
34+
"--use-snapshot",
35+
action="store_true",
36+
help="Whether or not to request snapshot subscription",
37+
)
38+
39+
return parser.parse_args()
40+
41+
42+
def run_client(args: argparse.Namespace) -> None:
43+
client = Live(key=get_api_key(args.api_key_env_var), gateway=args.gateway, port=args.port)
44+
45+
client.subscribe(
46+
dataset=args.dataset,
47+
schema=args.schema,
48+
stype_in=args.stype,
49+
symbols=args.symbols,
50+
start=args.start,
51+
)
52+
53+
print("Starting client...")
54+
55+
for record in client:
56+
if is_expected_record(args, record):
57+
print(f"Received expected record {record}")
58+
break
59+
elif isinstance(record, ErrorMsg):
60+
raise ValueError(f"Received error {record.err}")
61+
else:
62+
print(f"{record}")
63+
64+
print("Finished client")
65+
66+
67+
def run_client_with_snapshot(args: argparse.Namespace) -> None:
68+
client = Live(key=get_api_key(args.api_key_env_var), gateway=args.gateway, port=args.port)
69+
70+
client.subscribe(
71+
dataset=args.dataset,
72+
schema=args.schema,
73+
stype_in=args.stype,
74+
symbols=args.symbols,
75+
snapshot=True,
76+
)
77+
78+
received_snapshot_record = False
79+
80+
print("Starting client...")
81+
82+
for record in client:
83+
if isinstance(record, SymbolMappingMsg):
84+
continue
85+
elif isinstance(record, MBOMsg):
86+
if record.flags & RecordFlags.F_SNAPSHOT:
87+
received_snapshot_record = True
88+
else:
89+
print(f"Received expected record {record}")
90+
break
91+
elif isinstance(record, ErrorMsg):
92+
raise ValueError(f"Received error {record.err}")
93+
else:
94+
raise ValueError(f"Received unexpected record {record}")
95+
96+
print("Finished client")
97+
98+
assert received_snapshot_record
99+
100+
101+
def is_expected_record(args: argparse.Namespace, record: typing.Any) -> bool:
102+
try:
103+
start = int(args.start)
104+
except Exception:
105+
start = None
106+
107+
# For start != 0 we stop at SymbolMappingMsg so that the tests can be run outside trading hours
108+
should_expect_symbol_mapping = args.stype != SType.INSTRUMENT_ID and (
109+
start is None or start != 0
110+
)
111+
if should_expect_symbol_mapping:
112+
return isinstance(record, SymbolMappingMsg)
113+
else:
114+
return record.rtype == RType.from_schema(args.schema)
115+
116+
117+
def get_api_key(api_key_name: str) -> str:
118+
api_key = os.getenv(api_key_name)
119+
if not api_key:
120+
raise ValueError(f"Invalid api_key {api_key_name}")
121+
122+
return api_key
123+
124+
125+
def main() -> None:
126+
args = parse_args()
127+
128+
if args.use_snapshot:
129+
run_client_with_snapshot(args)
130+
else:
131+
run_client(args)
132+
133+
134+
if __name__ == "__main__":
135+
main()

0 commit comments

Comments
 (0)