Skip to content

Commit 28399d7

Browse files
committed
feat(client): support reading the base url from an env variable (#186)
1 parent a0fa576 commit 28399d7

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
@@ -320,6 +320,7 @@ import httpx
320320
from finch import Finch
321321

322322
client = Finch(
323+
# Or use the `FINCH_BASE_URL` env var
323324
base_url="http://my.test.server.example.com:8083",
324325
http_client=httpx.Client(
325326
proxies="http://my.test.proxy.example.com",

src/finch/_client.py

+4
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ def __init__(
112112
webhook_secret = os.environ.get("FINCH_WEBHOOK_SECRET")
113113
self.webhook_secret = webhook_secret
114114

115+
if base_url is None:
116+
base_url = os.environ.get("FINCH_BASE_URL")
115117
if base_url is None:
116118
base_url = f"https://api.tryfinch.com"
117119

@@ -414,6 +416,8 @@ def __init__(
414416
webhook_secret = os.environ.get("FINCH_WEBHOOK_SECRET")
415417
self.webhook_secret = webhook_secret
416418

419+
if base_url is None:
420+
base_url = os.environ.get("FINCH_BASE_URL")
417421
if base_url is None:
418422
base_url = f"https://api.tryfinch.com"
419423

tests/test_client.py

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

29+
from .utils import update_env
30+
2931
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
3032
access_token = "My Access Token"
3133

@@ -418,6 +420,11 @@ class Model2(BaseModel):
418420
assert isinstance(response, Model1)
419421
assert response.foo == 1
420422

423+
def test_base_url_env(self) -> None:
424+
with update_env(FINCH_BASE_URL="http://localhost:5000/from/env"):
425+
client = Finch(access_token=access_token, _strict_response_validation=True)
426+
assert client.base_url == "http://localhost:5000/from/env/"
427+
421428
@pytest.mark.parametrize(
422429
"client",
423430
[
@@ -1061,6 +1068,11 @@ class Model2(BaseModel):
10611068
assert isinstance(response, Model1)
10621069
assert response.foo == 1
10631070

1071+
def test_base_url_env(self) -> None:
1072+
with update_env(FINCH_BASE_URL="http://localhost:5000/from/env"):
1073+
client = AsyncFinch(access_token=access_token, _strict_response_validation=True)
1074+
assert client.base_url == "http://localhost:5000/from/env/"
1075+
10641076
@pytest.mark.parametrize(
10651077
"client",
10661078
[

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)