SAP发布简易REST 三:API平台之接口文档详解编程语言

为了完善API接口,增强友好性,做了一点小文档展示。

新建配置表:(用来做接口参数配置展示使用,不在程序中应用)

SAP发布简易REST 三:API平台之接口文档详解编程语言

 因为需要给每个接口一个对应的名字,所以在原来的API控制表中增加一个文本字段。

SAP发布简易REST 三:API平台之接口文档详解编程语言

 SAP发布简易REST 三:API平台之接口文档详解编程语言

 这里加了俩个测试用的接口名对应参数表数据如下:

SAP发布简易REST 三:API平台之接口文档详解编程语言

下面就要在API的默认GET方法中展示出来。

  METHOD if_http_extension~handle_request. 
TYPES:BEGIN OF ty_retu, 
success      TYPE string, 
access_token TYPE string, 
message      TYPE string, 
END OF ty_retu. 
DATA: lt_fields   TYPE tihttpnvp, 
lv_data     TYPE string, 
lv_retu     TYPE string, 
gw_retu     TYPE ty_retu, 
lv_ifid     TYPE string, 
lv_method   TYPE string, 
gv_json     TYPE string, 
gt_api      TYPE TABLE OF zapi_param, 
gw_api      LIKE LINE OF gt_api, 
gw_con      TYPE zapi_control, 
gv_flag     TYPE flag, 
gv_new_meth TYPE c, 
gv_type     TYPE c LENGTH 10, 
gv_new_io   TYPE c. 
FIELD-SYMBOLS: <fs_field>       LIKE LINE OF lt_fields. 
"限定不做GET方法,GET根方法作为API文档说明 
 
