{"library":"nsj-rest-lib2","title":"NSJ REST Lib2","description":"nsj-rest-lib2 is a Python library designed to enable the distribution of dynamic REST API routes. These routes are configured declaratively using JSON-based Endpoint Definition Language (EDL) files. It leverages Flask and Flask-RESTful to integrate these dynamic routes into an existing Flask application. The current version is 0.0.41, and it undergoes frequent patch updates, indicating active development.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install nsj-rest-lib2"],"cli":null},"imports":["from nsj_rest_lib2.util import create_dynamic_rest_api_endpoint_from_json","from nsj_rest_lib2.register import register_route"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import sys\nfrom types import ModuleType\nfrom flask import Flask\nfrom nsj_rest_lib2.register import register_route\nfrom nsj_rest_lib2.util import create_dynamic_rest_api_endpoint_from_json\nimport os\n\n# --- Step 1: Define your endpoint logic ---\n# In a real application, 'my_app_endpoints' would be a real .py file\n# and MyEndpointClass would be a class defined within it.\nclass MyEndpointClass:\n    def my_dynamic_method(self):\n        \"\"\"A simple method to be called by the dynamic route.\"\"\"\n        return {\"message\": \"Hello from nsj-rest-lib2 dynamic endpoint!\"}, 200\n\n# Dynamically add this class to a mock module in sys.modules\n# This allows create_dynamic_rest_api_endpoint_from_json to find it\n# without needing an actual file on disk for this quickstart example.\nmock_module_name = 'my_app_endpoints'\nif mock_module_name not in sys.modules:\n    sys.modules[mock_module_name] = ModuleType(mock_module_name)\nsetattr(sys.modules[mock_module_name], 'MyEndpointClass', MyEndpointClass)\n\n\n# --- Step 2: Initialize Flask application ---\napp = Flask(__name__)\napp.secret_key = os.environ.get('FLASK_SECRET_KEY', 'a_super_secret_key_for_dev') # Required for some Flask extensions\n\n# --- Step 3: Define the declarative route configuration (EDL) ---\nroute_config_json = {\n    \"uri\": \"/api/v1/dynamic-resource\",\n    \"http_method\": \"GET\",\n    \"endpoint_module\": mock_module_name, # References the mock module\n    \"endpoint_class_name\": \"MyEndpointClass\", # Class containing the method\n    \"endpoint_method_name\": \"my_dynamic_method\", # Method to be called\n    \"request_body_model_name\": None,\n    \"response_body_model_name\": None,\n    \"query_string_model_name\": None,\n    \"path_params_model_name\": None,\n    \"header_params_model_name\": None,\n    \"jwt_roles\": [],\n    \"jwt_allowed\": False,\n    \"jwt_required\": False\n}\n\n# --- Step 4: Create and register the dynamic endpoint ---\ndynamic_endpoint = create_dynamic_rest_api_endpoint_from_json(route_config_json)\nregister_route(app, dynamic_endpoint)\n\n# --- Step 5: Run the Flask application (for demonstration) ---\n# In a production environment, use a WSGI server like Gunicorn or Waitress.\nif __name__ == '__main__':\n    print(\"\\nFlask application running at http://127.0.0.1:5000\")\n    print(\"Test with: curl http://127.0.0.1:5000/api/v1/dynamic-resource\")\n    app.run(debug=True, port=5000)\n","lang":"python","description":"This quickstart demonstrates how to define an Endpoint Definition Language (EDL) configuration in JSON, use `create_dynamic_rest_api_endpoint_from_json` to create a dynamic endpoint object, and then `register_route` to add it to a Flask application. It includes a minimal Flask app and a dynamically created mock module to make the example runnable and testable. To run, save as a Python file and execute `python your_file.py`. Then, access `http://127.0.0.1:5000/api/v1/dynamic-resource` in your browser or with `curl`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}