Skip to content

Commit f3ce13e

Browse files
committed
feat(client): support reading the base url from an env variable (#54)
1 parent 264148e commit f3ce13e

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ import httpx
314314
from orb import Orb
315315

316316
client = Orb(
317+
# Or use the `ORB_BASE_URL` env var
317318
base_url="http://my.test.server.example.com:8083",
318319
http_client=httpx.Client(
319320
proxies="http://my.test.proxy.example.com",

src/orb/_client.py

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def __init__(
9090
)
9191
self.api_key = api_key
9292

93+
if base_url is None:
94+
base_url = os.environ.get("ORB_BASE_URL")
9395
if base_url is None:
9496
base_url = f"https://api.withorb.com/v1"
9597

@@ -326,6 +328,8 @@ def __init__(
326328
)
327329
self.api_key = api_key
328330

331+
if base_url is None:
332+
base_url = os.environ.get("ORB_BASE_URL")
329333
if base_url is None:
330334
base_url = f"https://api.withorb.com/v1"
331335

tests/test_client.py

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
make_request_options,
2626
)
2727

28+
from .utils import update_env
29+
2830
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
2931
api_key = "My API Key"
3032

@@ -421,6 +423,11 @@ def test_idempotency_header_options(self, respx_mock: MockRouter) -> None:
421423
)
422424
assert response.request.headers.get("Idempotency-Key") == "custom-key"
423425

426+
def test_base_url_env(self) -> None:
427+
with update_env(ORB_BASE_URL="http://localhost:5000/from/env"):
428+
client = Orb(api_key=api_key, _strict_response_validation=True)
429+
assert client.base_url == "http://localhost:5000/from/env/"
430+
424431
@pytest.mark.parametrize(
425432
"client",
426433
[
@@ -975,6 +982,11 @@ async def test_idempotency_header_options(self, respx_mock: MockRouter) -> None:
975982
)
976983
assert response.request.headers.get("Idempotency-Key") == "custom-key"
977984

985+
def test_base_url_env(self) -> None:
986+
with update_env(ORB_BASE_URL="http://localhost:5000/from/env"):
987+
client = AsyncOrb(api_key=api_key, _strict_response_validation=True)
988+
assert client.base_url == "http://localhost:5000/from/env/"
989+
978990
@pytest.mark.parametrize(
979991
"client",
980992
[

tests/utils.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from __future__ import annotations
22

3+
import os
34
import traceback
4-
from typing import Any, TypeVar, cast
5+
import contextlib
6+
from typing import Any, TypeVar, Iterator, cast
57
from datetime import date, datetime
68
from typing_extensions import Literal, get_args, get_origin, assert_type
79

@@ -103,3 +105,16 @@ def _assert_list_type(type_: type[object], value: object) -> None:
103105
inner_type = get_args(type_)[0]
104106
for entry in value:
105107
assert_type(inner_type, entry) # type: ignore
108+
109+
110+
@contextlib.contextmanager
111+
def update_env(**new_env: str) -> Iterator[None]:
112+
old = os.environ.copy()
113+
114+
try:
115+
os.environ.update(new_env)
116+
117+
yield None
118+
finally:
119+
os.environ.clear()
120+
os.environ.update(old)

0 commit comments

Comments
 (0)