SAP调用第三方API

SAP调用第三方API

前言

根据业务需求,需要和第三方的数据来支持。这时需要SAP请求外部的封装好的API,当然SAP调用webservice也是一种方法。请求API必定涉及到网络请求,这里记录下网络请求的方式。为了方便API统一管理,这里以SAP的RFC函数为例。

正文

1、创建http服务

2、设置http request的参数

3、把数据转换成json

4、发送http请求

5、接收返回的数据

FUNCTION z_get_api.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(I_URL) TYPE  STRING OPTIONAL
*"     VALUE(LV_JSON_STR) TYPE  STRING OPTIONAL
*"     REFERENCE(I_DJLX) TYPE  ZSEND_LOG-ZDJLX OPTIONAL
*"  EXPORTING
*"     REFERENCE(LV_RESPON) TYPE  STRING
*"----------------------------------------------------------------------

  DATA: json_ser TYPE REF TO cl_trex_json_serializer,
        json_des TYPE REF TO cl_trex_json_deserializer.
  DATA: lv_len TYPE i."发送报文长度
  DATA: lv_subrc TYPE sy-subrc.
  "DATA: lv_json_str TYPE string. "传入的Json
  "DATA: i_url TYPE string. "URL地址
  DATA: lv_msgty TYPE c,
        lv_msgtx TYPE string.
  DATA:wa_tab TYPE zsend_sms_data.
  DATA:p_text(2000) TYPE c.
  DATA:p_flag(1) TYPE c.
  DATA:len TYPE i .
  DATA:g_lines3 TYPE i.
  DATA:l_url TYPE string .
  DATA:strreq  TYPE string.
  DATA:p_tstamps LIKE tzonref-tstamps .
  DATA:split1 TYPE  string."分割字符串
  DATA:split2 TYPE  string.
  DATA:split3 TYPE  string.
  DATA:split4 TYPE  string.
  DATA:s_object LIKE tnro-object.

  DATA zsendlog LIKE TABLE OF zsend_log WITH HEADER LINE.
  DATA wa_send_log TYPE zsend_log.

  "Example样例
  "lv_json_str ='{"elsAccount":701015,"appId": els_test,"appSercet":edkihj34238ikjsdn23hl1od3445luwwasdfpsdfdf2 }'.
  "lv_json_str ='{ "databaseType":3,"operationType": 4,"sql":"SELECT CHDW,MRKWH,WPMC,MRCKH,GG FROM IIMH WHERE WPH = ''1050000001'' " }'.

  DATA: http_client_data TYPE REF TO if_http_client.
  "URL地址
  "i_url  = 'http://xxx.com/els-interface-base/rest/restAPI/'.

  "创建实例
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url    = i_url
    IMPORTING
      client = http_client_data.

  CALL METHOD http_client_data->request->set_header_field
    EXPORTING
      name  = '~server_protocol'
      value = 'HTTP/1.1'.
  "设定传输请求内容格式以及编码格式
  CALL METHOD http_client_data->request->set_header_field
    EXPORTING
      name  = 'Content-Type'
      value = 'text/json; charset=utf-8'.
  "定义请求方式,这里是GET请求方式,如果是POST替换
  CALL METHOD http_client_data->request->set_header_field
    EXPORTING
      name  = '~request_method'
      value = 'GET'.

  "设定调用服务
  "如果是POST方式换成http_client_data->request->set_method( if_http_request=>co_request_method_post ).
  http_client_data->request->set_method( if_http_request=>co_request_method_get ).
  

  "设置待传输内容
  lv_len = strlen( lv_json_str ).
  CALL METHOD http_client_data->request->set_cdata
    EXPORTING
      data   = lv_json_str
      offset = 0
      length = lv_len.

  "设置待传输内容
  CALL METHOD http_client_data->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
  IF sy-subrc <> 0.
    "操作失败,获取失败原因
    http_client_data->get_last_error( IMPORTING message = lv_msgtx ).
    lv_msgty = 'E'.
    EXIT.
  ENDIF.

  "读取远程服务返回的处理过结果。
  CALL METHOD http_client_data->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
  IF sy-subrc <> 0 .
    http_client_data->get_last_error( IMPORTING message = lv_msgtx ).
    lv_msgty = 'E'.
    EXIT.
  ELSE.
    "读取返回返回内容
    CLEAR lv_respon.
    lv_respon = http_client_data->response->get_cdata( ).
"lv_respon为API请求返回的数据

*-------------------------------记录请求API请求日志消息-------------------------------------------*
    "获取日志表流水号
    CLEAR s_object.
    s_object = 'ZTDLOG'.
    PERFORM frm_get_number USING s_object wa_send_log-zsendbh.

    "获取当前登录人账号
    DATA: wesuser TYPE sy-uname.
    "时间转化,获取时间戳
    CALL FUNCTION 'IB_CONVERT_INTO_TIMESTAMP'
      EXPORTING
        i_datlo     = sy-datum
        i_timlo     = sy-uzeit
      IMPORTING
        e_timestamp = p_tstamps.

    "短信发送返回消息
    zsendlog-zsendbh = wa_send_log-zsendbh."流水号
    zsendlog-zdjlx = i_djlx.               "表单类型 
    zsendlog-ernam = sy-uname.             "创建人
    zsendlog-erdat = sy-datum.             "创建日期
    zsendlog-uzeit = sy-uzeit.             "创建时间
    zsendlog-tzntstmps = p_tstamps.        "时间戳
    zsendlog-message = lv_respon.          "API返回消息

    MODIFY zsend_log FROM zsendlog.
    CLEAR:zsendlog.
*-------------------------------记录请求API请求日志消息-------------------------------------------*

  ENDIF.

ENDFUNCTION.
上一篇:智汇华云 | ArcherOS Stack共享存储虚拟化技术剖析


下一篇:LVM逻辑卷管理器