RESTful接口开发示例
本章节将详细介绍如何新增RESTful接口,并指导用户进行二次开发。
文件说明
开发示例中涉及到的文件路径为“{project_dir}/src/app/add_extend_restful_interface”,文件目录结构如下:
├── build_extend_restful_interface.sh // 构建脚本 ├── register_new_blueprint.py // 注册蓝图函数脚本 ├── security_blueprint.py // 定义扩展的蓝图脚本 ├── restful_extend_interfaces.py // 扩展RESTful接口配置文件 └── security_service.py // 接口实现脚本
操作步骤
- 打开新增云边协同接口功能开关。
将project.conf文件的“_NeedAddExtendFusionDirectorInterface”字段取值改为“yes”(默认值为“no”)。
- 新增自定义接口。
- 在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(): """请求进入前先记录一条日志""" if request.method != "GET": pass
- 在security_service.py中,实现接口。
from flask import request def get_life_time(): return {"status": 200, "data": 100} def service_required(): """ 服务年限/服务起始时间/出产日期查询 :return: 响应字典 资源模板或错误消息 """ input_err_info = "Get DigitalWarranty info failed." try: # 获取资源模板 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"
- 在security_blueprint.py中,定义新增的脚本。
- 在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): """ 功能描述:注册蓝图 app: Flask实例 """ # 注册新增的接口蓝图 app.register_blueprint(https_security_service_bp)
- 在配置文件restful_extend_interfaces.py中写入注册蓝图函数的路径。
# 注册扩展的蓝图的函数路径 EXTEND_RESTFUL_REGISTER_FUNCTIONS_PATH = "register_new_blueprint.register_new_blueprint"
- 编译。
- 在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 # 添加restful接口函数 function add_new_restful_interface() { local redfish_server_dir="${OUTPUT_PACKAGE_DIR}"/software/RedfishServer # 创建新增接口目录 [ ! -d "${redfish_server_dir}/add_extend_restful_interface" ] && mkdir "${redfish_server_dir}"/add_extend_restful_interface # OM SDK工程已有注册蓝图的模块,名为register_blueprint.py,请勿取相同名字覆盖掉OM SDK的注册蓝图模块 cp -rf "${CUR_DIR}"/register_new_blueprint.py "${redfish_server_dir}"/ # 扩展接口配置模块须覆盖OM SDK中的restful_extend_interfaces.py 配置文件中的字段名为固定值 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
- 在“{project_dir}/build/build.sh”中,实现调用扩展RESTful接口的编译脚本。
# 添加扩展restful接口 # build_extend_restful_interface.sh具体实现可参考对应章节实现 # 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
- 在build_extend_restful_interface.sh中实现编译的代码。
父主题: RESTful接口开发