FUNCTION ZFM_INTERFACE_LOG.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" REFERENCE(IV_DSTART) TYPE EDI_UPDDAT
*"----------------------------------------------------------------------
TYPES: TY_TIME_COND TYPE RANGE OF TIMESTAMP.
DATA: S_TS TYPE TY_TIME_COND.
* DATA: S_TS TYPE RANGE OF TIMESTAMP.
DATA START_TIMESTAMP TYPE TZONREF-TSTAMPS.
DATA END_TIMESTAMP TYPE TZONREF-TSTAMPS.
CALL FUNCTION 'CIF_GEN4_CONVERT_DATETIME'
EXPORTING
IV_DATE = IV_DSTART
IV_TIME = '000000'
IV_TIMEZONE = SY-ZONLO
IMPORTING
EV_TIMESTAMP = START_TIMESTAMP.
CALL FUNCTION 'CIF_GEN4_CONVERT_DATETIME'
EXPORTING
IV_DATE = IV_DSTART
IV_TIME = '235959'
IV_TIMEZONE = SY-ZONLO
IMPORTING
EV_TIMESTAMP = END_TIMESTAMP.
S_TS = VALUE #( SIGN = 'I' OPTION = 'BT' ( LOW = START_TIMESTAMP HIGH = END_TIMESTAMP ) ).
DATA :BEGIN OF GS_ITEM,
GUID TYPE ZAFL_LOG-GUID,
FNAME TYPE ZAFL_LOG-FNAME,
CUST_FIELD1 TYPE ZAFL_LOG-CUST_FIELD1,
CUST_FIELD2 TYPE ZAFL_LOG-CUST_FIELD2,
CUST_FIELD3 TYPE ZAFL_LOG-CUST_FIELD3,
STATUS TYPE ZAFL_LOG-STATUS,
TIMESTAMP TYPE ZAFL_LOG-TIMESTAMP,
TIME_COST TYPE ZAFL_LOG-TIME_COST,
UNAME TYPE ZAFL_LOG-UNAME,
MESSAGE TYPE ZAFL_LOG-MESSAGE,
IMPORT TYPE ZAFL_LOG-IMPORT,
EXPORT TYPE ZAFL_LOG-EXPORT,
TABLE_IN TYPE ZAFL_LOG-TABLE_IN,
TABLE_OUT TYPE ZAFL_LOG-TABLE_OUT,
CHANGE_IN TYPE ZAFL_LOG-CHANGE_IN,
CHANGE_OUT TYPE ZAFL_LOG-CHANGE_OUT,
UDATE TYPE ZTSD001M-UDATE,
UTIME TYPE ZTSD001M-UTIME,
END OF GS_ITEM,
GT_ITEM LIKE TABLE OF GS_ITEM.
DATA: BEGIN OF GS_HEAD,
apiName(20),
END OF GS_HEAD.
DATA: BEGIN OF GS_BODY,
DATA LIKE TABLE OF GS_ITEM,
END OF GS_BODY.
DATA: BEGIN OF GS_SEND,
HEAD LIKE GS_HEAD,
BODY LIKE GS_BODY,
END OF GS_SEND.
TYPES: BEGIN OF T_RESP,
ERR_CODE TYPE STRING,
ERR_MSG TYPE STRING,
TRACE_MSG TYPE STRING,
END OF T_RESP.
GS_SEND-HEAD-APINAME = 'SD100'.
DATA:GS_RESP TYPE T_RESP.
SELECT * FROM ZAFL_LOG WHERE TIMESTAMP IN @S_TS INTO CORRESPONDING FIELDS OF TABLE @GT_ITEM.
APPEND INITIAL LINE TO GT_ITEM.
GS_SEND-BODY-DATA = GT_ITEM.
* IF GT_ITEM IS NOT INITIAL.
DATA:LT_FIELDS TYPE TIHTTPNVP,
LS_FIELDS TYPE IHTTPNVP,
LS_URL TYPE ZTSEND_URL,
LV_TYPE TYPE BAPI_MTYPE,
LV_SEND TYPE STRING,
LV_MESSAGE TYPE STRING,
LV_RESPONSE TYPE STRING.
DATA:BEGIN OF LS_NAME_MAPPING,
ABAP TYPE ABAP_COMPNAME,
JSON TYPE STRING,
END OF LS_NAME_MAPPING,
LT_MAPPINGS LIKE HASHED TABLE OF LS_NAME_MAPPING WITH UNIQUE KEY ABAP.
LT_MAPPINGS = VALUE #(
( ABAP = 'GUID' JSON = 'guid' )
( ABAP = 'FNAME' JSON = 'fname' )
( ABAP = 'CUST_FIELD1' JSON = 'cust_field1' )
( ABAP = 'CUST_FIELD2' JSON = 'cust_field2' )
( ABAP = 'CUST_FIELD3' JSON = 'cust_field3' )
( ABAP = 'STATUS' JSON = 'status' )
( ABAP = 'TIMESTAMP' JSON = 'timestamp' )
( ABAP = 'TIME_COST' JSON = 'time_cost' )
( ABAP = 'UNAME' JSON = 'uname' )
( ABAP = 'MESSAGE' JSON = 'message' )
( ABAP = 'IMPORT' JSON = 'import' )
( ABAP = 'EXPORT' JSON = 'export' )
( ABAP = 'TABLE_IN' JSON = 'table_in' )
( ABAP = 'TABLE_OUT' JSON = 'table_out' )
( ABAP = 'CHANGE_IN' JSON = 'change_in' )
( ABAP = 'CHANGE_OUT' JSON = 'change_out' )
).
/UI2/CL_JSON=>SERIALIZE( EXPORTING DATA = GS_SEND PRETTY_NAME = '' NAME_MAPPINGS = LT_MAPPINGS RECEIVING R_JSON = LV_SEND ).
SELECT SINGLE * INTO @LS_URL FROM ZTSEND_URL WHERE POSOBJ = 'ZSD_100'.
SELECT SINGLE * INTO @DATA(LS_TOKEN) FROM ZTSC_TOKEN WHERE POSOBJ = 'SC1'.
*
LS_FIELDS-NAME = 'Content-Type'.
LS_FIELDS-VALUE = 'application/json'.
APPEND LS_FIELDS TO LT_FIELDS.
CLEAR:LS_FIELDS.
LS_FIELDS-NAME = 'accesstoken'.
LS_FIELDS-VALUE = LS_TOKEN-TOKEN.
APPEND LS_FIELDS TO LT_FIELDS.
CLEAR:LS_FIELDS.
*
*
CALL FUNCTION 'ZFM_RESTFUL_SEND'
EXPORTING
IV_URL = LS_URL-URL
IV_METHOD = 'POST'
IT_FIELDS = LT_FIELDS
IV_SEND_DATA = LV_SEND
IMPORTING
EV_TYPE = LV_TYPE
EV_MESSAGE = LV_MESSAGE
EV_RESPONSE_DATA = LV_RESPONSE.
/UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_RESPONSE PRETTY_NAME = 'X' CHANGING DATA = GS_RESP ).
* MESSAGE LV_MESSAGE TYPE LV_TYPE.
* ENDIF.
* ENDIF.
ENDFUNCTION.
=====================================
FUNCTION zfm_restful_send.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" VALUE(IV_URL) TYPE ZE_URL OPTIONAL
*" VALUE(IV_METHOD) TYPE STRING DEFAULT 'POST'
*" VALUE(IV_USERNAME) TYPE STRING OPTIONAL
*" VALUE(IV_PASSWORD) TYPE STRING OPTIONAL
*" VALUE(IT_FIELDS) TYPE TIHTTPNVP OPTIONAL
*" VALUE(IV_SEND_DATA) TYPE STRING OPTIONAL
*" VALUE(IV_PROXY_B) TYPE CHAR1 OPTIONAL
*" VALUE(IV_URL2) TYPE STRING OPTIONAL
*" EXPORTING
*" VALUE(EV_TYPE) TYPE BAPI_MTYPE
*" VALUE(EV_MESSAGE) TYPE STRING
*" VALUE(EV_RESPONSE_DATA) TYPE STRING
*"----------------------------------------------------------------------
TYPES: BEGIN OF ty_response,
code TYPE char1,
message TYPE string,
END OF ty_response.
DATA: ls_response TYPE ty_response.
DATA:lv_url TYPE string.
DATA:lo_http_client TYPE REF TO if_http_client,
lv_code TYPE i.
DATA:lv_proxy_host TYPE string.
DATA:lv_proxy_service TYPE string.
DATA: lv_ssl_id TYPE ssfapplssl .
IF iv_proxy_b IS NOT INITIAL.
lv_proxy_host = 'proxy'.
lv_proxy_service = '3128'.
ENDIF.
IF iv_url IS NOT INITIAL.
lv_url = iv_url.
ELSE.
lv_url = iv_url2.
ENDIF.
*创建客户端请求
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
proxy_host = lv_proxy_host
proxy_service = lv_proxy_service
ssl_id = lv_ssl_id
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
pse_not_found = 4
pse_not_distrib = 5
pse_errors = 6
oa2c_set_token_error = 7
oa2c_missing_authorization = 8
oa2c_invalid_config = 9
oa2c_invalid_parameters = 10
oa2c_invalid_scope = 11
oa2c_invalid_grant = 12
OTHERS = 13.
IF sy-subrc <> 0.
ev_type = 'E'.
lo_http_client->get_last_error( IMPORTING message = ev_message ).
ev_message = '创建代理失败!' && ev_message.
RETURN.
ENDIF.
*不显示登录界面
lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
*设置账号+密码
IF iv_username IS NOT INITIAL AND iv_password IS NOT INITIAL.
lo_http_client->authenticate( EXPORTING username = iv_username password = iv_password ).
ENDIF.
*设定调用服务
lo_http_client->request->set_method( EXPORTING method = iv_method ).
*设置Headers
lo_http_client->request->set_header_fields( EXPORTING fields = it_fields[] ).
*设置报文数据
IF iv_send_data IS NOT INITIAL.
CALL METHOD lo_http_client->request->set_cdata
EXPORTING
data = iv_send_data
offset = 0
length = strlen( iv_send_data ).
ENDIF.
*发送请求
CALL METHOD lo_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2.
IF sy-subrc <> 0.
ev_type = 'E'.
lo_http_client->get_last_error( IMPORTING message = ev_message ).
ev_message = '发送请求失败!' && ev_message.
RETURN.
ENDIF.
*读取远程服务返回的处理过结果
CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
IF sy-subrc <> 0 .
ev_type = 'E'.
lo_http_client->get_last_error( IMPORTING message = ev_message ).
ev_message = '获取返回消息失败!' && ev_message.
RETURN.
ELSE.
CALL METHOD lo_http_client->response->get_status
IMPORTING
code = lv_code
reason = ev_message.
IF lv_code BETWEEN '200' AND '299'.
ev_type = 'S'.
ev_message = '调用接口成功!'.
ELSE.
ev_type = 'E'.
ev_message = '返回码提示失败!' && ev_message.
ENDIF.
*读取返回内容
ev_response_data = lo_http_client->response->get_cdata( ).
IF ev_response_data IS NOT INITIAL.
REPLACE ALL OCCURRENCES OF '#' IN ev_response_data WITH space.
CONDENSE ev_response_data NO-GAPS.
ev_message = ev_message && ';' && ev_response_data.
ENDIF.
ENDIF.
lo_http_client->close( ).
ENDFUNCTION.
==================================
数例: | ||||
110 | ZFI_003 | SAP成本中心推送星瀚主数据平台 | http://172.16.17.220/ierp/kapi/v2/wwv2/base/bos_adminorg/bd_adminorg_save | |
110 | ZFI_028 | 成本中心推送主数据接口 | http://172.16.17.220/ierp/kapi/v2/wwv2/basedata/wwv2_bd_fi_profitcenter/bd_fi_profitcenter_save | |
110 | ZFI_028A | 公司代码映射 | http://172.16.17.220/ierp/kapi/v2/wwv2/basedata/wwv2_bd_company/bd_hr_company_query |
ZTSC_TOKEN | 获取接口TOKEN存储表 | ||||||
MANDT | MANDT | CLNT | 3 | 0 | 0 | 客户端 | |
POSOBJ | ZE_OBJECT | CHAR | 50 | 0 | 0 | 对象值 | |
TOKEN | STRING | 0 | 0 | 0 | token | ||
110 | SC | 2084196387246833664_74CFovIbX7QVBES18nXXsXfIBBNt3K8yz8ZDwmReL7lRaVxcNdNT4ORJ50nt15aEQB17kOSUQFXWAjK4y17bBqPTuqHgwRlENFzd00 | |||||
110 | SC1 | 2084196387246833664_ORuUKnPuM5Rt2Q54or0SvsN7YGueIWxvPOA8vUl8ykoRY4awNYJtCwuQh2dGXbhWls2qt8oza8H84IR4mW2PNGJQDRTXYl9KhKyr00 |
ZTSEND_URL | SAP推送数据接口地址维护表 | |||
MANDT | MANDT | CLNT | 3 | |
POSOBJ | ZE_OBJECT | CHAR | 50 | |
OBJECT_T | ZE_OBJECT_T | CHAR | 50 | |
URL | ZE_URL | CHAR | 257 | |
USERNAME | USRTX | CHAR | 40 | |
PASSWORD | CTE_PASSWORD | CHAR | 60 |