|
1 | 1 | from typing import Dict
|
2 | 2 | import httpx
|
3 |
| -import asyncio |
| 3 | + |
4 | 4 |
|
5 | 5 | class FunctionsClient:
|
6 | 6 | def __init__(self, url: str, headers: Dict):
|
7 | 7 | self.url = url
|
8 | 8 | self.headers = headers
|
9 | 9 |
|
10 |
| - async def invoke(self, function_name: str, invoke_options: Dict): |
| 10 | + def set_auth(self, token: str) -> None: |
| 11 | + """Updates the authorization header |
| 12 | +
|
| 13 | + Parameters |
| 14 | + ---------- |
| 15 | + token : str |
| 16 | + the new jwt token sent in the authorisation header |
| 17 | + """ |
| 18 | + |
| 19 | + self.headers["Authorization"] = f"Bearer {token}" |
| 20 | + |
| 21 | + async def invoke(self, function_name: str, invoke_options: Dict) -> Dict: |
| 22 | + """Invokes a function |
| 23 | +
|
| 24 | + Parameters |
| 25 | + ---------- |
| 26 | + function_name : the name of the function to invoke |
| 27 | + invoke_options : object with the following properties |
| 28 | + `headers`: object representing the headers to send with the request |
| 29 | + `body`: the body of the request |
| 30 | + `responseType`: how the response should be parsed. The default is `json` |
| 31 | +
|
| 32 | + Returns |
| 33 | + ------- |
| 34 | + Dict |
| 35 | + Dictionary with data and/or error message |
| 36 | + """ |
11 | 37 | try:
|
12 | 38 | headers = invoke_options.get('headers')
|
13 | 39 | body = invoke_options.get('body')
|
14 |
| - response = httpx.post(f"{self.url}/{function_name}", headers=headers) |
| 40 | + response_type = invoke_options.get('responseType') |
| 41 | + response = httpx.post( |
| 42 | + f"{self.url}/{function_name}", headers=headers) |
15 | 43 | is_relay_error = response.headers.get('x-relay-header')
|
16 | 44 | if is_relay_error and is_relay_error == 'true':
|
17 | 45 | return {
|
18 | 46 | "data": None,
|
19 | 47 | "error": response.text
|
20 | 48 | }
|
21 |
| - # TODO: Convert type accordingly |
22 |
| - |
23 |
| - return {"data": response.data, "error": None} |
| 49 | + if response_type == 'json': |
| 50 | + data = response.json() |
| 51 | + else: |
| 52 | + data = response.data |
| 53 | + return {"data": data, "error": None} |
24 | 54 | except Exception as e:
|
25 | 55 | return {
|
26 | 56 | "data": None,
|
27 | 57 | "error": e
|
28 | 58 | }
|
29 |
| - |
30 |
| -async def run_func(): |
31 |
| - fc = FunctionsClient("https://aipvgapcvmokjooimzlr.functions.supabase.co", {}) |
32 |
| - res = await fc.invoke("payment-sheet", {"responseType": "json"}) |
33 |
| - |
34 |
| -if __name__ == "__main__": |
35 |
| - asyncio.run(run_func()) |
36 |
| - |
37 |
| - |
|
0 commit comments