This ABAP class is designed for SAP BTP ABAP Environment to connect securely to an external OData or REST API (for example, an EHS Raw Material service).
It demonstrates:
- Secure HTTP communication via BTP destinations
- CSRF token handling
- JSON serialization/deserialization
- Clean separation between connection setup and data retrieval
Code:
CLASS /iap/cl_intelligent_matching DEFINITION
PUBLIC FINAL
CREATE PUBLIC.
PUBLIC SECTION.
“———————————————————————-
” Structure for API Response (Raw Material Compliance Info)
“———————————————————————-
TYPES: BEGIN OF ty_rawmat_result,
uuid TYPE string, ” Unique ID of the record
material_id TYPE string, ” Material number
material_name TYPE string, ” Material description
material_group TYPE string, ” Material group code
material_group_name TYPE string, ” Material group name
is_active TYPE abap_bool, ” Active status flag
END OF ty_rawmat_result.
“———————————————————————-
” Public Methods
“———————————————————————-
CLASS-METHODS:
create_client_proxy
RETURNING VALUE(ro_client_proxy) TYPE REF TO /iwbep/if_cp_client_proxy,
get_rawmat_data
RETURNING VALUE(rt_rawmat_data) TYPE STANDARD TABLE OF ty_rawmat_result,
convert_to_json
IMPORTING it_data TYPE any
EXPORTING ev_json TYPE string.
PRIVATE SECTION.
CONSTANTS:
c_destination TYPE string VALUE ‘DEMO_EHS_DEST’. ” Destination configured in BTP Cockpit
ENDCLASS.
CLASS /iap/cl_intelligent_matching IMPLEMENTATION.
“———————————————————————-
” 1. Create OData V4 Client Proxy
“———————————————————————-
METHOD create_client_proxy.
TRY.
” Create destination object from BTP destination service
DATA(lo_destination) = cl_http_destination_provider=>create_by_cloud_destination(
i_name = c_destination
i_authn_mode = if_a4c_cp_service=>service_specific ).
” Create HTTP client for communication
DATA(lo_http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_destination ).
” Create remote OData V4 proxy for the target service
ro_client_proxy = /iwbep/cl_cp_factory_remote=>create_v4_remote_proxy(
is_proxy_model_key = VALUE #(
repository_id = ‘DEFAULT’
proxy_model_id = ‘/IAP/CHM_SCM_IM’
proxy_model_version = ‘0001’ )
io_http_client = lo_http_client
iv_relative_service_root = ‘/sap/opu/odata/sap/ZDEMO_EHS_SRV/’ ). ” Demo endpoint
CATCH cx_http_dest_provider_error INTO DATA(lx_dest_err).
MESSAGE lx_dest_err->get_text( ) TYPE ‘E’.
CATCH cx_root INTO DATA(lx_root).
MESSAGE lx_root->get_text( ) TYPE ‘E’.
ENDTRY.
ENDMETHOD.
“———————————————————————-
” 2. Fetch Raw Material Data from External API
“———————————————————————-
METHOD get_rawmat_data.
DATA: lo_destination TYPE REF TO if_http_destination,
lo_http_client TYPE REF TO if_web_http_client,
lo_response TYPE REF TO if_web_http_response,
lv_json TYPE string,
lv_http_status TYPE i.
TRY.
” Obtain HTTP destination (configured in BTP cockpit)
lo_destination = cl_http_destination_provider=>create_by_cloud_destination(
i_name = c_destination
i_authn_mode = if_a4c_cp_service=>service_specific ).
lo_http_client = cl_web_http_client_manager=>create_by_http_destination( lo_destination ).
” === Step 1: Fetch CSRF Token ===
DATA(lo_req_token) = lo_http_client->get_http_request( ).
lo_req_token->set_header_field( name = ‘X-CSRF-Token’ value = ‘Fetch’ ).
DATA(lo_resp_token) = lo_http_client->execute( if_web_http_client=>get ).
DATA(lv_csrf_token) = lo_resp_token->get_header_field( ‘X-CSRF-Token’ ).
” === Step 2: Perform GET Request to Demo API ===
DATA(lo_req_get) = lo_http_client->get_http_request( ).
lo_req_get->set_header_field( name = ‘X-CSRF-Token’ value = lv_csrf_token ).
lo_req_get->set_header_field( name = ‘Accept’ value = ‘application/json’ ).
lo_response = lo_http_client->execute(
i_method = if_web_http_client=>get
i_relative_url = ‘/sap/opu/odata/sap/ZDEMO_EHS_SRV/C_RawMatInfoSet’ ). ” Demo path
lv_http_status = lo_response->get_status( ).
IF lv_http_status = 200.
lv_json = lo_response->get_text( ).
/ui2/cl_json=>deserialize(
EXPORTING json = lv_json
CHANGING data = rt_rawmat_data ).
ELSE.
MESSAGE |HTTP Error { lv_http_status }: { lo_response->get_status_text( ) }| TYPE ‘E’.
ENDIF.
CATCH cx_http_dest_provider_error INTO DATA(lx_dest_error).
MESSAGE lx_dest_error->get_text( ) TYPE ‘E’.
CATCH cx_web_http_client_error INTO DATA(lx_http_error).
MESSAGE lx_http_error->get_text( ) TYPE ‘E’.
CATCH cx_root INTO DATA(lx_any).
MESSAGE lx_any->get_text( ) TYPE ‘E’.
ENDTRY.
ENDMETHOD.
“———————————————————————-
” 3. Convert Any ABAP Data Structure to JSON String
“———————————————————————-
METHOD convert_to_json.
ev_json = /ui2/cl_json=>serialize(
EXPORTING
data = it_data
conversion_exits = abap_true
pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).
ENDMETHOD.
ENDCLASS.