refactor: 本仓升为唯一源(原 sap-cli 源码仓归档)
方向反转:此前 SKILL.md 是「模板渲染产物」、sap-cli 是源;现 sap-cli 归档, sap-cli-skill 承接开发与分发,SKILL.md 回归手工维护的正本。 迁移(来自 sap-cli,共 104 文件): - tests/ 692 例测试(15 个文件的内联 sys.path 改指 assets/) - openspec/ SDD 规格与归档变更(42 文件) - docs/ 开发文档与 ADT 原理(含 dev/CLAUDE.md、AGENTS.md) - .claude/ rules 副本 + settings.json(供 Claude Code) - .github/ .hermes/ .pre-commit-config.yaml .editorconfig CLAUDE.md - scripts/ 保持仅 setup.py(pack_skill.py 已随旧仓归档,不迁) 修复(迁移暴露的真实缺陷): - assets/pyproject.toml 的 build-backend 写作 `setuptools.backends._legacy:_Backend`, 该模块在 setuptools 中不存在 → `pip install -e` 从来装不上。改为 build_meta。 实测:临时 venv 安装成功,sap-cli --help 正常列出 31 个命令 - pyproject readme 指向不存在的 assets/README.md(editable 安装会失败)→ 改内联文本 - pyproject urls 改指 sap-cli-skill 机制调整: - .github/workflows/ci.yml 适配 assets/ 布局;顶部注明该工作流仅 GitHub 执行, 本仓在 Gitee 不会自动跑 - pre-commit 增本地测试门禁(Gitee 上真正生效的那道) - .gitignore 合并旧仓完整规则(保留 log/ 下 md 知识库入库,只忽略运行日志) - 大文件上限 100KB→1MB(架构图 512KB) 守卫测试 tests/unit/test_repo_guards.py(10 → 18 例): - SKILL.md 须记录 parser 全部 CLI 命令 / 铁律 1-5 须为真实小节标题 / 示例不得违反铁律 5 - references/ 规则齐备;.claude/rules 与 references 必须一致(实测抓到一次真实漂移) - VERSION == sapcli.__version__ == README 版本 - 仓内不得再出现 pack_skill.py / skill-src(防废弃流程回潮) 698 tests OK;editable 安装与 CLI 入口经临时 venv 实测通过。 docs/RELEASING.md 重写为单源开发流程。
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
CLASS zcl_sapilot_constants DEFINITION
|
||||
PUBLIC
|
||||
FINAL
|
||||
CREATE PUBLIC.
|
||||
|
||||
PUBLIC SECTION.
|
||||
CONSTANTS:
|
||||
gc_objtype_rfc TYPE string VALUE 'RFC',
|
||||
gc_objtype_cds TYPE string VALUE 'CDS',
|
||||
gc_objtype_odata TYPE string VALUE 'ODATA',
|
||||
gc_objtype_clas TYPE string VALUE 'CLAS'.
|
||||
|
||||
CONSTANTS:
|
||||
gc_op_get_list TYPE string VALUE 'GET_LIST',
|
||||
gc_op_get_detail TYPE string VALUE 'GET_DETAIL',
|
||||
gc_op_use TYPE string VALUE 'USE',
|
||||
gc_op_health TYPE string VALUE 'HEALTH',
|
||||
gc_op_docs TYPE string VALUE 'DOCS',
|
||||
gc_op_logs TYPE string VALUE 'LOGS',
|
||||
gc_op_admin_reg TYPE string VALUE 'ADMIN_REG',
|
||||
gc_op_admin_upd TYPE string VALUE 'ADMIN_UPD',
|
||||
gc_op_admin_del TYPE string VALUE 'ADMIN_DEL',
|
||||
gc_op_admin_act TYPE string VALUE 'ADMIN_ACT'.
|
||||
|
||||
CONSTANTS:
|
||||
gc_http_get TYPE string VALUE 'GET',
|
||||
gc_http_post TYPE string VALUE 'POST',
|
||||
gc_http_put TYPE string VALUE 'PUT',
|
||||
gc_http_delete TYPE string VALUE 'DELETE'.
|
||||
|
||||
CONSTANTS:
|
||||
gc_sicf_path TYPE string VALUE '/sap/bc/sapilot',
|
||||
gc_content_type TYPE string VALUE 'application/json',
|
||||
gc_charset TYPE string VALUE 'utf-8'.
|
||||
|
||||
CONSTANTS:
|
||||
gc_field_success TYPE string VALUE 'success',
|
||||
gc_field_data TYPE string VALUE 'data',
|
||||
gc_field_error TYPE string VALUE 'error',
|
||||
gc_field_metadata TYPE string VALUE 'metadata',
|
||||
gc_field_request_id TYPE string VALUE 'requestId',
|
||||
gc_field_duration TYPE string VALUE 'duration',
|
||||
gc_field_timestamp TYPE string VALUE 'timestamp'.
|
||||
|
||||
CONSTANTS:
|
||||
gc_default_timeout_ms TYPE i VALUE 30000,
|
||||
gc_default_limit TYPE i VALUE 50,
|
||||
gc_max_limit TYPE i VALUE 200.
|
||||
|
||||
CONSTANTS:
|
||||
gc_dir_import TYPE string VALUE 'IMPORT',
|
||||
gc_dir_export TYPE string VALUE 'EXPORT',
|
||||
gc_dir_changing TYPE string VALUE 'CHANGING',
|
||||
gc_dir_tables TYPE string VALUE 'TABLES'.
|
||||
|
||||
CONSTANTS:
|
||||
gc_status_success TYPE string VALUE 'success',
|
||||
gc_status_error TYPE string VALUE 'error'.
|
||||
|
||||
TYPES:
|
||||
BEGIN OF ty_route,
|
||||
http_method TYPE string,
|
||||
path_pattern TYPE string,
|
||||
operation TYPE string,
|
||||
END OF ty_route,
|
||||
tt_routes TYPE STANDARD TABLE OF ty_route WITH EMPTY KEY.
|
||||
|
||||
TYPES:
|
||||
BEGIN OF ty_list_params,
|
||||
type TYPE string,
|
||||
category TYPE string,
|
||||
keyword TYPE string,
|
||||
limit TYPE i,
|
||||
offset TYPE i,
|
||||
END OF ty_list_params.
|
||||
|
||||
TYPES:
|
||||
BEGIN OF ty_response,
|
||||
success TYPE abap_bool,
|
||||
data TYPE REF TO data,
|
||||
error TYPE string,
|
||||
metadata TYPE REF TO data,
|
||||
END OF ty_response.
|
||||
|
||||
TYPES:
|
||||
BEGIN OF ty_object_item,
|
||||
name TYPE string,
|
||||
type TYPE string,
|
||||
category TYPE string,
|
||||
description TYPE string,
|
||||
END OF ty_object_item,
|
||||
tt_object_items TYPE STANDARD TABLE OF ty_object_item WITH EMPTY KEY.
|
||||
|
||||
ENDCLASS.
|
||||
|
||||
|
||||
CLASS zcl_sapilot_constants IMPLEMENTATION.
|
||||
ENDCLASS.
|
||||
@@ -0,0 +1,205 @@
|
||||
CLASS zcl_sapilot_http_handler DEFINITION
|
||||
PUBLIC
|
||||
FINAL
|
||||
CREATE PUBLIC.
|
||||
|
||||
PUBLIC SECTION.
|
||||
INTERFACES if_http_extension.
|
||||
|
||||
PRIVATE SECTION.
|
||||
METHODS process_request
|
||||
IMPORTING io_server TYPE REF TO if_http_server
|
||||
RAISING zcx_sapilot_error.
|
||||
|
||||
METHODS set_response_headers
|
||||
IMPORTING io_server TYPE REF TO if_http_server.
|
||||
|
||||
METHODS send_error_response
|
||||
IMPORTING io_server TYPE REF TO if_http_server
|
||||
ix_error TYPE REF TO zcx_sapilot_error.
|
||||
|
||||
METHODS send_success_response
|
||||
IMPORTING io_server TYPE REF TO if_http_server
|
||||
iv_json TYPE string
|
||||
iv_status TYPE i DEFAULT 200.
|
||||
|
||||
METHODS get_client_ip
|
||||
IMPORTING io_server TYPE REF TO if_http_server
|
||||
RETURNING VALUE(rv_ip) TYPE string.
|
||||
|
||||
ENDCLASS.
|
||||
|
||||
|
||||
CLASS zcl_sapilot_http_handler IMPLEMENTATION.
|
||||
|
||||
METHOD if_http_extension~handle_request.
|
||||
DATA: lx_error TYPE REF TO zcx_sapilot_error.
|
||||
DATA: lx_unexpected TYPE REF TO cx_static_check.
|
||||
DATA: lo_unexpected TYPE REF TO zcx_sapilot_error.
|
||||
|
||||
TRY.
|
||||
process_request( server ).
|
||||
|
||||
CATCH zcx_sapilot_error INTO lx_error.
|
||||
send_error_response( io_server = server ix_error = lx_error ).
|
||||
|
||||
CATCH cx_static_check INTO lx_unexpected.
|
||||
CREATE OBJECT lo_unexpected
|
||||
EXPORTING
|
||||
iv_http_status = 500
|
||||
iv_error_code = 'INTERNAL_ERROR'
|
||||
iv_message = 'An unexpected error occurred'
|
||||
iv_details = lx_unexpected->get_text( ).
|
||||
send_error_response( io_server = server ix_error = lo_unexpected ).
|
||||
ENDTRY.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD process_request.
|
||||
DATA: lo_router TYPE REF TO zcl_sapilot_router,
|
||||
lo_response_builder TYPE REF TO zcl_sapilot_response,
|
||||
lo_audit TYPE REF TO zcl_sapilot_audit_logger,
|
||||
lo_health TYPE REF TO zcl_sapilot_op_health,
|
||||
lo_getlist TYPE REF TO zcl_sapilot_op_getlist,
|
||||
lo_getdetail TYPE REF TO zcl_sapilot_op_getdetail,
|
||||
lo_use TYPE REF TO zcl_sapilot_op_use,
|
||||
lo_logs TYPE REF TO zcl_sapilot_op_logs,
|
||||
lo_admin TYPE REF TO zcl_sapilot_op_admin.
|
||||
|
||||
CREATE OBJECT lo_router.
|
||||
CREATE OBJECT lo_response_builder.
|
||||
CREATE OBJECT lo_audit.
|
||||
|
||||
GET TIME STAMP FIELD DATA(lv_start_ts).
|
||||
DATA(lv_request_id) = lo_response_builder->generate_request_id( ).
|
||||
|
||||
DATA(lv_method) = CONV string( io_server->request->get_method( ) ).
|
||||
DATA(lv_path) = CONV string( io_server->request->get_header_field( '~path_info' ) ).
|
||||
DATA(lv_query) = CONV string( io_server->request->get_header_field( '~query_string' ) ).
|
||||
DATA(lv_body) = io_server->request->get_cdata( ).
|
||||
|
||||
DATA(ls_route) = lo_router->route( iv_method = lv_method iv_path = lv_path ).
|
||||
|
||||
DATA: lv_result_json TYPE string.
|
||||
DATA: lx_route TYPE REF TO zcx_sapilot_error.
|
||||
|
||||
CASE ls_route-operation.
|
||||
WHEN zcl_sapilot_constants=>gc_op_health.
|
||||
CREATE OBJECT lo_health.
|
||||
lv_result_json = lo_health->execute( ).
|
||||
WHEN zcl_sapilot_constants=>gc_op_get_list.
|
||||
CREATE OBJECT lo_getlist.
|
||||
lv_result_json = lo_getlist->execute( iv_query_string = lv_query ).
|
||||
WHEN zcl_sapilot_constants=>gc_op_get_detail.
|
||||
CREATE OBJECT lo_getdetail.
|
||||
lv_result_json = lo_getdetail->execute( iv_path_info = lv_path ).
|
||||
WHEN zcl_sapilot_constants=>gc_op_use.
|
||||
CREATE OBJECT lo_use.
|
||||
lv_result_json = lo_use->execute( iv_path_info = lv_path iv_request_body = lv_body ).
|
||||
WHEN zcl_sapilot_constants=>gc_op_logs.
|
||||
CREATE OBJECT lo_logs.
|
||||
lv_result_json = lo_logs->execute( iv_query_string = lv_query ).
|
||||
WHEN zcl_sapilot_constants=>gc_op_admin_reg OR
|
||||
zcl_sapilot_constants=>gc_op_admin_upd OR
|
||||
zcl_sapilot_constants=>gc_op_admin_del OR
|
||||
zcl_sapilot_constants=>gc_op_admin_act.
|
||||
CREATE OBJECT lo_admin.
|
||||
lv_result_json = lo_admin->execute(
|
||||
iv_operation = ls_route-operation
|
||||
iv_object_type = CONV string( ls_route-object_type )
|
||||
iv_object_name = CONV string( ls_route-object_name )
|
||||
iv_request_body = lv_body ).
|
||||
WHEN OTHERS.
|
||||
RAISE EXCEPTION TYPE zcx_sapilot_error
|
||||
EXPORTING
|
||||
iv_http_status = 404
|
||||
iv_error_code = 'ROUTE_NOT_FOUND'
|
||||
iv_message = |No handler for path: { lv_path }|.
|
||||
ENDCASE.
|
||||
|
||||
GET TIME STAMP FIELD DATA(lv_end_ts).
|
||||
DATA: lv_duration_ms TYPE i.
|
||||
lv_duration_ms = cl_abap_tstmp=>subtract( tstmp1 = lv_end_ts tstmp2 = lv_start_ts ).
|
||||
|
||||
DATA(lv_response) = lo_response_builder->build_success(
|
||||
iv_json = lv_result_json
|
||||
iv_request_id = lv_request_id
|
||||
iv_duration = |{ lv_duration_ms }ms| ).
|
||||
|
||||
TRY.
|
||||
lo_audit->log(
|
||||
iv_request_id = lv_request_id
|
||||
iv_operation = ls_route-operation
|
||||
iv_object_type = CONV string( ls_route-object_type )
|
||||
iv_object_name = CONV string( ls_route-object_name )
|
||||
iv_input_json = lv_body
|
||||
iv_status = zcl_sapilot_constants=>gc_status_success
|
||||
iv_duration_ms = lv_duration_ms
|
||||
iv_remote_ip = get_client_ip( io_server ) ).
|
||||
CATCH cx_static_check.
|
||||
ENDTRY.
|
||||
|
||||
send_success_response( io_server = io_server iv_json = lv_response ).
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD set_response_headers.
|
||||
DATA(lo_response) = io_server->response.
|
||||
lo_response->set_content_type( zcl_sapilot_constants=>gc_content_type ).
|
||||
lo_response->set_header_field( name = 'charset' value = zcl_sapilot_constants=>gc_charset ).
|
||||
lo_response->set_header_field( name = 'Access-Control-Allow-Origin' value = '*' ).
|
||||
lo_response->set_header_field( name = 'Access-Control-Allow-Methods' value = 'GET, POST, PUT, DELETE, OPTIONS' ).
|
||||
lo_response->set_header_field( name = 'Access-Control-Allow-Headers' value = 'Content-Type, Authorization' ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD send_error_response.
|
||||
DATA: lo_response_builder TYPE REF TO zcl_sapilot_response.
|
||||
DATA: lv_request_id TYPE string.
|
||||
DATA: lv_duration TYPE string.
|
||||
DATA: lv_json TYPE string.
|
||||
DATA: lv_error_json TYPE string.
|
||||
DATA: lv_metadata TYPE string.
|
||||
|
||||
CREATE OBJECT lo_response_builder.
|
||||
lv_request_id = lo_response_builder->generate_request_id( ).
|
||||
lv_duration = '0ms'.
|
||||
lv_error_json = ix_error->get_json_error( ).
|
||||
lv_metadata = lo_response_builder->build_metadata( iv_request_id = lv_request_id iv_duration = lv_duration ).
|
||||
|
||||
DATA: lv_open TYPE c LENGTH 1,
|
||||
lv_close TYPE string.
|
||||
lv_open = cl_abap_char_utilities=>horizontal_tab.
|
||||
FIELD-SYMBOLS: <lv_x> TYPE x.
|
||||
ASSIGN lv_open TO <lv_x> CASTING.
|
||||
<lv_x> = '7B'.
|
||||
lv_close = '}'.
|
||||
|
||||
CONCATENATE lv_open '"success": false, "error": ' lv_error_json ', "metadata": ' lv_metadata lv_close INTO lv_json.
|
||||
|
||||
set_response_headers( io_server ).
|
||||
io_server->response->set_status( code = ix_error->mv_http_status reason = ix_error->mv_error_code ).
|
||||
io_server->response->set_cdata( lv_json ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD send_success_response.
|
||||
set_response_headers( io_server ).
|
||||
io_server->response->set_status( code = iv_status reason = 'OK' ).
|
||||
io_server->response->set_cdata( iv_json ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD get_client_ip.
|
||||
DATA: lv_fwd TYPE string,
|
||||
lv_real TYPE string.
|
||||
lv_fwd = io_server->request->get_header_field( 'X-Forwarded-For' ).
|
||||
IF lv_fwd IS NOT INITIAL.
|
||||
rv_ip = lv_fwd.
|
||||
ELSE.
|
||||
lv_real = io_server->request->get_header_field( 'X-Real-IP' ).
|
||||
IF lv_real IS NOT INITIAL.
|
||||
rv_ip = lv_real.
|
||||
ELSE.
|
||||
rv_ip = io_server->request->get_header_field( 'REMOTE_ADDR' ).
|
||||
ENDIF.
|
||||
ENDIF.
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
@@ -0,0 +1,382 @@
|
||||
"! SAPilot Operation: admin
|
||||
"! Manages the object whitelist registry via REST API.
|
||||
"!
|
||||
"! Endpoints:
|
||||
"! POST /admin/objects — Register new object
|
||||
"! POST /admin/objects/{type}/{name} — Update object metadata
|
||||
"! DELETE /admin/objects/{type}/{name} — Delete object from registry
|
||||
"! POST /admin/objects/{type}/{name}/active — Toggle active status
|
||||
"!
|
||||
"! All operations require SAP administrator authority.
|
||||
CLASS zcl_sapilot_op_admin DEFINITION
|
||||
PUBLIC
|
||||
FINAL
|
||||
CREATE PUBLIC.
|
||||
|
||||
PUBLIC SECTION.
|
||||
METHODS execute
|
||||
IMPORTING iv_operation TYPE string
|
||||
iv_object_type TYPE string OPTIONAL
|
||||
iv_object_name TYPE string OPTIONAL
|
||||
iv_request_body TYPE string OPTIONAL
|
||||
RETURNING VALUE(rv_json) TYPE string
|
||||
RAISING zcx_sapilot_error.
|
||||
|
||||
PRIVATE SECTION.
|
||||
METHODS check_admin_authority
|
||||
RAISING zcx_sapilot_error.
|
||||
|
||||
METHODS register_object
|
||||
IMPORTING iv_request_body TYPE string
|
||||
RETURNING VALUE(rv_json) TYPE string
|
||||
RAISING zcx_sapilot_error.
|
||||
|
||||
METHODS update_object
|
||||
IMPORTING iv_object_type TYPE string
|
||||
iv_object_name TYPE string
|
||||
iv_request_body TYPE string
|
||||
RETURNING VALUE(rv_json) TYPE string
|
||||
RAISING zcx_sapilot_error.
|
||||
|
||||
METHODS delete_object
|
||||
IMPORTING iv_object_type TYPE string
|
||||
iv_object_name TYPE string
|
||||
RETURNING VALUE(rv_json) TYPE string
|
||||
RAISING zcx_sapilot_error.
|
||||
|
||||
METHODS toggle_active
|
||||
IMPORTING iv_object_type TYPE string
|
||||
iv_object_name TYPE string
|
||||
iv_request_body TYPE string
|
||||
RETURNING VALUE(rv_json) TYPE string
|
||||
RAISING zcx_sapilot_error.
|
||||
|
||||
METHODS extract_json_field
|
||||
IMPORTING iv_json TYPE string
|
||||
iv_field TYPE string
|
||||
RETURNING VALUE(rv_value) TYPE string.
|
||||
|
||||
METHODS build_result_json
|
||||
IMPORTING iv_action TYPE string
|
||||
iv_type TYPE string
|
||||
iv_name TYPE string
|
||||
RETURNING VALUE(rv_json) TYPE string.
|
||||
|
||||
ENDCLASS.
|
||||
|
||||
|
||||
CLASS zcl_sapilot_op_admin IMPLEMENTATION.
|
||||
|
||||
METHOD execute.
|
||||
" -----------------------------------------------------------------
|
||||
" Admin operation dispatcher
|
||||
" -----------------------------------------------------------------
|
||||
check_admin_authority( ).
|
||||
|
||||
CASE iv_operation.
|
||||
WHEN zcl_sapilot_constants=>gc_op_admin_reg.
|
||||
rv_json = register_object( iv_request_body = iv_request_body ).
|
||||
|
||||
WHEN zcl_sapilot_constants=>gc_op_admin_upd.
|
||||
rv_json = update_object(
|
||||
iv_object_type = iv_object_type
|
||||
iv_object_name = iv_object_name
|
||||
iv_request_body = iv_request_body ).
|
||||
|
||||
WHEN zcl_sapilot_constants=>gc_op_admin_del.
|
||||
rv_json = delete_object(
|
||||
iv_object_type = iv_object_type
|
||||
iv_object_name = iv_object_name ).
|
||||
|
||||
WHEN zcl_sapilot_constants=>gc_op_admin_act.
|
||||
rv_json = toggle_active(
|
||||
iv_object_type = iv_object_type
|
||||
iv_object_name = iv_object_name
|
||||
iv_request_body = iv_request_body ).
|
||||
|
||||
WHEN OTHERS.
|
||||
RAISE EXCEPTION TYPE zcx_sapilot_error
|
||||
EXPORTING
|
||||
iv_http_status = 400
|
||||
iv_error_code = 'UNKNOWN_ADMIN_OPERATION'
|
||||
iv_message = |Unknown admin operation: { iv_operation }|.
|
||||
ENDCASE.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD check_admin_authority.
|
||||
" -----------------------------------------------------------------
|
||||
" Verify the current user has SAP development/admin authority
|
||||
" Check S_DEVELOP with object type 'ZS' (custom namespace)
|
||||
" -----------------------------------------------------------------
|
||||
AUTHORITY-CHECK OBJECT 'S_DEVELOP'
|
||||
ID 'DEVCLASS' DUMMY
|
||||
ID 'OBJTYPE' FIELD 'ZS'
|
||||
ID 'OBJNAME' DUMMY
|
||||
ID 'P_GROUP' DUMMY
|
||||
ID 'ACTVT' FIELD '02'.
|
||||
|
||||
IF sy-subrc <> 0.
|
||||
" Fallback: check if user has S_RFC with *
|
||||
AUTHORITY-CHECK OBJECT 'S_RFC'
|
||||
ID 'RFC_TYPE' FIELD '*'
|
||||
ID 'RFC_NAME' FIELD '*'.
|
||||
|
||||
IF sy-subrc <> 0.
|
||||
RAISE EXCEPTION TYPE zcx_sapilot_error
|
||||
EXPORTING
|
||||
iv_http_status = 403
|
||||
iv_error_code = 'ADMIN_ACCESS_DENIED'
|
||||
iv_message = |User { sy-uname } does not have admin authority|.
|
||||
ENDIF.
|
||||
ENDIF.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD register_object.
|
||||
" -----------------------------------------------------------------
|
||||
" POST /admin/objects
|
||||
" Body: {"type": "RFC", "name": "BAPI_USER_GET_DETAIL", "category": "...", "description": "..."}
|
||||
" Actual ZSAPILOT_OBJ fields: object_type, object_name, category,
|
||||
" description, http_method, status
|
||||
" -----------------------------------------------------------------
|
||||
IF iv_request_body IS INITIAL.
|
||||
RAISE EXCEPTION TYPE zcx_sapilot_error
|
||||
EXPORTING iv_http_status = 400 iv_error_code = 'EMPTY_BODY'
|
||||
iv_message = |Request body is required. Expected: \{"type", "name"\}|.
|
||||
ENDIF.
|
||||
|
||||
DATA(lv_type) = extract_json_field( iv_json = iv_request_body iv_field = 'type' ).
|
||||
DATA(lv_name) = extract_json_field( iv_json = iv_request_body iv_field = 'name' ).
|
||||
|
||||
IF lv_type IS INITIAL OR lv_name IS INITIAL.
|
||||
RAISE EXCEPTION TYPE zcx_sapilot_error
|
||||
EXPORTING iv_http_status = 400 iv_error_code = 'MISSING_REQUIRED_FIELD'
|
||||
iv_message = |Both 'type' and 'name' are required|.
|
||||
ENDIF.
|
||||
|
||||
lv_type = to_upper( lv_type ).
|
||||
lv_name = to_upper( lv_name ).
|
||||
|
||||
" Validate object type
|
||||
IF lv_type <> 'RFC' AND lv_type <> 'CDS' AND lv_type <> 'ODATA' AND lv_type <> 'CLAS'.
|
||||
RAISE EXCEPTION TYPE zcx_sapilot_error
|
||||
EXPORTING iv_http_status = 400 iv_error_code = 'INVALID_OBJECT_TYPE'
|
||||
iv_message = |Object type '{ lv_type }' is not supported. Use: RFC, CDS, ODATA, CLAS|.
|
||||
ENDIF.
|
||||
|
||||
" Check if already registered (old OpenSQL for NW 7.40)
|
||||
DATA: lv_exists TYPE zsapilot_object_name.
|
||||
SELECT SINGLE object_name FROM zsapilot_obj
|
||||
WHERE object_type = lv_type AND object_name = lv_name
|
||||
INTO lv_exists.
|
||||
|
||||
IF sy-subrc = 0.
|
||||
RAISE EXCEPTION TYPE zcx_sapilot_error
|
||||
EXPORTING iv_http_status = 409 iv_error_code = 'OBJECT_ALREADY_REGISTERED'
|
||||
iv_message = |Object { lv_type }/{ lv_name } is already registered|.
|
||||
ENDIF.
|
||||
|
||||
" Build registry entry
|
||||
DATA: ls_registry TYPE zsapilot_obj.
|
||||
ls_registry-object_type = lv_type.
|
||||
ls_registry-object_name = lv_name.
|
||||
ls_registry-category = extract_json_field( iv_json = iv_request_body iv_field = 'category' ).
|
||||
ls_registry-description = extract_json_field( iv_json = iv_request_body iv_field = 'description' ).
|
||||
ls_registry-status = 'ACTIVE'.
|
||||
|
||||
" Determine default HTTP method based on type
|
||||
CASE lv_type.
|
||||
WHEN 'RFC' OR 'CLAS'.
|
||||
ls_registry-http_method = 'POST'.
|
||||
WHEN OTHERS.
|
||||
ls_registry-http_method = 'GET'.
|
||||
ENDCASE.
|
||||
|
||||
INSERT zsapilot_obj FROM ls_registry.
|
||||
IF sy-subrc <> 0.
|
||||
RAISE EXCEPTION TYPE zcx_sapilot_error
|
||||
EXPORTING iv_http_status = 500 iv_error_code = 'REGISTER_FAILED'
|
||||
iv_message = |Failed to register object { lv_type }/{ lv_name }|.
|
||||
ENDIF.
|
||||
|
||||
rv_json = build_result_json( iv_action = 'registered' iv_type = lv_type iv_name = lv_name ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD update_object.
|
||||
" -----------------------------------------------------------------
|
||||
" POST /admin/objects/{type}/{name} (acting as PUT)
|
||||
" Body: {"category": "...", "description": "..."}
|
||||
" -----------------------------------------------------------------
|
||||
DATA: lv_otype TYPE zsapilot_object_type_de,
|
||||
lv_oname TYPE zsapilot_object_name.
|
||||
lv_otype = iv_object_type.
|
||||
lv_oname = iv_object_name.
|
||||
|
||||
" Verify object exists (old OpenSQL)
|
||||
DATA: ls_registry TYPE zsapilot_obj.
|
||||
SELECT SINGLE * FROM zsapilot_obj
|
||||
WHERE object_type = lv_otype AND object_name = lv_oname
|
||||
INTO ls_registry.
|
||||
|
||||
IF sy-subrc <> 0.
|
||||
RAISE EXCEPTION TYPE zcx_sapilot_error
|
||||
EXPORTING iv_http_status = 404 iv_error_code = 'OBJECT_NOT_FOUND'
|
||||
iv_message = |Object { iv_object_type }/{ iv_object_name } not found|.
|
||||
ENDIF.
|
||||
|
||||
" Update fields from request body
|
||||
IF iv_request_body IS NOT INITIAL.
|
||||
DATA(lv_category) = extract_json_field( iv_json = iv_request_body iv_field = 'category' ).
|
||||
DATA(lv_description) = extract_json_field( iv_json = iv_request_body iv_field = 'description' ).
|
||||
|
||||
IF lv_category IS NOT INITIAL.
|
||||
ls_registry-category = lv_category.
|
||||
ENDIF.
|
||||
IF lv_description IS NOT INITIAL.
|
||||
ls_registry-description = lv_description.
|
||||
ENDIF.
|
||||
ENDIF.
|
||||
|
||||
UPDATE zsapilot_obj FROM ls_registry.
|
||||
IF sy-subrc <> 0.
|
||||
RAISE EXCEPTION TYPE zcx_sapilot_error
|
||||
EXPORTING iv_http_status = 500 iv_error_code = 'UPDATE_FAILED'
|
||||
iv_message = |Failed to update object { iv_object_type }/{ iv_object_name }|.
|
||||
ENDIF.
|
||||
|
||||
rv_json = build_result_json( iv_action = 'updated' iv_type = iv_object_type iv_name = iv_object_name ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD delete_object.
|
||||
" -----------------------------------------------------------------
|
||||
" DELETE /admin/objects/{type}/{name}
|
||||
" -----------------------------------------------------------------
|
||||
DATA: lv_otype TYPE zsapilot_object_type_de,
|
||||
lv_oname TYPE zsapilot_object_name.
|
||||
lv_otype = iv_object_type.
|
||||
lv_oname = iv_object_name.
|
||||
|
||||
DATA: lv_exists TYPE zsapilot_object_name.
|
||||
SELECT SINGLE object_name FROM zsapilot_obj
|
||||
WHERE object_type = lv_otype AND object_name = lv_oname
|
||||
INTO lv_exists.
|
||||
|
||||
IF sy-subrc <> 0.
|
||||
RAISE EXCEPTION TYPE zcx_sapilot_error
|
||||
EXPORTING iv_http_status = 404 iv_error_code = 'OBJECT_NOT_FOUND'
|
||||
iv_message = |Object { iv_object_type }/{ iv_object_name } not found|.
|
||||
ENDIF.
|
||||
|
||||
" Delete associated field records first (old OpenSQL)
|
||||
DELETE FROM zsapilot_objf
|
||||
WHERE object_type = lv_otype AND object_name = lv_oname.
|
||||
|
||||
" Delete the main record
|
||||
DELETE FROM zsapilot_obj
|
||||
WHERE object_type = lv_otype AND object_name = lv_oname.
|
||||
|
||||
IF sy-subrc <> 0.
|
||||
RAISE EXCEPTION TYPE zcx_sapilot_error
|
||||
EXPORTING iv_http_status = 500 iv_error_code = 'DELETE_FAILED'
|
||||
iv_message = |Failed to delete object { iv_object_type }/{ iv_object_name }|.
|
||||
ENDIF.
|
||||
|
||||
rv_json = build_result_json( iv_action = 'deleted' iv_type = iv_object_type iv_name = iv_object_name ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD toggle_active.
|
||||
" -----------------------------------------------------------------
|
||||
" POST /admin/objects/{type}/{name}/active
|
||||
" Body: {"active": true} or {"active": false}
|
||||
" Uses 'status' field: 'ACTIVE' or 'INACTIVE'
|
||||
" -----------------------------------------------------------------
|
||||
DATA: lv_otype TYPE zsapilot_object_type_de,
|
||||
lv_oname TYPE zsapilot_object_name.
|
||||
lv_otype = iv_object_type.
|
||||
lv_oname = iv_object_name.
|
||||
|
||||
DATA: ls_registry TYPE zsapilot_obj.
|
||||
SELECT SINGLE * FROM zsapilot_obj
|
||||
WHERE object_type = lv_otype AND object_name = lv_oname
|
||||
INTO ls_registry.
|
||||
|
||||
IF sy-subrc <> 0.
|
||||
RAISE EXCEPTION TYPE zcx_sapilot_error
|
||||
EXPORTING iv_http_status = 404 iv_error_code = 'OBJECT_NOT_FOUND'
|
||||
iv_message = |Object { iv_object_type }/{ iv_object_name } not found|.
|
||||
ENDIF.
|
||||
|
||||
" Parse active flag from body
|
||||
IF iv_request_body IS NOT INITIAL.
|
||||
DATA(lv_active) = extract_json_field( iv_json = iv_request_body iv_field = 'active' ).
|
||||
IF lv_active = 'true' OR lv_active = 'X'.
|
||||
ls_registry-status = 'ACTIVE'.
|
||||
ELSE.
|
||||
ls_registry-status = 'INACTIVE'.
|
||||
ENDIF.
|
||||
ELSE.
|
||||
" Toggle: if currently active, deactivate; if inactive, activate
|
||||
IF ls_registry-status = 'ACTIVE'.
|
||||
ls_registry-status = 'INACTIVE'.
|
||||
ELSE.
|
||||
ls_registry-status = 'ACTIVE'.
|
||||
ENDIF.
|
||||
ENDIF.
|
||||
|
||||
UPDATE zsapilot_obj FROM ls_registry.
|
||||
|
||||
DATA: lv_action TYPE string.
|
||||
IF ls_registry-status = 'ACTIVE'.
|
||||
lv_action = 'activated'.
|
||||
ELSE.
|
||||
lv_action = 'deactivated'.
|
||||
ENDIF.
|
||||
rv_json = build_result_json( iv_action = lv_action iv_type = iv_object_type iv_name = iv_object_name ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD extract_json_field.
|
||||
" -----------------------------------------------------------------
|
||||
" Simple JSON field extractor: find "field": "value" or "field": value
|
||||
" Uses CONCATENATE to avoid backslash in string templates (NW 7.40)
|
||||
" -----------------------------------------------------------------
|
||||
IF iv_json IS INITIAL OR iv_field IS INITIAL.
|
||||
RETURN.
|
||||
ENDIF.
|
||||
|
||||
DATA: lv_bs TYPE string.
|
||||
FIELD-SYMBOLS: <lv_x> TYPE x.
|
||||
DATA: lv_bs_c TYPE c LENGTH 1.
|
||||
lv_bs_c = cl_abap_char_utilities=>horizontal_tab.
|
||||
ASSIGN lv_bs_c TO <lv_x> CASTING.
|
||||
<lv_x> = '5C'.
|
||||
lv_bs = lv_bs_c.
|
||||
|
||||
" Pattern: "field": "value"
|
||||
DATA: lv_ws TYPE string.
|
||||
lv_ws = lv_bs && 's' && lv_bs && '*'.
|
||||
DATA(lv_field_l) = to_lower( iv_field ).
|
||||
CONCATENATE '"' lv_field_l '"' lv_ws ':' lv_ws '*"([^"]*)"'
|
||||
INTO DATA(lv_pattern).
|
||||
FIND REGEX lv_pattern IN to_lower( iv_json ) SUBMATCHES DATA(lv_val).
|
||||
IF sy-subrc = 0 AND lv_val IS NOT INITIAL.
|
||||
rv_value = lv_val.
|
||||
RETURN.
|
||||
ENDIF.
|
||||
|
||||
" Pattern: "field": value (unquoted: true, false, number)
|
||||
CONCATENATE '"' lv_field_l '"' lv_ws ':' lv_ws '*([a-z0-9]+)'
|
||||
INTO lv_pattern.
|
||||
FIND REGEX lv_pattern IN to_lower( iv_json ) SUBMATCHES lv_val.
|
||||
IF sy-subrc = 0.
|
||||
rv_value = lv_val.
|
||||
ENDIF.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD build_result_json.
|
||||
" -----------------------------------------------------------------
|
||||
" Build a simple result JSON: {"action": "...", "type": "...", "name": "..."}
|
||||
" -----------------------------------------------------------------
|
||||
rv_json = '{"action": "' && iv_action && '", "type": "' && iv_type
|
||||
&& '", "name": "' && iv_name && '"}'.
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
Reference in New Issue
Block a user