1
- from httpx import HTTPError , Response
2
1
from typing import Any , Dict , Literal , Optional , Union
3
2
4
- from .. errors import FunctionsHttpError , FunctionsRelayError
3
+ from httpx import HTTPError , Response
5
4
5
+ from ..errors import FunctionsHttpError , FunctionsRelayError
6
6
from ..utils import SyncClient , __version__
7
7
8
8
9
9
class SyncFunctionsClient :
10
10
def __init__ (self , url : str , headers : Dict ):
11
11
self .url = url
12
12
self .headers = {
13
- "User-Agent" : f"supabase-py/storage3 v{ __version__ } " ,
13
+ "User-Agent" : f"supabase-py/functions-py v{ __version__ } " ,
14
14
** headers ,
15
15
}
16
16
@@ -25,8 +25,11 @@ def _request(
25
25
response = client .request (method , url , json = json , headers = headers )
26
26
try :
27
27
response .raise_for_status ()
28
- except HTTPError :
29
- raise Exception ({** response .json (), "statusCode" : response .status_code })
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
+ )
30
33
31
34
return response
32
35
@@ -41,7 +44,7 @@ def set_auth(self, token: str) -> None:
41
44
42
45
self .headers ["Authorization" ] = f"Bearer { token } "
43
46
44
- def invoke (self , function_name : str , invoke_options : Dict = {} ) -> Dict :
47
+ def invoke (self , function_name : str , invoke_options : Optional [ Dict ] = None ) -> Dict :
45
48
"""Invokes a function
46
49
47
50
Parameters
@@ -55,16 +58,22 @@ def invoke(self, function_name: str, invoke_options: Dict = {}) -> Dict:
55
58
Returns
56
59
-------
57
60
Dict
58
- Dictionary with data and/or error message
61
+ Dictionary with data
59
62
"""
60
- headers = {** self .headers , ** invoke_options .get ("headers" , {})}
61
- body = invoke_options .get ("body" )
63
+ headers = self .headers
64
+ if invoke_options is not None :
65
+ headers .update (invoke_options .get ("headers" , {}))
66
+
67
+ body = invoke_options .get ("body" ) if invoke_options else None
68
+ response_type = (
69
+ invoke_options .get ("responseType" ) if invoke_options else "text/plain"
70
+ )
71
+
62
72
if type (body ) == str :
63
73
headers ["Content-Type" ] = "text/plain"
64
74
elif type (body ) == dict :
65
75
headers ["Content-Type" ] = "application/json"
66
76
67
- response_type = invoke_options .get ("responseType" )
68
77
response = self ._request (
69
78
"POST" , f"{ self .url } /{ function_name } " , headers = headers , json = body
70
79
)
@@ -73,9 +82,6 @@ def invoke(self, function_name: str, invoke_options: Dict = {}) -> Dict:
73
82
if is_relay_error and is_relay_error == "true" :
74
83
raise FunctionsRelayError (response .json ().get ("error" ))
75
84
76
- if response .is_success is not True :
77
- raise FunctionsHttpError (response .json ().get ("error" ))
78
-
79
85
if response_type == "json" :
80
86
data = response .json ()
81
87
else :
0 commit comments