API Reference¶
If you are looking for information on a specific function, class or method, this part of the documentation is for you.
- class flask_shell2http.base_entrypoint.Shell2HTTP(app=None, executor: Optional[flask_executor.executor.Executor] = None, base_url_prefix: str = '/')¶
Flask-Shell2HTTP base entrypoint class. The only public API available to users.
- app¶
Flask application instance.
- executor¶
Flask-Executor instance
- base_url_prefix¶
base prefix to apply to endpoints. Defaults to “/”.
- Type
str
Example:
app = Flask(__name__) executor = Executor(app) shell2http = Shell2HTTP(app=app, executor=executor, base_url_prefix="/tasks/")
- get_registered_commands() OrderedDict[str, str] ¶
Most of the time you won’t need this since Flask provides a
Flask.url_map
attribute.- Returns
OrderedDict[uri, command] i.e. mapping of registered commands and their URLs.
- init_app(app, executor: flask_executor.executor.Executor) None ¶
For use with Flask’s Application Factory method.
Example:
executor = Executor() shell2http = Shell2HTTP(base_url_prefix="/commands/") app = Flask(__name__) executor.init_app(app) shell2http.init_app(app=app, executor=executor)
- register_command(endpoint: str, command_name: str, callback_fn: Optional[Callable[[Dict, concurrent.futures._base.Future], Any]] = None, decorators: List = []) None ¶
Function to map a shell command to an endpoint.
- Parameters
endpoint (str) –
your command would live here:
/{base_url_prefix}/{endpoint}
command_name (str) –
The base command which can be executed from the given endpoint.
If
command_name='echo'
, then all arguments passed to this endpoint will be appended toecho
.For example, if you pass
{ "args": ["Hello", "World"] }
in POST request, it gets converted toecho Hello World
.
callback_fn (Callable[[Dict, Future], Any]) –
- An optional function that is invoked when a requested process
to this endpoint completes execution.
- This is added as a
concurrent.Future.add_done_callback(fn=callback_fn)
The same callback function may be used for multiple commands.
if request JSON contains a callback_context attr, it will be passed as the first argument to this function.
decorators (List[Callable]) –
A List of view decorators to apply to the endpoint.
New in version v1.5.0
Examples:
def my_callback_fn(context: dict, future: Future) -> None: print(future.result(), context) shell2http.register_command(endpoint="echo", command_name="echo") shell2http.register_command( endpoint="myawesomescript", command_name="./fuxsocy.py", callback_fn=my_callback_fn, decorators=[], )