Skip to content

[wip] refactor: accept httpx client #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions supabase_functions/_async/functions_client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from typing import Any, Dict, Literal, Optional, Union
from warnings import warn

from httpx import HTTPError, Response
from httpx import AsyncClient, HTTPError, Response

from ..errors import FunctionsHttpError, FunctionsRelayError
from ..utils import (
AsyncClient,
FunctionRegion,
is_http_url,
is_valid_jwt,
Expand All @@ -19,8 +18,9 @@ def __init__(
self,
url: str,
headers: Dict,
timeout: int,
verify: bool = True,
httpx: Optional[AsyncClient] = None,
timeout: Optional[int] = None,
verify: Optional[bool] = None,
proxy: Optional[str] = None,
):
if not is_http_url(url):
Expand All @@ -30,15 +30,33 @@ def __init__(
"User-Agent": f"supabase-py/functions-py v{__version__}",
**headers,
}
self._client = AsyncClient(
base_url=self.url,
headers=self.headers,
verify=bool(verify),
timeout=int(abs(timeout)),
proxy=proxy,
follow_redirects=True,
http2=True,
)

if timeout is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of deprecating these and suggesting to handle this in your httpx client yourself.

warn("The 'timeout' parameter is deprecated. Please configure it in the httpx client instead.", DeprecationWarning, stacklevel=2)
if verify is not None:
warn("The 'verify' parameter is deprecated. Please configure it in the httpx client instead.", DeprecationWarning, stacklevel=2)
if proxy is not None:
warn("The 'proxy' parameter is deprecated. Please configure it in the httpx client instead.", DeprecationWarning, stacklevel=2)

self.verify = bool(verify) if verify is not None else True
self.timeout = int(abs(timeout)) if timeout is not None else 60

if httpx is not None:
self._client = httpx
self._client.base_url = self.url
self._client.headers = self.headers
self._client.follow_redirects = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe these params should be set besides the base_url and headers. The others should be handled by the developer when they decide to use their own httpx client.

self._client.http2 = True
else:
self._client = AsyncClient(
base_url=self.url,
headers=self.headers,
verify=self.verify,
timeout=self.timeout,
proxy=proxy,
follow_redirects=True,
http2=True,
)

async def _request(
self,
Expand Down
26 changes: 25 additions & 1 deletion tests/_async/test_function_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import AsyncMock, Mock, patch

import pytest
from httpx import HTTPError, Response, Timeout
from httpx import AsyncClient, HTTPError, Response, Timeout

# Import the class to test
from supabase_functions import AsyncFunctionsClient
Expand Down Expand Up @@ -197,3 +197,27 @@ async def test_invoke_with_json_body(client: AsyncFunctionsClient):

_, kwargs = mock_request.call_args
assert kwargs["headers"]["Content-Type"] == "application/json"


async def test_init_with_httpx_client():
# Create a custom httpx client with specific options
custom_client = AsyncClient(
timeout=Timeout(30), follow_redirects=True, max_redirects=5
)

# Initialize the functions client with the custom httpx client
client = AsyncFunctionsClient(
url="https://example.com",
headers={"Authorization": "Bearer token"},
timeout=30,
httpx=custom_client,
)

# Verify the custom client options are preserved
assert client._client.timeout == Timeout(30)
assert client._client.verify is True
assert client._client.follow_redirects is True
assert client._client.max_redirects == 5

# Verify the client is properly configured with our custom client
assert client._client is custom_client
Loading