Skip to content

Commit 30042a9

Browse files
committed
monkey patch the execute method to make it sync
1 parent 5e65ebf commit 30042a9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Diff for: supabase_py/lib/query_builder.py

+32
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
1+
from typing import Any, Dict
2+
3+
import requests
14
from httpx import AsyncClient
25
from postgrest_py.client import PostgrestClient
6+
from postgrest_py.request_builder import QueryRequestBuilder
37

48
from .realtime_client import SupabaseRealtimeClient
59

610

11+
def _execute_monkey_patch(self) -> Dict[str, Any]:
12+
"""Temporary method to enable syncronous client code."""
13+
method: str = self.http_method.lower()
14+
if method == "get":
15+
func = requests.get
16+
elif method == "post":
17+
func = requests.post
18+
elif method == "put":
19+
func = requests.put
20+
elif method == "patch":
21+
func = requests.patch
22+
elif method == "delete":
23+
func = requests.delete
24+
url: str = str(self.session.base_url).rstrip("/")
25+
query: str = str(self.session.params)
26+
response = func(f"{url}?{query}", headers=self.session.headers)
27+
return {
28+
"data": response.json(),
29+
"status_code": response.status_code,
30+
}
31+
32+
33+
# NOTE(fedden): Here we monkey patch the otherwise async method and use the
34+
# requests module instead. Hopefully cleans things up a little
35+
# for the user as they are now not bound to async methods.
36+
QueryRequestBuilder.execute = _execute_monkey_patch
37+
38+
739
class SupabaseQueryBuilder(PostgrestClient):
840
def __init__(self, url, headers, schema, realtime, table):
941
"""

0 commit comments

Comments
 (0)