【SAP ABAP】调用API接口实例

2021-12-30 10:12:42 浏览数 (1)

代码语言:javascript复制
FUNCTION zhri_rfc_uim_creat_id .
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(IT_DATA) TYPE  ZHRS_UIM_CREAT_T
*"  EXPORTING
*"     REFERENCE(OT_MSG) TYPE  ZHRS_UIM_CREAT_MSG_T
*"----------------------------------------------------------------------
*--------------------------------------------------------------------*

*定义json用的表
  DATA: lt_data TYPE TABLE OF zhrs_uim_creat,
        ls_data TYPE  zhrs_uim_creat.
  DATA: lrf_descr TYPE REF TO cl_abap_typedescr,
        lv_json   TYPE /ui2/cl_json=>json.
  DATA: len         TYPE        i, "发送报文长度
        len_string  TYPE        string,
        url         TYPE        string, "接口地址
        host        TYPE        string,
        http_client TYPE REF TO if_http_client, "http客户端
        post_string TYPE        string,
        result      TYPE        string.
  DATA:lv_name_password TYPE string,
       lv_md5           TYPE md5_fields-hash,
       lv_str_md5       TYPE string.
  DATA:lv_code    TYPE sysubrc,
       lv_message TYPE string.
  DATA: it_header  TYPE tihttpnvp.
  DATA:iv_name     TYPE string, "用户名
       iv_password TYPE string. "密码
  TYPES:BEGIN OF ty_token,
          token       TYPE string,
          code        TYPE string,
          description TYPE string,
        END OF ty_token.
  DATA:ls_token TYPE ty_token.
  TYPES:BEGIN OF ty_result,
          code        TYPE string,
          description TYPE string,
          result      TYPE zhrs_uim_result,
        END OF ty_result.
  DATA:ls_result TYPE ty_result.
  DATA:ls_result_d TYPE zhrs_uim_result_s."详细结果
  DATA: lv_tokentype TYPE char1,
        lv_tokenmsg  TYPE string.
  DATA:ls_msg TYPE zhrs_uim_creat_msg.
  DATA:ls_t004 TYPE zhrt_004.

  "URL地址
  SELECT SINGLE * INTO ls_t004 FROM zhrt_004
    WHERE sysid = sy-sysid
    AND type  = 'UIM'.
  MOVE ls_t004-url TO host."地址
  MOVE ls_t004-uname TO iv_name."用户名
  MOVE ls_t004-password TO iv_password."密码
  IF host IS NOT INITIAL.
***************************记录日志************************************
    zlylog 'ZHRI_RFC_UIM_CREAT_ID' '01' ''."#记录日志

**************************地址获取账号密码获取**************************

**********************************************************************
*1  获取token  start
**********************************************************************
    CONCATENATE host '/api/Auth/GetToken' INTO url.
    "创建http客户端
    CALL METHOD cl_http_client=>create_by_url
      EXPORTING
        url                = url
      IMPORTING
        client             = http_client
      EXCEPTIONS
        argument_not_found = 1
        plugin_not_active  = 2
        internal_error     = 3
        OTHERS             = 4.
*创建传输内容的类型和格式
    http_client->request->set_content_type( content_type = 'application/json; charset=utf-8' ).
*  http_client->request->set_method( if_http_request=>co_request_method_post ).
    http_client->request->set_method( if_http_request=>co_request_method_get )."yong get 方法
*加密用户名和密码~~MD5加密
    CONCATENATE iv_name ':' iv_password INTO lv_name_password .
    CALL FUNCTION 'MD5_CALCULATE_HASH_FOR_CHAR'
      EXPORTING
        data   = lv_name_password
        length = 0
      IMPORTING
        hash   = lv_md5.
    MOVE lv_md5 TO lv_str_md5 .
