Skip to content

Commit 93cd2ff

Browse files
author
Joel Collins
committed
Fixed arg handling in request handlers
1 parent 376db07 commit 93cd2ff

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

src/labthings/server/view/__init__.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,6 @@ def dispatch_request(self, *args, **kwargs):
9393
# Flask should ensure this is assersion never fails
9494
assert meth is not None, f"Unimplemented method {request.method!r}"
9595

96-
# Inject request arguments if an args schema is defined
97-
if self.get_args():
98-
meth = use_args(self.get_args())(meth)
99-
100-
# Marhal response if a response schema is defines
101-
if self.get_schema():
102-
meth = marshal_with(self.get_schema())(meth)
103-
10496
# Generate basic response
10597
return self.represent_response(meth(*args, **kwargs))
10698

@@ -145,6 +137,14 @@ def dispatch_request(self, *args, **kwargs):
145137
if request.method != "POST":
146138
return View.dispatch_request(self, *args, **kwargs)
147139

140+
# Inject request arguments if an args schema is defined
141+
if self.get_args():
142+
meth = use_args(self.get_args())(meth)
143+
144+
# Marhal response if a response schema is defines
145+
if self.get_schema():
146+
meth = marshal_with(self.get_schema())(meth)
147+
148148
# Make a task out of the views `post` method
149149
task = taskify(meth)(*args, **kwargs)
150150

@@ -179,10 +179,29 @@ def get_args(cls):
179179
return cls.schema
180180

181181
def dispatch_request(self, *args, **kwargs):
182-
if request.method in ("POST", "PUT", "DELETE", "PATCH"):
183-
# FIXME: Sketchy as this breaks if the response is a generator/loop
184-
resp = View.dispatch_request(self, *args, **kwargs)
182+
meth = getattr(self, request.method.lower(), None)
185183

184+
# If the request method is HEAD and we don't have a handler for it
185+
# retry with GET.
186+
if meth is None and request.method == "HEAD":
187+
meth = getattr(self, "get", None)
188+
189+
# Flask should ensure this is assersion never fails
190+
assert meth is not None, f"Unimplemented method {request.method!r}"
191+
192+
# Inject request arguments if an args schema is defined
193+
if request.method in ("POST", "PUT", "PATCH") and self.get_args():
194+
meth = use_args(self.get_args())(meth)
195+
196+
# Marhal response if a response schema is defines
197+
if self.get_schema():
198+
meth = marshal_with(self.get_schema())(meth)
199+
200+
# Generate basic response
201+
resp = self.represent_response(meth(*args, **kwargs))
202+
203+
# Emit property event
204+
if request.method in ("POST", "PUT", "DELETE", "PATCH"):
186205
property_value = self.get_value()
187206
property_name = getattr(self, "endpoint", None) or getattr(
188207
self, "__name__", "unknown"
@@ -193,6 +212,4 @@ def dispatch_request(self, *args, **kwargs):
193212
PropertyStatusEvent(property_name), property_value,
194213
)
195214

196-
return resp
197-
198-
return View.dispatch_request(self, *args, **kwargs)
215+
return resp

0 commit comments

Comments
 (0)