Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Dict, Literal, Optional, Union
from urllib.parse import urlencode
from warnings import warn

from httpx import AsyncClient, HTTPError, Response
Expand Down Expand Up @@ -123,6 +124,8 @@ async def invoke(
headers = self.headers
body = None
response_type = "text/plain"
url = f"{self.url}/{function_name}"

if invoke_options is not None:
headers.update(invoke_options.get("headers", {}))
response_type = invoke_options.get("responseType", "text/plain")
Expand All @@ -135,16 +138,16 @@ async def invoke(

if region.value != "any":
headers["x-region"] = region.value
# Add region as query parameter
url = f"{url}?{urlencode({'forceFunctionRegion': region.value})}"

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

response = await self._request(
"POST", f"{self.url}/{function_name}", headers=headers, json=body
)
response = await self._request("POST", url, headers=headers, json=body)
is_relay_error = response.headers.get("x-relay-header")

if is_relay_error and is_relay_error == "true":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Dict, Literal, Optional, Union
from urllib.parse import urlencode
from warnings import warn

from httpx import Client, HTTPError, Response
Expand Down Expand Up @@ -123,6 +124,8 @@ def invoke(
headers = self.headers
body = None
response_type = "text/plain"
url = f"{self.url}/{function_name}"

if invoke_options is not None:
headers.update(invoke_options.get("headers", {}))
response_type = invoke_options.get("responseType", "text/plain")
Expand All @@ -135,16 +138,16 @@ def invoke(

if region.value != "any":
headers["x-region"] = region.value
# Add region as query parameter
url = f"{url}?{urlencode({'forceFunctionRegion': region.value})}"

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

response = self._request(
"POST", f"{self.url}/{function_name}", headers=headers, json=body
)
response = self._request("POST", url, headers=headers, json=body)
is_relay_error = response.headers.get("x-relay-header")

if is_relay_error and is_relay_error == "true":
Expand Down
10 changes: 8 additions & 2 deletions src/functions/tests/_async/test_function_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ async def test_invoke_with_region(client: AsyncFunctionsClient):

await client.invoke("test-function", {"region": FunctionRegion("us-east-1")})

_, kwargs = mock_request.call_args
args, kwargs = mock_request.call_args
# Check that x-region header is present
assert kwargs["headers"]["x-region"] == "us-east-1"
# Check that the URL contains the forceFunctionRegion query parameter
assert "forceFunctionRegion=us-east-1" in args[1]


async def test_invoke_with_region_string(client: AsyncFunctionsClient):
Expand All @@ -118,8 +121,11 @@ async def test_invoke_with_region_string(client: AsyncFunctionsClient):
with pytest.warns(UserWarning, match=r"Use FunctionRegion\(us-east-1\)"):
await client.invoke("test-function", {"region": "us-east-1"})

_, kwargs = mock_request.call_args
args, kwargs = mock_request.call_args
# Check that x-region header is present
assert kwargs["headers"]["x-region"] == "us-east-1"
# Check that the URL contains the forceFunctionRegion query parameter
assert "forceFunctionRegion=us-east-1" in args[1]


async def test_invoke_with_http_error(client: AsyncFunctionsClient):
Expand Down
10 changes: 8 additions & 2 deletions src/functions/tests/_sync/test_function_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ def test_invoke_with_region(client: SyncFunctionsClient):

client.invoke("test-function", {"region": FunctionRegion("us-east-1")})

_, kwargs = mock_request.call_args
args, kwargs = mock_request.call_args
# Check that x-region header is present
assert kwargs["headers"]["x-region"] == "us-east-1"
# Check that the URL contains the forceFunctionRegion query parameter
assert "forceFunctionRegion=us-east-1" in args[1]


def test_invoke_with_region_string(client: SyncFunctionsClient):
Expand All @@ -110,8 +113,11 @@ def test_invoke_with_region_string(client: SyncFunctionsClient):
with pytest.warns(UserWarning, match=r"Use FunctionRegion\(us-east-1\)"):
client.invoke("test-function", {"region": "us-east-1"})

_, kwargs = mock_request.call_args
args, kwargs = mock_request.call_args
# Check that x-region header is present
assert kwargs["headers"]["x-region"] == "us-east-1"
# Check that the URL contains the forceFunctionRegion query parameter
assert "forceFunctionRegion=us-east-1" in args[1]


def test_invoke_with_http_error(client: SyncFunctionsClient):
Expand Down