Skip to content

Commit c9d035e

Browse files
author
Joel Lee
committed
chore: cleanup and add LICENSE
1 parent 0e26f92 commit c9d035e

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Joel Lee
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

supafunc/supafunc.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,58 @@
11
from typing import Dict
22
import httpx
3-
import asyncio
3+
44

55
class FunctionsClient:
66
def __init__(self, url: str, headers: Dict):
77
self.url = url
88
self.headers = headers
99

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+
"""
1137
try:
1238
headers = invoke_options.get('headers')
1339
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)
1543
is_relay_error = response.headers.get('x-relay-header')
1644
if is_relay_error and is_relay_error == 'true':
1745
return {
1846
"data": None,
1947
"error": response.text
2048
}
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}
2454
except Exception as e:
2555
return {
2656
"data": None,
2757
"error": e
2858
}
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

Comments
 (0)