7
7
class HopsFlask (base .HopsBase ):
8
8
"""Hops Middleware for Flask"""
9
9
10
- ERROR_PAGE_405 = """<!doctype html>
11
- <html lang=en>
12
- <title>405 Method Not Allowed</title>
13
- <h1>Method Not Allowed</h1>
14
- <p>The method is not allowed for the requested URL.</p>"""
15
-
16
10
def __init__ (self , flask_app ):
17
11
# keep a ref to original flask app
18
12
super (HopsFlask , self ).__init__ (flask_app )
@@ -36,18 +30,36 @@ def __call__(self, environ, start_response):
36
30
comp_uri = request .path
37
31
38
32
# if uri is registered on the hops app
39
- if self .contains (comp_uri ):
33
+ if self .handles (comp_uri ):
40
34
if method == "GET" :
41
- # if component exists
35
+ # if calling GET /solve, respond 405
36
+ if self .is_solve_uri (comp_uri ):
37
+ return self ._return_method_not_allowed (
38
+ environ ,
39
+ start_response
40
+ )
41
+
42
+ # if component exists, return component data
42
43
res , results = self .query (uri = comp_uri )
43
44
if res :
44
45
response = self ._prep_response ()
45
46
response .data = results
47
+
48
+ # otherwise return 404
46
49
else :
47
50
response = self ._prep_response (404 , "Unknown URI" )
51
+
48
52
return response (environ , start_response )
49
53
50
54
elif method == "POST" :
55
+ # if POST on component uri, return 405
56
+ if self .is_comp_uri (comp_uri ):
57
+ return self ._return_method_not_allowed (
58
+ environ ,
59
+ start_response
60
+ )
61
+
62
+ # otherwise try to solve with payload
51
63
data = request .data
52
64
res , results = self .solve (uri = comp_uri , payload = data )
53
65
if res :
@@ -56,12 +68,11 @@ def __call__(self, environ, start_response):
56
68
else :
57
69
response = self ._prep_response (404 , "Execution Error" )
58
70
response .data = results .encode (encoding = "utf_8" )
71
+
59
72
return response (environ , start_response )
60
73
61
74
# respond with 405 if method is not valid
62
- response = self ._prep_response (405 , "Method Not Allowed" )
63
- response .data = HopsFlask .ERROR_PAGE_405 .encode (encoding = "utf_8" )
64
- return response (environ , start_response )
75
+ return self ._return_method_not_allowed (environ , start_response )
65
76
66
77
# otherwise ask wapped app to process the call
67
78
return self .wsgi_app (environ , start_response )
0 commit comments