Skip to content

Commit d729f87

Browse files
committed
fix: pass params to httpx request instead of encoding it in url
1 parent 175f8a0 commit d729f87

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

src/functions/src/supabase_functions/_async/functions_client.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from typing import Any, Dict, Literal, Optional, Union
2-
from urllib.parse import urlencode
32
from warnings import warn
43

5-
from httpx import AsyncClient, HTTPError, Response
4+
from httpx import AsyncClient, HTTPError, Response, QueryParams
65

76
from ..errors import FunctionsHttpError, FunctionsRelayError
87
from ..utils import (
@@ -74,11 +73,16 @@ async def _request(
7473
url: str,
7574
headers: Optional[Dict[str, str]] = None,
7675
json: Optional[Dict[Any, Any]] = None,
76+
params: Optional[QueryParams] = None,
7777
) -> Response:
7878
response = (
79-
await self._client.request(method, url, data=json, headers=headers)
79+
await self._client.request(
80+
method, url, data=json, headers=headers, params=params
81+
)
8082
if isinstance(json, str)
81-
else await self._client.request(method, url, json=json, headers=headers)
83+
else await self._client.request(
84+
method, url, json=json, headers=headers, params=params
85+
)
8286
)
8387
try:
8488
response.raise_for_status()
@@ -122,6 +126,7 @@ async def invoke(
122126
if not is_valid_str_arg(function_name):
123127
raise ValueError("function_name must a valid string value.")
124128
headers = self.headers
129+
params = QueryParams()
125130
body = None
126131
response_type = "text/plain"
127132
url = f"{self.url}/{function_name}"
@@ -139,15 +144,17 @@ async def invoke(
139144
if region.value != "any":
140145
headers["x-region"] = region.value
141146
# Add region as query parameter
142-
url = f"{url}?{urlencode({'forceFunctionRegion': region.value})}"
147+
params = params.set("forceFunctionRegion", region.value)
143148

144149
body = invoke_options.get("body")
145150
if isinstance(body, str):
146151
headers["Content-Type"] = "text/plain"
147152
elif isinstance(body, dict):
148153
headers["Content-Type"] = "application/json"
149154

150-
response = await self._request("POST", url, headers=headers, json=body)
155+
response = await self._request(
156+
"POST", url, headers=headers, json=body, params=params
157+
)
151158
is_relay_error = response.headers.get("x-relay-header")
152159

153160
if is_relay_error and is_relay_error == "true":

src/functions/src/supabase_functions/_sync/functions_client.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from typing import Any, Dict, Literal, Optional, Union
2-
from urllib.parse import urlencode
32
from warnings import warn
43

5-
from httpx import Client, HTTPError, Response
4+
from httpx import Client, HTTPError, Response, QueryParams
65

76
from ..errors import FunctionsHttpError, FunctionsRelayError
87
from ..utils import (
@@ -74,11 +73,14 @@ def _request(
7473
url: str,
7574
headers: Optional[Dict[str, str]] = None,
7675
json: Optional[Dict[Any, Any]] = None,
76+
params: Optional[QueryParams] = None,
7777
) -> Response:
7878
response = (
79-
self._client.request(method, url, data=json, headers=headers)
79+
self._client.request(method, url, data=json, headers=headers, params=params)
8080
if isinstance(json, str)
81-
else self._client.request(method, url, json=json, headers=headers)
81+
else self._client.request(
82+
method, url, json=json, headers=headers, params=params
83+
)
8284
)
8385
try:
8486
response.raise_for_status()
@@ -122,6 +124,7 @@ def invoke(
122124
if not is_valid_str_arg(function_name):
123125
raise ValueError("function_name must a valid string value.")
124126
headers = self.headers
127+
params = QueryParams()
125128
body = None
126129
response_type = "text/plain"
127130
url = f"{self.url}/{function_name}"
@@ -139,15 +142,15 @@ def invoke(
139142
if region.value != "any":
140143
headers["x-region"] = region.value
141144
# Add region as query parameter
142-
url = f"{url}?{urlencode({'forceFunctionRegion': region.value})}"
145+
params = params.set("forceFunctionRegion", region.value)
143146

144147
body = invoke_options.get("body")
145148
if isinstance(body, str):
146149
headers["Content-Type"] = "text/plain"
147150
elif isinstance(body, dict):
148151
headers["Content-Type"] = "application/json"
149152

150-
response = self._request("POST", url, headers=headers, json=body)
153+
response = self._request("POST", url, headers=headers, json=body, params=params)
151154
is_relay_error = response.headers.get("x-relay-header")
152155

153156
if is_relay_error and is_relay_error == "true":

src/functions/tests/_async/test_function_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ async def test_invoke_with_region(client: AsyncFunctionsClient):
104104
# Check that x-region header is present
105105
assert kwargs["headers"]["x-region"] == "us-east-1"
106106
# Check that the URL contains the forceFunctionRegion query parameter
107-
assert "forceFunctionRegion=us-east-1" in args[1]
107+
assert kwargs["params"]["forceFunctionRegion"] == "us-east-1"
108108

109109

110110
async def test_invoke_with_region_string(client: AsyncFunctionsClient):
@@ -125,7 +125,7 @@ async def test_invoke_with_region_string(client: AsyncFunctionsClient):
125125
# Check that x-region header is present
126126
assert kwargs["headers"]["x-region"] == "us-east-1"
127127
# Check that the URL contains the forceFunctionRegion query parameter
128-
assert "forceFunctionRegion=us-east-1" in args[1]
128+
assert kwargs["params"]["forceFunctionRegion"] == "us-east-1"
129129

130130

131131
async def test_invoke_with_http_error(client: AsyncFunctionsClient):

src/functions/tests/_sync/test_function_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def test_invoke_with_region(client: SyncFunctionsClient):
9898
# Check that x-region header is present
9999
assert kwargs["headers"]["x-region"] == "us-east-1"
100100
# Check that the URL contains the forceFunctionRegion query parameter
101-
assert "forceFunctionRegion=us-east-1" in args[1]
101+
assert kwargs["params"]["forceFunctionRegion"] == "us-east-1"
102102

103103

104104
def test_invoke_with_region_string(client: SyncFunctionsClient):
@@ -117,7 +117,7 @@ def test_invoke_with_region_string(client: SyncFunctionsClient):
117117
# Check that x-region header is present
118118
assert kwargs["headers"]["x-region"] == "us-east-1"
119119
# Check that the URL contains the forceFunctionRegion query parameter
120-
assert "forceFunctionRegion=us-east-1" in args[1]
120+
assert kwargs["params"]["forceFunctionRegion"] == "us-east-1"
121121

122122

123123
def test_invoke_with_http_error(client: SyncFunctionsClient):

0 commit comments

Comments
 (0)