Skip to content

Commit

Permalink
current
Browse files Browse the repository at this point in the history
  • Loading branch information
JungeWerther committed Apr 25, 2024
1 parent 6713361 commit 32196ed
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 13 deletions.
81 changes: 68 additions & 13 deletions connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def get_service_role_key(self):


class Callables():
"""Class to define explicit methods callable from config"""
"""Class to define explicit methods callable from config.
Important: you need to add type hints and return signatures.
"""
def __init__(self, config=None, debug=False):
self.config = config
self.debug = debug
Expand Down Expand Up @@ -132,7 +134,8 @@ def censor(self, value: str):
@add_error("Unable to parse response (hint: change the Content-Type)", 472)
def parse_doctype(self, res: Response, doctype: str) -> dict | str:
"""Parse a doctype from a string"""

print("Parsing doctype:", doctype)
print("Response:", res.text[:100])
match doctype:
case "application/xml":
return parsing.parse_xml(res.text)
Expand All @@ -147,6 +150,7 @@ def parse_doctype(self, res: Response, doctype: str) -> dict | str:
case _:
return res.json()

@add_error("Error calling function", 472)
def _request(self,
url=None,
base_url=None,
Expand All @@ -157,7 +161,7 @@ def _request(self,
session: Session=None,
sleep=0,
debug=False,
):
) -> dict | str | list:
"""send a request with the specified parameters
TODO: implement POST, PUT methods.
"""
Expand Down Expand Up @@ -200,8 +204,20 @@ def _request(self,
time.sleep(sleep)

if method == "GET":
res = session.get(url)
print("GET request")
try:
res = session.get(url)
except Exception as e:
raise e

if res is None:
print("No response from server")
else:
print("Response:")
print(type(res))

elif method == "POST":
print("POST request")
res = session.post(url)
else:
raise ValueError(f"{method} needs implementation")
Expand All @@ -213,7 +229,7 @@ def _request(self,
)
with open("./debug.html", "wt") as f:
f.write(res.text)

return self.parse_doctype(res, headers["Content-Type"])

def new_session(self, auth, headers):
Expand All @@ -240,18 +256,21 @@ def _fromFile(self, path):

return obj

def _test(self, base_url, rel_url=""):
def _test(self, base_url, rel_url="") -> str:
"""Test function. Can you pass base_url and rel_url to get url?"""
url = base_url + rel_url
return url

# TODO: figure out kwargs situation. (?)
class Config():
"""Class used as target for configuration settings. Define special methods here."""
def __init__(self, **kwargs):

# initialize config with any passed kwargs
for k, v in kwargs.items():
setattr(self, k, v)
# for k, v in kwargs.items():
# setattr(self, k, v)

# does this work?
self.add_attributes(**kwargs)

def add_attributes(self, **kwargs):
"""Add attributes to config"""
Expand All @@ -263,10 +282,11 @@ class Writeables():
def __init__(self, config=None, debug=False):
self.config = config
self.debug = debug
self.decoded = self.config.decoded or None
self.metadata = self.config.metadata or None
self.decoded = getattr(self.config, "decoded", None)
self.metadata = getattr(self.config, "metadata", None)

def toSupa_(self, data, table: str, overwrite: bool=False):
@add_error("Error calling function", 472)
def toSupa_(self, data, table: str, schema: str = "etl", overwrite: bool=False) -> None:
"""
Upsert data to supabase table. You need a supabase session cookie.
Let me know if you have any trouble here, I can help.
Expand All @@ -275,7 +295,7 @@ def toSupa_(self, data, table: str, overwrite: bool=False):
if overwrite: print("[WARNING] overwrite needs implementation")
if self.decoded is None: raise ValueError("invalid session")

client = AnyClient(self.decoded.token, schema="etl").client
client = AnyClient(self.decoded.token, schema=schema).client
user_tld = client.from_("organization").select("tld").single().execute().data["tld"]
newclient = AnyClient(self.decoded.token, schema=user_tld).client

Expand Down Expand Up @@ -462,6 +482,38 @@ def key_writeable(self, key):
"""
return (key in dir(self.writeables)) and key[-1]=="_" and key[-2]!="_"

def get_callables(self):
"""Return a list of callable functions in self.functions"""
return [f for f in dir(self.functions) if self.key_callable(f)]

def get_writeables(self):
"""Return a list of writeable functions in self.writeables"""
return [f for f in dir(self.writeables) if self.key_writeable(f)]

def get_callable_signature(self, func_name):
"""Return the signature of a callable function"""
return inspect.signature(getattr(self.functions, func_name))

def get_writeable_signature(self, func_name):
"""Return the signature of a writeable function"""
return inspect.signature(getattr(self.writeables, func_name))

def get_callable_return_type(self, func_name):
"""DEPRECIATED: Return the return type of a callable function"""
return inspect.getfullargspec(getattr(self.functions, func_name)).annotations.get("return", None)

def get_writeable_return_type(self, func_name):
"""DEPRECTIATED: Return the return type of a writeable function"""
return inspect.getfullargspec(getattr(self.writeables, func_name)).annotations.get("return", None)

def get_callable_description(self, func_name):
"""Return the description of a callable function"""
return inspect.getdoc(getattr(self.functions, func_name))

def get_writeable_description(self, func_name):
"""Return the description of a writeable function"""
return inspect.getdoc(getattr(self.writeables, func_name))

def trimargs(self, func):
"""Returns a list of arguments that can be passed to func."""

Expand Down Expand Up @@ -489,6 +541,7 @@ def trimargs(self, func):

# return args that can be passed to func.
return iargs

def val_data(self, value: str):
"""
Check if value corresponds to an escaped callable.
Expand Down Expand Up @@ -594,10 +647,12 @@ def evaluate(self,
match iargs:
case dict():
do = self.writeables.caller(func, **iargs)

print(do)
case list():
for i in iargs:
do = self.writeables.caller(func, **i)

print(do)

# handle callables
Expand Down
2 changes: 2 additions & 0 deletions errors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import wraps
"""Error classes and decorators for the APIConnector module."""

# custom error class for all api-connector related errors
Expand All @@ -13,6 +14,7 @@ class APIConnectorError(BaseException):
def add_error(message, code):
"""Decorator to add error handling to a function."""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
Expand Down
31 changes: 31 additions & 0 deletions socket.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
</head>
<body>
<h1>WebSocket Chat</h1>
<form action="" onsubmit="sendMessage(event)">
<input type="text" id="messageText" autocomplete="off"/>
<button>Send</button>
</form>
<ul id='messages'>
</ul>
<script>
var ws = new WebSocket("ws://localhost:10432/ws");
ws.onmessage = function(event) {
var messages = document.getElementById('messages')
var message = document.createElement('li')
var content = document.createTextNode(event.data)
message.appendChild(content)
messages.appendChild(message)
};
function sendMessage(event) {
var input = document.getElementById("messageText")
ws.send(input.value)
input.value = ''
event.preventDefault()
}
</script>
</body>
</html>
22 changes: 22 additions & 0 deletions socket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing_extensions import Unpack
from pydantic import BaseModel, ConfigDict

class Endpoint():
"""
Endpoint class
"""
html: str = None

def __init__(self, html: str = None) -> None:
self.html = html or self.get_html()

def get_html(self):
with open("./Connect/socket.html", "r") as f:
return f.read()

class Socket():
"""
Socket class
"""
def __init__(self, ) -> None:
pass

0 comments on commit 32196ed

Please sign in to comment.