Skip to content

Commit 4b8a134

Browse files
committed
fix: add single instance of client instantiation
1 parent e2a59aa commit 4b8a134

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

supafunc/_async/functions_client.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ def __init__(self, url: str, headers: Dict):
1313
"User-Agent": f"supabase-py/functions-py v{__version__}",
1414
**headers,
1515
}
16+
self._client = AsyncClient(base_url=self.url, headers=self.headers)
1617

1718
async def _request(
1819
self,
1920
method: Literal["GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"],
2021
url: str,
2122
headers: Union[Dict[str, str], None] = None,
22-
json: Optional[dict[Any, Any]] = None,
23+
json: Optional[Dict[Any, Any]] = None,
2324
) -> Response:
24-
async with AsyncClient(base_url=self.url, headers=self.headers) as client:
25-
response = await client.request(method, url, json=json, headers=headers)
26-
try:
27-
response.raise_for_status()
28-
except HTTPError as exc:
29-
raise FunctionsHttpError(
30-
response.json().get("error")
31-
or f"An error occurred while requesting your edge function at {exc.request.url!r}."
32-
) from exc
33-
34-
return response
25+
response = await self._client.request(method, url, json=json, headers=headers)
26+
try:
27+
response.raise_for_status()
28+
except HTTPError as exc:
29+
raise FunctionsHttpError(
30+
response.json().get("error")
31+
or f"An error occurred while requesting your edge function at {exc.request.url!r}."
32+
) from exc
33+
34+
return response
3535

3636
def set_auth(self, token: str) -> None:
3737
"""Updates the authorization header

supafunc/_sync/functions_client.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ def __init__(self, url: str, headers: Dict):
1313
"User-Agent": f"supabase-py/functions-py v{__version__}",
1414
**headers,
1515
}
16+
self._client = SyncClient(base_url=self.url, headers=self.headers)
1617

1718
def _request(
1819
self,
1920
method: Literal["GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"],
2021
url: str,
2122
headers: Union[Dict[str, str], None] = None,
22-
json: Optional[dict[Any, Any]] = None,
23+
json: Optional[Dict[Any, Any]] = None,
2324
) -> Response:
24-
with SyncClient(base_url=self.url, headers=self.headers) as client:
25-
response = client.request(method, url, json=json, headers=headers)
26-
try:
27-
response.raise_for_status()
28-
except HTTPError as exc:
29-
raise FunctionsHttpError(
30-
response.json().get("error")
31-
or f"An error occurred while requesting your edge function at {exc.request.url!r}."
32-
)
33-
34-
return response
25+
response = self._client.request(method, url, json=json, headers=headers)
26+
try:
27+
response.raise_for_status()
28+
except HTTPError as exc:
29+
raise FunctionsHttpError(
30+
response.json().get("error")
31+
or f"An error occurred while requesting your edge function at {exc.request.url!r}."
32+
) from exc
33+
34+
return response
3535

3636
def set_auth(self, token: str) -> None:
3737
"""Updates the authorization header
@@ -44,7 +44,9 @@ def set_auth(self, token: str) -> None:
4444

4545
self.headers["Authorization"] = f"Bearer {token}"
4646

47-
def invoke(self, function_name: str, invoke_options: Optional[Dict] = None) -> Dict:
47+
def invoke(
48+
self, function_name: str, invoke_options: Optional[Dict] = None
49+
) -> Dict:
4850
"""Invokes a function
4951
5052
Parameters

0 commit comments

Comments
 (0)