Skip to content

Commit 34c94ca

Browse files
committed
MOD: Add client key for Live auth request
1 parent c1749aa commit 34c94ca

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

databento/common/system.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import sys
2+
3+
from databento.version import __version__
4+
5+
6+
PYTHON_VERSION = f"{sys.version_info.major}.{sys.version_info.minor}"
7+
USER_AGENT = f"Databento/{__version__} Python/{PYTHON_VERSION}"

databento/historical/http.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import json
4-
import sys
54
import warnings
65
from collections.abc import Iterable
76
from io import BytesIO
@@ -21,7 +20,7 @@
2120
from databento.common.error import BentoDeprecationWarning
2221
from databento.common.error import BentoServerError
2322
from databento.common.error import BentoWarning
24-
from databento.version import __version__
23+
from databento.common.system import USER_AGENT
2524

2625

2726
_32KIB = 1024 * 32 # 32_768
@@ -36,12 +35,9 @@ class BentoHttpAPI:
3635
TIMEOUT = 100
3736

3837
def __init__(self, key: str, gateway: str):
39-
python_version = f"{sys.version_info.major}.{sys.version_info.minor}"
40-
user_agent = f"Databento/{__version__} Python/{python_version}"
41-
4238
self._key = key
4339
self._gateway = gateway
44-
self._headers = {"accept": "application/json", "user-agent": user_agent}
40+
self._headers = {"accept": "application/json", "user-agent": USER_AGENT}
4541

4642
def _check_api_key(self) -> None:
4743
if self._key == "YOUR_API_KEY":

databento/live/gateway.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import dataclasses
44
import logging
5-
from functools import partial
65
from io import BytesIO
76
from operator import attrgetter
87
from typing import TypeVar
@@ -12,12 +11,14 @@
1211
from databento_dbn import SType
1312

1413
from databento.common.publishers import Dataset
14+
from databento.common.system import USER_AGENT
1515

1616

1717
logger = logging.getLogger(__name__)
1818

1919
T = TypeVar("T", bound="GatewayControl")
2020

21+
2122
@dataclasses.dataclass
2223
class GatewayControl:
2324
"""
@@ -42,9 +43,8 @@ def parse(cls: type[T], line: str) -> T:
4243
if not line.endswith("\n"):
4344
raise ValueError(f"`{line.strip()}` does not end with a newline")
4445

45-
tokens = line[:-1].split("|") # split excluding trailing new line
46-
splitter = partial(str.split, sep="=", maxsplit=1)
47-
data_dict = {k: v for k, v in map(splitter, tokens)}
46+
split_tokens = [t.partition("=") for t in line[:-1].split("|")]
47+
data_dict = {k: v for k, _, v in split_tokens}
4848

4949
try:
5050
return cls(**data_dict)
@@ -108,6 +108,7 @@ class AuthenticationRequest(GatewayControl):
108108
encoding: Encoding = Encoding.DBN
109109
details: str | None = None
110110
ts_out: str = "0"
111+
client: str = USER_AGENT
111112

112113

113114
@dataclasses.dataclass

tests/test_live_gateway_messages.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,18 @@ def test_parse_authentication_request(
8686
AuthenticationRequest(
8787
auth="abcd1234",
8888
dataset=Dataset.GLBX_MDP3,
89+
client="unittest",
8990
),
90-
b"auth=abcd1234|dataset=GLBX.MDP3|encoding=dbn|ts_out=0\n",
91+
b"auth=abcd1234|dataset=GLBX.MDP3|encoding=dbn|ts_out=0|client=unittest\n",
9192
),
9293
pytest.param(
9394
AuthenticationRequest(
9495
auth="abcd1234",
9596
dataset=Dataset.XNAS_ITCH,
9697
ts_out="1",
98+
client="unittest",
9799
),
98-
b"auth=abcd1234|dataset=XNAS.ITCH|encoding=dbn|ts_out=1\n",
100+
b"auth=abcd1234|dataset=XNAS.ITCH|encoding=dbn|ts_out=1|client=unittest\n",
99101
),
100102
],
101103
)
@@ -243,6 +245,7 @@ def test_serialize_greeting(
243245
"line, expected",
244246
[
245247
pytest.param("start_session=0\n", "0"),
248+
pytest.param("start_session\n", "", id="no_value"),
246249
pytest.param("start_session=0", ValueError, id="no_newline"),
247250
pytest.param("start_session=0|extra=key\n", ValueError, id="extra_key"),
248251
],

0 commit comments

Comments
 (0)