lv_ifid = server->request->get_form_field( name = 'IFID' )."GET INTERFACE ID 
lv_method = server->request->get_header_field( name = '~request_method' )."GET INTERFACE METHOD 
"CHECK CONTROL TABLE 
IF lv_ifid IS INITIAL AND lv_method = 'GET'. 
SELECT * INTO TABLE gt_api FROM zapi_param. 
lv_retu = lv_retu && '<html><body>'. 
lv_retu = lv_retu && '<H1><center>Hisense API Document</center></H1>'. 
"说明 
lv_retu = lv_retu && '<I>Hisense API document is online from 2020.This is for public purpose to get connection to our system</I>'. 
lv_retu = lv_retu && '<br>'. 
lv_retu = lv_retu && '<br>'. 
lv_retu = lv_retu && '<br>'. 
LOOP AT gt_api INTO gw_api. 
AT NEW zmethod. 
gv_new_meth = 'X'. 
ENDAT. 
AT NEW zinout. 
gv_new_io = 'X'. 
ENDAT. 
IF gv_new_meth = 'X'. 
"二级标题 接口名 
SELECT SINGLE * INTO gw_con FROM zapi_control WHERE zif_id = gw_api-zif_id AND zmethod = gw_api-zmethod. 
lv_retu = lv_retu && '<H2>---------------' && gw_con-zme_name && '-----------------</H2>'. 
*          lv_retu = lv_retu && 'order create API'. 
"三级标题 接口说明 
lv_retu = lv_retu && '<H3>HTTP Request</H3>'. 
"文本 接口URL 
lv_retu = lv_retu && 'POST www.gjyxcrmdev.crm.hisense.com:8000/sap/zapi?ITID=' && gw_api-zif_id. 
lv_retu = lv_retu && '<br>'. 
lv_retu = lv_retu && '<br>'. 
lv_retu = lv_retu && '<br>'. 
CLEAR gv_new_meth. 
ENDIF. 
IF gv_new_io = 'X'. 
IF gw_api-zinout = 'I'. 
lv_retu = lv_retu && '<H3>Request Body Parameters</H3>'. 
lv_retu = lv_retu && '<br>'. 
ELSEIF gw_api-zinout = 'O'. 
lv_retu = lv_retu && '<H3>Response Body Parameters</H3>'. 
lv_retu = lv_retu && '<br>'. 
ENDIF. 
"字段属性说明 
lv_retu = lv_retu && '<table border="1">'. 
lv_retu = lv_retu && '<td>' && 'Parameter' && '</td>'. 
lv_retu = lv_retu && '<td>' && 'Type' && '</td>'. 
lv_retu = lv_retu && '<td>' && 'Status' && '</td>'. 
lv_retu = lv_retu && '<td>' && 'Description' && '</td>'. 
gv_new_io = ''. 
ENDIF. 
"字段 
lv_retu = lv_retu && '<tr>'. 
lv_retu = lv_retu && '<td>' && gw_api-zparameter && '</td>'. 
CALL FUNCTION 'CONVERSION_EXIT_DTYPE_OUTPUT' 
EXPORTING 
input  = gw_api-type 
IMPORTING 
output = gv_type. 
lv_retu = lv_retu && '<td>' && gv_type && '</td>'. 
lv_retu = lv_retu && '<td>' && gw_api-status && '</td>'. 
lv_retu = lv_retu && '<td>' && gw_api-description && '</td>'. 
lv_retu = lv_retu && '</tr>'. 
AT END OF zinout. 
lv_retu = lv_retu && '</table>'. 
lv_retu = lv_retu && '<br>'. 
ENDAT. 
ENDLOOP. 
*      CONCATENATE 'T1:{' 
*  cl_abap_char_utilities=>newline 
*  'HEAD:{' 
*  cl_abap_char_utilities=>newline 
*  `"PROCESS_TYPE":"ZSV1"` 
*  cl_abap_char_utilities=>newline 
*  `"POSTING_DATE":"20201124"` 
*  cl_abap_char_utilities=>newline 
*  `"ITEM":{` 
*  cl_abap_char_utilities=>newline 
*  `"NO":"10"` 
*  cl_abap_char_utilities=>newline 
*  `"PRODUCT":"TEST12333"` 
*  cl_abap_char_utilities=>newline 
*  `}` 
*  cl_abap_char_utilities=>newline 
*  '}' 
*  cl_abap_char_utilities=>newline 
*  '}' 
*  INTO gv_json. 
*      lv_retu = lv_retu && '<I>' && gv_json && '</I>'. 
"结束 
lv_retu = lv_retu && '</body></html>'. 
"SET RETURN DATA 
server->response->if_http_entity~set_content_type( content_type = 'Text/HTML' ). 
server->response->set_cdata( 
EXPORTING 
data   = lv_retu    " Character data 
            ). 
ELSE. 
SELECT SINGLE * INTO gw_con FROM zapi_control WHERE zif_id = lv_ifid AND zmethod = lv_method. 
IF sy-subrc <> 0. 
gw_retu-success = 'E'. 
gw_retu-message = 'Called Error,Please check the url or method!'. 
lv_retu = /ui2/cl_json=>serialize( 
data        = gw_retu 
pretty_name = 'L' 
compress    = abap_true ). 
ELSE. 
CASE gw_con-zflag."used flag 
WHEN 'X'. 
*""GET HTTP MESSAGE BODY 
lv_data = server->request->if_http_entity~get_cdata( ). 
"call method 
CALL FUNCTION gw_con-zpro_fm 
EXPORTING 
input  = lv_data 
IMPORTING 
output = lv_retu. 
WHEN ''. 
gw_retu-success = 'E'. 
gw_retu-message = 'Interface Not Used!'. 
lv_retu = /ui2/cl_json=>serialize( 
data        = gw_retu 
pretty_name = 'L' 
compress    = abap_true ). 
WHEN OTHERS. 
ENDCASE. 
ENDIF. 
"SET RETURN DATA 
server->response->if_http_entity~set_content_type( content_type = 'application/json' ). 
server->response->set_cdata( 
EXPORTING 
data   = lv_retu    " Character data 
            ). 
ENDIF. 
ENDMETHOD.

SICF测试服务,效果如下:

SAP发布简易REST 三:API平台之接口文档详解编程语言

 SAP发布简易REST 三:API平台之接口文档详解编程语言

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/18716.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论