SAP releases simple REST II: interface configuration of API platform

Recently, I met an awesome business consultant. The demand is 10000 times worse than the user's direct statement, so I have more free time. Being idle is also idle. Come and do something fun.

API interface platform design: (full JSON format)

ABAP operators all know that the general interface processing is to encapsulate a function for separate processing. Can these functions be configured? In what form are parameters passed in and out?

ps: so far, I just have some ideas, so now and later articles may be improvised. Just look at the bad places, the good places... You don't have to pay.

 

The API for user authentication was designed in the front. Later, I tried token, but it didn't seem to work. Therefore, it is either interface verification or login verification in the future.

Let's talk about the design idea: (the first version of the idea may be updated later)

1. Set the function configuration table corresponding to the interface parameters:

2. In the command API, obtain URL parameters, BODY parameters, HEADER parameters, etc., and match the configuration table in 1

3. Dynamically call the corresponding function in 2 and process the return parameters.

 

OK, let's start...

 

1, Create a configuration table and configure the corresponding processing functions of the interface:

 

 

2, Create implementation class: follow the previous ZCRM_REST class, modify the processing method.

  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,
          gw_api    TYPE zapi_control.

    FIELD-SYMBOLS: <fs_field>       LIKE LINE OF lt_fields.

    lv_ifid = server->request->get_uri_parameter( name = 'IFID' )."GET INTERFACE ID
    lv_method = server->request->get_header_field( name = '~request_method' )."GET INTERFACE METHOD
    "CHECK CONTROL TABLE
    SELECT SINGLE * INTO gw_api 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_api-zflag."used flag
        WHEN 'X'.
*""GET HTTP MESSAGE BODY
          lv_data = server->request->if_http_entity~get_cdata( ).

          "call method
          CALL METHOD (gw_api-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->set_cdata(
            EXPORTING
              data   = lv_retu    " Character data
          ).

  ENDMETHOD.

Test:

 

Here, the parameters of the function are strictly limited. If there is time later, you can use param table... Use it like this first.

Posted by kyme on Mon, 16 May 2022 23:54:14 +0300