Skip to content

Commit 9fd9674

Browse files
committed
api: Shell2HttpAPI.delete method
1 parent 3374da2 commit 9fd9674

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

flask_shell2http/api.py

+38-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
class Shell2HttpAPI(MethodView):
3030
"""
31-
``Flask.MethodView`` that registers ``GET`` and ``POST``
31+
``Flask.MethodView`` that creates ``GET``, ``POST`` and ``DELETE``
3232
methods for a given endpoint.
3333
This is invoked on ``Shell2HTTP.register_command``.
3434
@@ -37,6 +37,7 @@ class Shell2HttpAPI(MethodView):
3737

3838
def get(self):
3939
"""
40+
Get report by job key.
4041
Args:
4142
key (str):
4243
- Future key
@@ -138,6 +139,42 @@ def post(self):
138139
response_dict["result_url"] = self.__build_result_url(key)
139140
return make_response(jsonify(response_dict), HTTPStatus.BAD_REQUEST)
140141

142+
def delete(self):
143+
"""
144+
Cancel (if running) and delete job by job key.
145+
Args:
146+
key (str):
147+
- Future key
148+
"""
149+
key: str = ""
150+
try:
151+
key = request.args.get("key")
152+
logger.info(
153+
f"Job: '{key}' --> deletion requested. "
154+
f"Requester: '{request.remote_addr}'."
155+
)
156+
if not key:
157+
raise Exception("No key provided in arguments.")
158+
159+
# get the future object
160+
future: Future = self.executor.futures._futures.get(key)
161+
if not future:
162+
raise JobNotFoundException(f"No job exists for key: '{key}'.")
163+
164+
# cancel and delete from memory
165+
future.cancel()
166+
self.executor.futures.pop(key)
167+
168+
return make_response({}, HTTPStatus.NO_CONTENT)
169+
170+
except JobNotFoundException as e:
171+
logger.error(e)
172+
return make_response(jsonify(error=str(e)), HTTPStatus.NOT_FOUND)
173+
174+
except Exception as e:
175+
logger.error(e)
176+
return make_response(jsonify(error=str(e)), HTTPStatus.BAD_REQUEST)
177+
141178
@classmethod
142179
def __build_result_url(cls, key: str) -> str:
143180
return f"{request.base_url}?key={key}&wait=false"

0 commit comments

Comments
 (0)