diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8cf83dc08..1199f9f70 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -39,6 +39,7 @@ Changed ------- - ``self._deactivate()`` method from the ``FormAction`` class has been renamed to ``self.deactivate()`` +- changed endpoint function so that it is now accessible with Python as well [0.12.1] - 2018-11-11 ^^^^^^^^^^^^^^^^^^^^^ diff --git a/rasa_core_sdk/__init__.py b/rasa_core_sdk/__init__.py index 4fa1a1da3..bcde5fb63 100644 --- a/rasa_core_sdk/__init__.py +++ b/rasa_core_sdk/__init__.py @@ -10,6 +10,7 @@ from typing import Dict, Text, Any, Optional, Iterator, List import rasa_core_sdk.version +import rasa_core_sdk.cli logger = logging.getLogger(__name__) diff --git a/rasa_core_sdk/cli/__init__.py b/rasa_core_sdk/cli/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/rasa_core_sdk/cli/arguments.py b/rasa_core_sdk/cli/arguments.py new file mode 100644 index 000000000..56b75efbb --- /dev/null +++ b/rasa_core_sdk/cli/arguments.py @@ -0,0 +1,32 @@ +import argparse + +from rasa_core_sdk.constants import DEFAULT_SERVER_PORT + + +def action_arg(action): + if "/" in action: + raise argparse.ArgumentTypeError( + 'Invalid actions format. Actions file should be a python module ' + 'and passed with module notation (e.g. directory.actions).') + else: + return action + + +def add_endpoint_arguments(parser): + parser.add_argument( + '-p', '--port', + default=DEFAULT_SERVER_PORT, + type=int, + help="port to run the server at") + parser.add_argument( + '--cors', + nargs='*', + type=str, + help="enable CORS for the passed origin. " + "Use * to whitelist all origins") + parser.add_argument( + '--actions', + type=action_arg, + default=None, + help="name of action package to be loaded" + ) diff --git a/rasa_core_sdk/constants.py b/rasa_core_sdk/constants.py new file mode 100644 index 000000000..706aa6b7b --- /dev/null +++ b/rasa_core_sdk/constants.py @@ -0,0 +1 @@ +DEFAULT_SERVER_PORT = 5055 diff --git a/rasa_core_sdk/endpoint.py b/rasa_core_sdk/endpoint.py index 22e9b5d21..efd008773 100644 --- a/rasa_core_sdk/endpoint.py +++ b/rasa_core_sdk/endpoint.py @@ -11,48 +11,24 @@ from flask import Flask, jsonify, request from flask_cors import CORS, cross_origin from gevent.pywsgi import WSGIServer + +from rasa_core_sdk.cli.arguments import add_endpoint_arguments +from rasa_core_sdk.constants import DEFAULT_SERVER_PORT from rasa_core_sdk.executor import ActionExecutor from rasa_core_sdk import ActionExecutionRejection import rasa_core_sdk from rasa_core_sdk import utils -DEFAULT_SERVER_PORT = 5055 - logger = logging.getLogger(__name__) -def action_arg(action): - if "/" in action: - raise argparse.ArgumentTypeError( - 'Invalid actions format. Actions file should be a python module ' - 'and passed with module notation (e.g. directory.actions).') - else: - return action - - def create_argument_parser(): """Parse all the command line arguments for the run script.""" parser = argparse.ArgumentParser( description='starts the action endpoint') - parser.add_argument( - '-p', '--port', - default=DEFAULT_SERVER_PORT, - type=int, - help="port to run the server at") - parser.add_argument( - '--cors', - nargs='*', - type=str, - help="enable CORS for the passed origin. " - "Use * to whitelist all origins") - parser.add_argument( - '--actions', - type=action_arg, - default=None, - help="name of action package to be loaded" - ) + add_endpoint_arguments(parser) utils.add_logging_option_arguments(parser) return parser @@ -135,24 +111,32 @@ def check_version_compatibility(core_version): "".format(core_version, rasa_core_sdk.__version__)) -if __name__ == '__main__': - # Running as standalone python application - arg_parser = create_argument_parser() - cmdline_args = arg_parser.parse_args() - - logging.basicConfig(level=logging.DEBUG) - logging.getLogger('matplotlib').setLevel(logging.WARN) - - utils.configure_colored_logging(cmdline_args.loglevel) - +def run(actions, port=DEFAULT_SERVER_PORT, cors='*'): logger.info("Starting action endpoint server...") - edp_app = endpoint_app(cors_origins=cmdline_args.cors, - action_package_name=cmdline_args.actions) + edp_app = endpoint_app(cors_origins=cors, + action_package_name=actions) - http_server = WSGIServer(('0.0.0.0', cmdline_args.port), edp_app) + http_server = WSGIServer(('0.0.0.0', port), edp_app) http_server.start() logger.info("Action endpoint is up and running. on {}" "".format(http_server.address)) http_server.serve_forever() + + +def main(args): + logging.basicConfig(level=logging.DEBUG) + logging.getLogger('matplotlib').setLevel(logging.WARN) + + utils.configure_colored_logging(args.loglevel) + + run(args.actions, args.port, args.cors) + + +if __name__ == '__main__': + # Running as standalone python application + arg_parser = create_argument_parser() + cmdline_args = arg_parser.parse_args() + + main(cmdline_args) diff --git a/tests/test_endpoint.py b/tests/test_endpoint.py index 36c372346..e55af21da 100644 --- a/tests/test_endpoint.py +++ b/tests/test_endpoint.py @@ -7,16 +7,12 @@ import rasa_core_sdk.endpoint as ep -def test_endpoint(): - pass - - def test_arg_parser_actions_params_folder_style(): parser = ep.create_argument_parser() args = ['--actions', 'actions/act'] with pytest.raises(BaseException) as e: - cmdline_args = parser.parse_args(args) + parser.parse_args(args) if e is not None: assert True else: