Skip to content

Commit 567d2f2

Browse files
committed
refactor: accept httpx client
1 parent 2675964 commit 567d2f2

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

supabase_functions/_async/functions_client.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from typing import Any, Dict, Literal, Optional, Union
22
from warnings import warn
33

4-
from httpx import HTTPError, Response
4+
from httpx import AsyncClient, HTTPError, Response
55

66
from ..errors import FunctionsHttpError, FunctionsRelayError
77
from ..utils import (
8-
AsyncClient,
98
FunctionRegion,
109
is_http_url,
1110
is_valid_jwt,
@@ -19,8 +18,9 @@ def __init__(
1918
self,
2019
url: str,
2120
headers: Dict,
22-
timeout: int,
23-
verify: bool = True,
21+
httpx: Optional[AsyncClient] = None,
22+
timeout: Optional[int] = None,
23+
verify: Optional[bool] = None,
2424
proxy: Optional[str] = None,
2525
):
2626
if not is_http_url(url):
@@ -30,15 +30,33 @@ def __init__(
3030
"User-Agent": f"supabase-py/functions-py v{__version__}",
3131
**headers,
3232
}
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-
)
33+
34+
if timeout is not None:
35+
warn("The 'timeout' parameter is deprecated. Please configure it in the httpx client instead.", DeprecationWarning, stacklevel=2)
36+
if verify is not None:
37+
warn("The 'verify' parameter is deprecated. Please configure it in the httpx client instead.", DeprecationWarning, stacklevel=2)
38+
if proxy is not None:
39+
warn("The 'proxy' parameter is deprecated. Please configure it in the httpx client instead.", DeprecationWarning, stacklevel=2)
40+
41+
self.verify = bool(verify) if verify is not None else True
42+
self.timeout = int(abs(timeout)) if timeout is not None else 60
43+
44+
if httpx is not None:
45+
self._client = httpx
46+
self._client.base_url = self.url
47+
self._client.headers = self.headers
48+
self._client.follow_redirects = True
49+
self._client.http2 = True
50+
else:
51+
self._client = AsyncClient(
52+
base_url=self.url,
53+
headers=self.headers,
54+
verify=self.verify,
55+
timeout=self.timeout,
56+
proxy=proxy,
57+
follow_redirects=True,
58+
http2=True,
59+
)
4260

4361
async def _request(
4462
self,

tests/_async/test_function_client.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest.mock import AsyncMock, Mock, patch
22

33
import pytest
4-
from httpx import HTTPError, Response, Timeout
4+
from httpx import AsyncClient, HTTPError, Response, Timeout
55

66
# Import the class to test
77
from supabase_functions import AsyncFunctionsClient
@@ -197,3 +197,27 @@ 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+
httpx=custom_client,
214+
)
215+
216+
# Verify the custom client options are preserved
217+
assert client._client.timeout == Timeout(30)
218+
assert client._client.verify is True
219+
assert client._client.follow_redirects is True
220+
assert client._client.max_redirects == 5
221+
222+
# Verify the client is properly configured with our custom client
223+
assert client._client is custom_client

0 commit comments

Comments
 (0)