**设置请求head
    CALL METHOD http_client->request->set_header_field
      EXPORTING
        name  = 'Authorization'
        value = lv_str_md5.
    "发送
    CALL METHOD http_client->send
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2
        http_processing_failed     = 3
        http_invalid_timeout       = 4
        OTHERS                     = 5.
    "接收
    CALL METHOD http_client->receive
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2
        http_processing_failed     = 3.

    IF sy-subrc <> 0.
      lv_tokentype = 'E'.
      lv_tokenmsg = '获取UIMtoken失败,请检查传入账号密码以及连接状态'.
    ELSE.
      result = http_client->response->get_cdata( ).
***解析返回json到内表
      /ui2/cl_json=>deserialize(
      EXPORTING json = result
        pretty_name = /ui2/cl_json=>pretty_mode-camel_case
      CHANGING data = ls_token ).
      IF ls_token-token IS INITIAL.
        lv_tokentype = 'E'.
        MOVE ls_token-description TO lv_tokenmsg.
      ENDIF.
    ENDIF.
**********************************************************************
*    获取token结束
**********************************************************************
**********************************************************************
*    调用创建函数开始
**********************************************************************
    IF ls_token-token IS NOT INITIAL.
      CLEAR url.
**拼接创建地址
      CONCATENATE host '/api/ADUser/CreateADUser' INTO url.
      CALL METHOD cl_http_client=>create_by_url
        EXPORTING
          url                = url
        IMPORTING
          client             = http_client
        EXCEPTIONS
          argument_not_found = 1
          plugin_not_active  = 2
          internal_error     = 3
          OTHERS             = 4.
***设置方法
      http_client->request->set_method( if_http_request=>co_request_method_post ).
***设置head
      http_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ).
      http_client->request->set_header_field( name = 'Authorization' value = ls_token-token ).
*    APPEND iv_data TO lt_data.
****设置请求body
      lv_json = /ui2/cl_json=>serialize( data          = it_data
                                       pretty_name   = /ui2/cl_json=>pretty_mode-camel_case
                                       compress      = abap_true
                                       ).
***设置长度
      len = strlen( lv_json ).
***设置数据
      CALL METHOD http_client->request->set_cdata
        EXPORTING
          data   = lv_json
          offset = 0
          length = len.
***发送数据
      CALL METHOD http_client->send
        EXCEPTIONS
          http_communication_failure = 1
          http_invalid_state         = 2
          http_processing_failed     = 3
          http_invalid_timeout       = 4.
    ENDIF.
***获取消息
    CALL METHOD http_client->get_last_error
      IMPORTING
        code    = lv_code
        message = lv_message.
***返回结果
    CALL METHOD http_client->receive
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2
        http_processing_failed     = 3.
***捕获返回消息
    CALL METHOD http_client->get_last_error
      IMPORTING
        code    = lv_code
        message = lv_message.
    CLEAR result .
    result = http_client->response->get_cdata( ).
***解析到内表
    /ui2/cl_json=>deserialize(
    EXPORTING json = result
      pretty_name = /ui2/cl_json=>pretty_mode-camel_case
    CHANGING data = ls_result ).
*  MOVE ls_result-description TO ov_msg .
    LOOP AT it_data INTO ls_data.
      MOVE ls_data-_sam_account_name TO ls_msg-perid.
      CLEAR ls_result_d.
      READ TABLE ls_result-result INTO ls_result_d
                                  WITH KEY samaccountname = ls_data-_sam_account_name.
      IF sy-subrc = 0.
        ls_msg-ov_msgty = 'S'.
        ls_msg-ov_msg =  '生成成功'.
      ELSE.
        IF lv_tokentype = 'E'.
          ls_msg-ov_msgty = 'E'.
          MOVE lv_tokenmsg TO ls_msg-ov_msg.
        ELSE.
          ls_msg-ov_msgty = 'E'.
          ls_msg-ov_msg =  '创建失败'.
        ENDIF.
      ENDIF.
      APPEND ls_msg TO ot_msg.
      CLEAR ls_msg.
    ENDLOOP.
*  IF ls_result-code NE '000000'.
*    ov_msgty = 'E'.
*  ELSE.
*    ov_msgty = 'S'.
*  ENDIF.
    zlylog 'ZHRI_RFC_UIM_CREAT_ID' '02' ''."#记录日志
  ENDIF.

ENDFUNCTION.

0 人点赞