mirror of
https://github.com/eliasrenman/gardentron.git
synced 2026-03-16 20:46:07 +01:00
feat: added body support
This commit is contained in:
@@ -39,16 +39,41 @@ class Endpoint:
|
|||||||
if endpoint != self.path and self.path != '*' or method != self.method:
|
if endpoint != self.path and self.path != '*' or method != self.method:
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
|
body = get_body(request)
|
||||||
|
if body:
|
||||||
|
kwargs = dict(**kwargs, body=body)
|
||||||
|
|
||||||
val = function(instance, *args, **kwargs)
|
val = function(instance, *args, **kwargs)
|
||||||
respond(cl, 200, val)
|
respond(cl, 200, val)
|
||||||
return val
|
return val
|
||||||
except HttpError as e:
|
except HttpError as e:
|
||||||
respond(cl, e.status,
|
respond(cl, e.status,
|
||||||
e.message)
|
e.message)
|
||||||
|
except Exception as e:
|
||||||
|
print("Some other unkown error occured:", e)
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Please note that this only supports content-type 'application/json'
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def get_body(request: str):
|
||||||
|
req = request.split("\r\n")
|
||||||
|
content_type_index = -10
|
||||||
|
for index in range(len(req)):
|
||||||
|
item = req[index]
|
||||||
|
|
||||||
|
if item.find("Content-Type") != -1:
|
||||||
|
if not item.find("application/json"):
|
||||||
|
return
|
||||||
|
content_type_index = index
|
||||||
|
if (content_type_index + 4) == index:
|
||||||
|
return item
|
||||||
|
|
||||||
|
|
||||||
class ServerHandler(object):
|
class ServerHandler(object):
|
||||||
def __init__(self, s: socket.socket):
|
def __init__(self, s: socket.socket):
|
||||||
not_found_endpoints = (self.__post_not_found,
|
not_found_endpoints = (self.__post_not_found,
|
||||||
|
|||||||
Reference in New Issue
Block a user