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
|
request: str = instance.__request # type: ignore
|
||||||
cl: socket.socket = instance.__cl # 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
|
return
|
||||||
try:
|
try:
|
||||||
val = function(instance, *args, **kwargs)
|
val = function(instance, *args, **kwargs)
|
||||||
@@ -65,23 +66,25 @@ class ServerHandler(object):
|
|||||||
self.__cl = cl
|
self.__cl = cl
|
||||||
method_list = dir(self.__class__)
|
method_list = dir(self.__class__)
|
||||||
alreadyReturned = False
|
alreadyReturned = False
|
||||||
for endpoint in method_list:
|
for method in method_list:
|
||||||
|
|
||||||
if endpoint.startswith('_') is True:
|
if method.startswith('_') is True:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
func = getattr(self.__class__, endpoint)
|
func = getattr(self.__class__, method)
|
||||||
|
|
||||||
if not callable(func):
|
if not callable(func):
|
||||||
continue
|
continue
|
||||||
|
if not is_decorated(func):
|
||||||
|
continue
|
||||||
|
|
||||||
result = func(self)
|
result = func(self)
|
||||||
if result:
|
if result:
|
||||||
alreadyReturned = True
|
alreadyReturned = True
|
||||||
break
|
break
|
||||||
if (not alreadyReturned):
|
if (not alreadyReturned):
|
||||||
for endpoint in not_found_endpoints:
|
for method in not_found_endpoints:
|
||||||
result = endpoint()
|
result = method()
|
||||||
if result:
|
if result:
|
||||||
break
|
break
|
||||||
|
|
||||||
@@ -112,3 +115,11 @@ class ServerHandler(object):
|
|||||||
@Endpoint('*', 'DELETE')
|
@Endpoint('*', 'DELETE')
|
||||||
def __delete_not_found(self):
|
def __delete_not_found(self):
|
||||||
raise HttpError(404, {'status': 'Not Found'})
|
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):
|
class Handler(ServerHandler):
|
||||||
|
|
||||||
@Endpoint('light/on', 'POST')
|
@Endpoint('/light/on', 'POST')
|
||||||
def light_on(self):
|
def light_on(self):
|
||||||
print("led on")
|
print("led on")
|
||||||
led.value(1)
|
led.value(1)
|
||||||
return {"led": "on"}
|
return {"led": "on"}
|
||||||
|
|
||||||
@Endpoint('light/off', 'POST')
|
@Endpoint('/light/off', 'POST')
|
||||||
def light_off(self):
|
def light_off(self):
|
||||||
print("led off")
|
print("led off")
|
||||||
led.value(0)
|
led.value(0)
|
||||||
|
|||||||
Reference in New Issue
Block a user