Skip to content

Commit

Permalink
example: add deletion.py
Browse files Browse the repository at this point in the history
  • Loading branch information
eshaan7 committed Dec 22, 2021
1 parent 15310cc commit f393902
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/deletion.py
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)
1 change: 1 addition & 0 deletions examples/text.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"hello": "world"}
42 changes: 42 additions & 0 deletions examples/with_parse_json.py
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"))

0 comments on commit f393902

Please sign in to comment.