18
18
class HopsBase :
19
19
"""Base class for all Hops middleware implementations"""
20
20
21
- SOLVE_URI = "/solve"
21
+ ROOT_ROUTE = "/"
22
+ SOLVE_ROUTE = "/solve"
23
+
24
+ BUILTIN_ROUTES = [ROOT_ROUTE , SOLVE_ROUTE ]
22
25
23
26
ERROR_PAGE_405 = """<!doctype html>
24
27
<html lang=en>
@@ -33,13 +36,57 @@ def __init__(self, app):
33
36
# it is assumed that uri and solve uri and both unique to the component
34
37
self ._components : dict [str , HopsComponent ] = {}
35
38
36
- def handles (self , uri ):
37
- return uri == HopsBase .SOLVE_URI or uri in self ._components
39
+ def handles (self , request ):
40
+ uri = request .path
41
+ return uri in HopsBase .BUILTIN_ROUTES or uri in self ._components
42
+
43
+ def handle_HEAD (self , _ ):
44
+ return self ._prep_response (200 , "Success" )
45
+
46
+ def handle_GET (self , request ):
47
+ uri = request .path
48
+
49
+ # if calling GET /solve, respond 405
50
+ if self ._is_solve_uri (uri ):
51
+ return self ._return_method_not_allowed ()
52
+
53
+ # if component exists, return component data
54
+ res , results = self .query (uri = uri )
55
+ if res :
56
+ response = self ._prep_response ()
57
+ response .data = results
58
+
59
+ # otherwise return 404
60
+ else :
61
+ response = self ._prep_response (404 , "Unknown URI" )
62
+
63
+ return response
64
+
65
+ def handle_POST (self , request ):
66
+ uri = request .path
67
+
68
+ # if POST on component uri, return 405
69
+ if self ._is_comp_uri (uri ):
70
+ return self ._return_method_not_allowed ()
71
+
72
+ # otherwise try to solve with payload
73
+ data = request .data
74
+ res , results = self .solve (uri = uri , payload = data )
75
+ if res :
76
+ response = self ._prep_response ()
77
+ response .data = results .encode (encoding = "utf_8" )
78
+
79
+ # otherwise return 404
80
+ else :
81
+ response = self ._prep_response (404 , "Execution Error" )
82
+ response .data = results .encode (encoding = "utf_8" )
83
+
84
+ return response
38
85
39
- def is_solve_uri (self , uri ):
40
- return uri == HopsBase .SOLVE_URI
86
+ def _is_solve_uri (self , uri ):
87
+ return uri == HopsBase .SOLVE_ROUTE
41
88
42
- def is_comp_uri (self , uri ):
89
+ def _is_comp_uri (self , uri ):
43
90
return uri in self ._components
44
91
45
92
def query (self , uri ) -> Tuple [bool , str ]:
@@ -62,16 +109,16 @@ def query(self, uri) -> Tuple[bool, str]:
62
109
63
110
def solve (self , uri , payload ) -> Tuple [bool , str ]:
64
111
"""Perform Solve on given uri"""
65
- if uri == "/" :
112
+ if uri == HopsBase . ROOT_ROUTE :
66
113
hlogger .debug ("Nothing to solve on root" )
67
114
return False , self ._return_with_err ("Nothing to solve on root" )
68
115
69
116
# FIXME: remove support for legacy solve behaviour
70
- elif uri == HopsBase .SOLVE_URI :
117
+ elif uri == HopsBase .SOLVE_ROUTE :
71
118
data = json .loads (payload )
72
119
comp_uri = data ["pointer" ]
73
- if not comp_uri .startswith ("/" ):
74
- comp_uri = "/" + comp_uri
120
+ if not comp_uri .startswith (HopsBase . ROOT_ROUTE ):
121
+ comp_uri = HopsBase . ROOT_ROUTE + comp_uri
75
122
for comp in self ._components .values ():
76
123
if comp_uri == comp .uri :
77
124
hlogger .info ("Solving using legacy API: %s" , comp )
@@ -98,10 +145,10 @@ def _return_with_err(self, err_msg, res_dict=None):
98
145
99
146
return json .dumps (err_res , cls = _HopsEncoder )
100
147
101
- def _return_method_not_allowed (self , environ , start_response ):
148
+ def _return_method_not_allowed (self ):
102
149
response = self ._prep_response (405 , "Method Not Allowed" )
103
150
response .data = HopsBase .ERROR_PAGE_405 .encode (encoding = "utf_8" )
104
- return response ( environ , start_response )
151
+ return response
105
152
106
153
def _get_all_comps_data (self ):
107
154
# return json formatted string of all components metadata
0 commit comments