-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# web imports | ||
from flask import Flask | ||
from flask_executor import Executor | ||
from flask_shell2http import Shell2HTTP | ||
|
||
# Flask application instance | ||
app = Flask(__name__) | ||
|
||
# application factory | ||
executor = Executor(app) | ||
shell2http = Shell2HTTP(app, executor) | ||
|
||
|
||
shell2http.register_command( | ||
endpoint="sleep", | ||
command_name="sleep", | ||
) | ||
|
||
|
||
# Test Runner | ||
if __name__ == "__main__": | ||
app.testing = True | ||
c = app.test_client() | ||
# request new process | ||
r1 = c.post("/sleep", json={"args": ["10"], "force_unique_key": True}) | ||
print(r1) | ||
# request cancellation | ||
r2 = c.delete(f"/sleep?key={r1.get_json()['key']}") | ||
print(r2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"hello": "world"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# generic imports | ||
import functools | ||
import json | ||
|
||
# web imports | ||
from flask import Flask, request, g, abort, make_response, jsonify | ||
from flask_executor import Executor | ||
from flask_shell2http import Shell2HTTP | ||
|
||
# Flask application instance | ||
app = Flask(__name__) | ||
|
||
# application factory | ||
executor = Executor(app) | ||
shell2http = Shell2HTTP(app, executor, base_url_prefix="/cmd/") | ||
|
||
|
||
def json_parser_decorator(f): | ||
@functools.wraps(f) | ||
def decorator(*args, **kwargs): | ||
response = f(*args, **kwargs) | ||
print(response.status_code, response.json) | ||
resp_json = response.json.copy() | ||
if isinstance(resp_json.get("report", None), str): | ||
resp_json["report"] = json.loads(resp_json["report"]) | ||
return make_response(jsonify(resp_json), response.status_code) | ||
|
||
return decorator | ||
|
||
|
||
shell2http.register_command( | ||
endpoint="echo", command_name="cat text.json", decorators=[json_parser_decorator] | ||
) | ||
|
||
|
||
# Test Runner | ||
if __name__ == "__main__": | ||
app.testing = True | ||
c = app.test_client() | ||
# request 1 | ||
r1 = c.post("/cmd/echo") | ||
r2 = c.get(r1.json["result_url"].replace("wait=false", "wait=true")) |