Skip to content

Commit cbfeb75

Browse files
authored
Infer base URL from read token in query client (#1088)
1 parent 09b18d8 commit cbfeb75

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

logfire/experimental/query_client.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
from types import TracebackType
55
from typing import TYPE_CHECKING, Any, Generic, Literal, TypedDict, TypeVar
66

7+
from typing_extensions import Self
8+
9+
from logfire._internal.config import get_base_url_from_token
10+
711
try:
812
from httpx import AsyncClient, Client, Response, Timeout
913
from httpx._client import BaseClient
@@ -56,8 +60,6 @@ class RowQueryResults(TypedDict):
5660

5761

5862
T = TypeVar('T', bound=BaseClient)
59-
S = TypeVar('S', bound='LogfireQueryClient')
60-
R = TypeVar('R', bound='AsyncLogfireQueryClient')
6163

6264

6365
class _BaseLogfireQueryClient(Generic[T]):
@@ -102,13 +104,14 @@ class LogfireQueryClient(_BaseLogfireQueryClient[Client]):
102104
def __init__(
103105
self,
104106
read_token: str,
105-
base_url: str = 'https://logfire-api.pydantic.dev/',
107+
base_url: str | None = None,
106108
timeout: Timeout = DEFAULT_TIMEOUT,
107109
**client_kwargs: Any,
108110
):
111+
base_url = base_url or get_base_url_from_token(read_token)
109112
super().__init__(base_url, read_token, timeout, Client, **client_kwargs)
110113

111-
def __enter__(self: S) -> S:
114+
def __enter__(self) -> Self:
112115
self.client.__enter__()
113116
return self
114117

@@ -226,13 +229,14 @@ class AsyncLogfireQueryClient(_BaseLogfireQueryClient[AsyncClient]):
226229
def __init__(
227230
self,
228231
read_token: str,
229-
base_url: str = 'https://logfire-api.pydantic.dev/',
232+
base_url: str | None = None,
230233
timeout: Timeout = DEFAULT_TIMEOUT,
231234
**async_client_kwargs: Any,
232235
):
236+
base_url = base_url or get_base_url_from_token(read_token)
233237
super().__init__(base_url, read_token, timeout, AsyncClient, **async_client_kwargs)
234238

235-
async def __aenter__(self: R) -> R:
239+
async def __aenter__(self) -> Self:
236240
await self.client.__aenter__()
237241
return self
238242

tests/aaa_query_client/test_query_client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import sys
24
from datetime import datetime, timezone
35

@@ -21,6 +23,22 @@
2123
]
2224

2325

26+
@pytest.mark.parametrize('client_class', [AsyncLogfireQueryClient, LogfireQueryClient])
27+
@pytest.mark.parametrize(
28+
['token', 'expected'],
29+
[
30+
('pylf_v1_us_0kYhc414Ys2FNDRdt5vFB05xFx5NjVcbcBMy4Kp6PH0W', 'https://logfire-us.pydantic.dev'),
31+
('pylf_v1_eu_0kYhc414Ys2FNDRdt5vFB05xFx5NjVcbcBMy4Kp6PH0W', 'https://logfire-eu.pydantic.dev'),
32+
('0kYhc414Ys2FNDRdt5vFB05xFx5NjVcbcBMy4Kp6PH0W', 'https://logfire-us.pydantic.dev'),
33+
],
34+
)
35+
def test_infers_base_url_from_token(
36+
client_class: type[AsyncLogfireQueryClient | LogfireQueryClient], token: str, expected: str
37+
):
38+
client = client_class(read_token=token)
39+
assert client.base_url == expected
40+
41+
2442
def test_read_sync():
2543
with LogfireQueryClient(read_token=CLIENT_READ_TOKEN, base_url=CLIENT_BASE_URL) as client:
2644
sql = """

0 commit comments

Comments
 (0)