mirror of
https://github.com/eliasrenman/gardentron.git
synced 2026-03-16 20:46:07 +01:00
feat: fixed server to only use endpoint decorated methods
This commit is contained in:
@@ -34,8 +34,9 @@ class Endpoint:
|
||||
|
||||
request: str = instance.__request # type: ignore
|
||||
cl: socket.socket = instance.__cl # type: ignore
|
||||
# TODO: Improve find checker here to be more precise when dealing with requests.
|
||||
if request.find(self.path) == -1 and self.path != '*' or request.find(self.method) == -1:
|
||||
|
||||
method, endpoint = split_request(request)
|
||||
if endpoint != self.path and self.path != '*' or method != self.method:
|
||||
return
|
||||
try:
|
||||
val = function(instance, *args, **kwargs)
|
||||
@@ -65,23 +66,25 @@ class ServerHandler(object):
|
||||
self.__cl = cl
|
||||
method_list = dir(self.__class__)
|
||||
alreadyReturned = False
|
||||
for endpoint in method_list:
|
||||
for method in method_list:
|
||||
|
||||
if endpoint.startswith('_') is True:
|
||||
if method.startswith('_') is True:
|
||||
continue
|
||||
|
||||
func = getattr(self.__class__, endpoint)
|
||||
func = getattr(self.__class__, method)
|
||||
|
||||
if not callable(func):
|
||||
continue
|
||||
if not is_decorated(func):
|
||||
continue
|
||||
|
||||
result = func(self)
|
||||
if result:
|
||||
alreadyReturned = True
|
||||
break
|
||||
if (not alreadyReturned):
|
||||
for endpoint in not_found_endpoints:
|
||||
result = endpoint()
|
||||
for method in not_found_endpoints:
|
||||
result = method()
|
||||
if result:
|
||||
break
|
||||
|
||||
@@ -112,3 +115,11 @@ class ServerHandler(object):
|
||||
@Endpoint('*', 'DELETE')
|
||||
def __delete_not_found(self):
|
||||
raise HttpError(404, {'status': 'Not Found'})
|
||||
|
||||
|
||||
def split_request(req: str):
|
||||
return req.split(' ')[:2]
|
||||
|
||||
|
||||
def is_decorated(func):
|
||||
return hasattr(func, '__wrapped__') or func.__name__ not in globals()
|
||||
|
||||
@@ -18,13 +18,13 @@ def initalize_app():
|
||||
|
||||
class Handler(ServerHandler):
|
||||
|
||||
@Endpoint('light/on', 'POST')
|
||||
@Endpoint('/light/on', 'POST')
|
||||
def light_on(self):
|
||||
print("led on")
|
||||
led.value(1)
|
||||
return {"led": "on"}
|
||||
|
||||
@Endpoint('light/off', 'POST')
|
||||
@Endpoint('/light/off', 'POST')
|
||||
def light_off(self):
|
||||
print("led off")
|
||||
led.value(0)
|
||||
|
||||
Reference in New Issue
Block a user