Skip to content

Commit d10f1a3

Browse files
committed
feat: allow injection of httpx client
1 parent 2675964 commit d10f1a3

File tree

4 files changed

+80
-20
lines changed

4 files changed

+80
-20
lines changed

supabase_functions/_async/functions_client.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(
2222
timeout: int,
2323
verify: bool = True,
2424
proxy: Optional[str] = None,
25+
http_client: Optional[AsyncClient] = None,
2526
):
2627
if not is_http_url(url):
2728
raise ValueError("url must be a valid HTTP URL string")
@@ -30,15 +31,21 @@ def __init__(
3031
"User-Agent": f"supabase-py/functions-py v{__version__}",
3132
**headers,
3233
}
33-
self._client = AsyncClient(
34-
base_url=self.url,
35-
headers=self.headers,
36-
verify=bool(verify),
37-
timeout=int(abs(timeout)),
38-
proxy=proxy,
39-
follow_redirects=True,
40-
http2=True,
41-
)
34+
35+
if http_client is not None:
36+
http_client.base_url = self.url
37+
http_client.headers.update({**self.headers})
38+
self._client = http_client
39+
else:
40+
self._client = AsyncClient(
41+
base_url=self.url,
42+
headers=self.headers,
43+
verify=bool(verify),
44+
timeout=int(abs(timeout)),
45+
proxy=proxy,
46+
follow_redirects=True,
47+
http2=True,
48+
)
4249

4350
async def _request(
4451
self,

supabase_functions/_sync/functions_client.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(
2222
timeout: int,
2323
verify: bool = True,
2424
proxy: Optional[str] = None,
25+
http_client: Optional[SyncClient] = None,
2526
):
2627
if not is_http_url(url):
2728
raise ValueError("url must be a valid HTTP URL string")
@@ -30,15 +31,21 @@ def __init__(
3031
"User-Agent": f"supabase-py/functions-py v{__version__}",
3132
**headers,
3233
}
33-
self._client = SyncClient(
34-
base_url=self.url,
35-
headers=self.headers,
36-
verify=bool(verify),
37-
timeout=int(abs(timeout)),
38-
proxy=proxy,
39-
follow_redirects=True,
40-
http2=True,
41-
)
34+
35+
if http_client is not None:
36+
http_client.base_url = self.url
37+
http_client.headers.update({**self.headers})
38+
self._client = http_client
39+
else:
40+
self._client = SyncClient(
41+
base_url=self.url,
42+
headers=self.headers,
43+
verify=bool(verify),
44+
timeout=int(abs(timeout)),
45+
proxy=proxy,
46+
follow_redirects=True,
47+
http2=True,
48+
)
4249

4350
def _request(
4451
self,

tests/_async/test_function_client.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Import the class to test
77
from supabase_functions import AsyncFunctionsClient
88
from supabase_functions.errors import FunctionsHttpError, FunctionsRelayError
9-
from supabase_functions.utils import FunctionRegion
9+
from supabase_functions.utils import AsyncClient, FunctionRegion
1010
from supabase_functions.version import __version__
1111

1212

@@ -197,3 +197,26 @@ async def test_invoke_with_json_body(client: AsyncFunctionsClient):
197197

198198
_, kwargs = mock_request.call_args
199199
assert kwargs["headers"]["Content-Type"] == "application/json"
200+
201+
202+
async def test_init_with_httpx_client():
203+
# Create a custom httpx client with specific options
204+
custom_client = AsyncClient(
205+
timeout=Timeout(30), follow_redirects=True, max_redirects=5
206+
)
207+
208+
# Initialize the functions client with the custom httpx client
209+
client = AsyncFunctionsClient(
210+
url="https://example.com",
211+
headers={"Authorization": "Bearer token"},
212+
timeout=30,
213+
http_client=custom_client,
214+
)
215+
216+
# Verify the custom client options are preserved
217+
assert client._client.timeout == Timeout(30)
218+
assert client._client.follow_redirects is True
219+
assert client._client.max_redirects == 5
220+
221+
# Verify the client is properly configured with our custom client
222+
assert client._client is custom_client

tests/_sync/test_function_client.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Import the class to test
77
from supabase_functions import SyncFunctionsClient
88
from supabase_functions.errors import FunctionsHttpError, FunctionsRelayError
9-
from supabase_functions.utils import FunctionRegion
9+
from supabase_functions.utils import FunctionRegion, SyncClient
1010
from supabase_functions.version import __version__
1111

1212

@@ -181,3 +181,26 @@ def test_invoke_with_json_body(client: SyncFunctionsClient):
181181

182182
_, kwargs = mock_request.call_args
183183
assert kwargs["headers"]["Content-Type"] == "application/json"
184+
185+
186+
def test_init_with_httpx_client():
187+
# Create a custom httpx client with specific options
188+
custom_client = SyncClient(
189+
timeout=Timeout(30), follow_redirects=True, max_redirects=5
190+
)
191+
192+
# Initialize the functions client with the custom httpx client
193+
client = SyncFunctionsClient(
194+
url="https://example.com",
195+
headers={"Authorization": "Bearer token"},
196+
timeout=30,
197+
http_client=custom_client,
198+
)
199+
200+
# Verify the custom client options are preserved
201+
assert client._client.timeout == Timeout(30)
202+
assert client._client.follow_redirects is True
203+
assert client._client.max_redirects == 5
204+
205+
# Verify the client is properly configured with our custom client
206+
assert client._client is custom_client

0 commit comments

Comments
 (0)