Skip to content

Commit

Permalink
MOD: Add client key for Live auth request
Browse files Browse the repository at this point in the history
  • Loading branch information
nmacholl committed Oct 20, 2023
1 parent c1749aa commit 34c94ca
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
7 changes: 7 additions & 0 deletions databento/common/system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sys

from databento.version import __version__


PYTHON_VERSION = f"{sys.version_info.major}.{sys.version_info.minor}"
USER_AGENT = f"Databento/{__version__} Python/{PYTHON_VERSION}"
8 changes: 2 additions & 6 deletions databento/historical/http.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import json
import sys
import warnings
from collections.abc import Iterable
from io import BytesIO
Expand All @@ -21,7 +20,7 @@
from databento.common.error import BentoDeprecationWarning
from databento.common.error import BentoServerError
from databento.common.error import BentoWarning
from databento.version import __version__
from databento.common.system import USER_AGENT


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

def __init__(self, key: str, gateway: str):
python_version = f"{sys.version_info.major}.{sys.version_info.minor}"
user_agent = f"Databento/{__version__} Python/{python_version}"

self._key = key
self._gateway = gateway
self._headers = {"accept": "application/json", "user-agent": user_agent}
self._headers = {"accept": "application/json", "user-agent": USER_AGENT}

def _check_api_key(self) -> None:
if self._key == "YOUR_API_KEY":
Expand Down
9 changes: 5 additions & 4 deletions databento/live/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import dataclasses
import logging
from functools import partial
from io import BytesIO
from operator import attrgetter
from typing import TypeVar
Expand All @@ -12,12 +11,14 @@
from databento_dbn import SType

from databento.common.publishers import Dataset
from databento.common.system import USER_AGENT


logger = logging.getLogger(__name__)

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


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

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

try:
return cls(**data_dict)
Expand Down Expand Up @@ -108,6 +108,7 @@ class AuthenticationRequest(GatewayControl):
encoding: Encoding = Encoding.DBN
details: str | None = None
ts_out: str = "0"
client: str = USER_AGENT


@dataclasses.dataclass
Expand Down
7 changes: 5 additions & 2 deletions tests/test_live_gateway_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,18 @@ def test_parse_authentication_request(
AuthenticationRequest(
auth="abcd1234",
dataset=Dataset.GLBX_MDP3,
client="unittest",
),
b"auth=abcd1234|dataset=GLBX.MDP3|encoding=dbn|ts_out=0\n",
b"auth=abcd1234|dataset=GLBX.MDP3|encoding=dbn|ts_out=0|client=unittest\n",
),
pytest.param(
AuthenticationRequest(
auth="abcd1234",
dataset=Dataset.XNAS_ITCH,
ts_out="1",
client="unittest",
),
b"auth=abcd1234|dataset=XNAS.ITCH|encoding=dbn|ts_out=1\n",
b"auth=abcd1234|dataset=XNAS.ITCH|encoding=dbn|ts_out=1|client=unittest\n",
),
],
)
Expand Down Expand Up @@ -243,6 +245,7 @@ def test_serialize_greeting(
"line, expected",
[
pytest.param("start_session=0\n", "0"),
pytest.param("start_session\n", "", id="no_value"),
pytest.param("start_session=0", ValueError, id="no_newline"),
pytest.param("start_session=0|extra=key\n", ValueError, id="extra_key"),
],
Expand Down

0 comments on commit 34c94ca

Please sign in to comment.