RESTful API Development Example
This section describes how to add RESTful APIs and provides guidance for secondary development.
File Description
The file path involved in the development example is {project_dir}/src/app/add_extend_restful_interface. The file directory structure is as follows:
├── build_extend_restful_interface.sh // Build script ├── register_new_blueprint.py // Script for registering blueprint functions ├── security_blueprint.py // Script for defining the extended blueprint ├── restful_extend_interfaces.py // Extended RESTful API configuration file └── security_service.py // API implementation script
Procedure
- Enable the cloud-edge collaboration API adding function.
Change the value of the _NeedAddExtendFusionDirectorInterface field in the project.conf file to yes (the default value is no).
- Add the custom API.
- Define the new script in security_blueprint.py.
from flask import Blueprint from flask import request from add_extend_restful_interface.security_service import service_required https_security_service_bp = Blueprint("SecurityService", __name__, url_prefix="/redfish/v1/Systems/SecurityService") https_security_service_bp.add_url_rule("/DigitalWarranty", view_func=service_required, methods=["GET"]) @https_security_service_bp.before_request def set_endpoint_executing_log(): """Record a log before the request.""" if request.method != "GET": pass - Implement the API in security_service.py.
from flask import request def get_life_time(): return {"status": 200, "data": 100} def service_required(): """ service life/service start time/production date query :return: response dictionary, resource template, or error message """ input_err_info = "Get DigitalWarranty info failed." try: # Obtain the resource template. ret_dict = get_life_time() except Exception as err: ret_dict = {"status": 400, "data": input_err_info} return ret_dict, "GeneralError" return ret_dict, "Success"
- Define the new script in security_blueprint.py.
- Implement the blueprint registration function in register_new_blueprint.py.
from flask import Flask from add_extend_restful_interface.security_blueprint import https_security_service_bp def register_new_blueprint(app: Flask): """ Function description: Register a blueprint. app: Flask instance """ # Register the new API blueprint. app.register_blueprint(https_security_service_bp) - Write the path for registering the blueprint function in the restful_extend_interfaces.py configuration file.
# Register the function path of the extended blueprint. EXTEND_RESTFUL_REGISTER_FUNCTIONS_PATH = "register_new_blueprint.register_new_blueprint"
- Perform compilation.
- Implement the compiled code in build_extend_restful_interface.sh.
#!/bin/bash SCRIPT_NAME=$(basename "$0") CUR_DIR=$(dirname "$(readlink -f "$0")") TOP_DIR="${CUR_DIR}"/../../.. OUTPUT_PACKAGE_DIR="${TOP_DIR}"/platform/omsdk # Add RESTful API functions. function add_new_restful_interface() { local redfish_server_dir="${OUTPUT_PACKAGE_DIR}"/software/RedfishServer #Create a directory of new APIs. [ ! -d "${redfish_server_dir}/add_extend_restful_interface" ] && mkdir "${redfish_server_dir}"/add_extend_restful_interface # The OM SDK project has a registered blueprint module named "register_blueprint.py". Do not use the same name to prevent overwriting this module. cp -rf "${CUR_DIR}"/register_new_blueprint.py "${redfish_server_dir}"/ # The extended API configuration module must overwrite the field name in the restful_extend_interfaces.py configuration file of the OM SDK with fixed values. cp -rf "${CUR_DIR}"/restful_extend_interfaces.py "${redfish_server_dir}"/ cp -rf "${CUR_DIR}"/security_blueprint.py "${redfish_server_dir}"/add_extend_restful_interface cp -rf "${CUR_DIR}"/security_service.py "${redfish_server_dir}"/add_extend_restful_interface } add_new_restful_interface - In {project_dir}/build/build.sh, implement the compilation script for calling the extended RESTful API.
#Add an extended RESTful API. # For details about the implementation of build_extend_restful_interface.sh, see the corresponding section. # CUR_DIR={project_dir}/build if [[ "${_NeedAddExtendRestfulInterface}" == "yes" ]]; then if ! bash "${CUR_DIR}/../src/app/add_extend_restful_interface/build_extend_restful_interface.sh";then return 1 fi fi
- Implement the compiled code in build_extend_restful_interface.sh.
Parent topic: RESTful API Development