Use the Proxy generated by ABAP sproxy transaction code to consume Web Service

The ABAP source code is as follows:

REPORT zweb.

DATA: lo     TYPE REF TO zzco_prod_ws,
      input  TYPE zzcrmost__pro001prodadvsea01,
      output TYPE zzcrmost__pro001prodadvsea00.

CREATE OBJECT lo
  EXPORTING
    logical_port_name = 'LP_TEST1'.

input-input-searchforproducts-created_by-sign = 'I'.
input-input-searchforproducts-created_by-option = 'EQ'.
input-input-searchforproducts-created_by-low = 'WANGJER'.
TRY.
    lo->crmost__pro001prodadvsea001d(
      EXPORTING
        input                   = input
      IMPORTING
        output                  =  output ).

  CATCH cx_root INTO DATA(lv_text).
    DATA(ls) = lv_text->get_text( ).
    WRITE:/ ls.
ENDTRY.

DATA: ls_read_input  TYPE zzcrmost__prod_ws_read,
      ls_read_result TYPE zzcrmost__prod_ws_read_respo.
TRY.
    ls_read_input-input-prod_ws-product_id = 'ARNO_TEST004'.
    lo->crmost__prod_ws_read(
       EXPORTING
          input  = ls_read_input
       IMPORTING
          output = ls_read_result ).

  CATCH cx_root INTO lv_text.
    ls = lv_text->get_text( ).
    WRITE:/ ls.
ENDTRY.
BREAK-POINT.

Among them, class ZZCO_PROD_WS and so on are automatically generated:

[picture]

Its super Class is CL_PROXY_CLIENT:

CL_PROXY_CLIENT is an important class in SAP ABAP, used to handle the invocation and communication of external network services. It acts as a proxy between the SAP system and external systems, allowing ABAP applications to interact seamlessly with external network services. In this article, I will explain the role of CL_PROXY_CLIENT in detail, discuss its purpose and functions, and use examples to illustrate its application in real development.

The role of CL_PROXY_CLIENT

CL_PROXY_CLIENT is an ABAP class designed to simplify communication and integration with external web services. Its main functions include:

  1. Encapsulating network communication : CL_PROXY_CLIENT provides an abstraction layer that encapsulates the details of network communication with external services, allowing developers to communicate without having a deep understanding of network protocols and communication details.

  2. Protocol support : It supports multiple protocols including HTTP, SOAP, REST, etc., allowing developers to easily interact with different types of external services.

  3. Data format conversion : CL_PROXY_CLIENT is responsible for converting ABAP internal data into the format required by external services, and converting response data from external services into ABAP data.

  4. Error handling : It provides a powerful error handling mechanism, allowing developers to handle various error conditions that may occur in network communication, such as connection failure, timeout, etc.

  5. Security : CL_PROXY_CLIENT supports security features such as SSL/TLS to ensure communications with external services are secure.

Purpose of CL_PROXY_CLIENT

CL_PROXY_CLIENT has a wide range of uses in various scenarios. The following are some typical application scenarios:

  1. Integrate with external APIs : Enterprises often need to integrate with APIs from external vendors, partners, or third-party service providers. CL_PROXY_CLIENT can be used to call these APIs to implement data exchange, payment processing, logistics tracking and other functions.

  2. Web Service Call : CL_PROXY_CLIENT can be used to call SOAP or REST based web services. For example, you can use it to communicate with an external shipment tracking system to obtain real-time shipment location information.

  3. Data synchronization : In an enterprise, there may be a need to synchronize data with cloud services or other systems. CL_PROXY_CLIENT can be used to synchronize data regularly to ensure data consistency between various systems.

  4. File transfer : Sometimes it is necessary to transfer files to or get files from external systems. CL_PROXY_CLIENT can be used to handle file upload and download operations.

  5. Report generation : CL_PROXY_CLIENT can also be used to generate reports related to external systems. For example, you can write an ABAP program that uses CL_PROXY_CLIENT to obtain sales data from an external e-commerce platform and generate sales reports.

CL_PROXY_CLIENT example

Now, let's take an example to illustrate how to use the CL_PROXY_CLIENT class to call an external service. Suppose we need to get the current city's weather information from an external weather API and display it in the SAP system.

First, we need to create an instance of CL_PROXY_CLIENT and configure it to call the external API. Here is sample code:

DATA: lo_proxy_client TYPE REF TO cl_proxy_client,
      lt_parameters     TYPE tihttpnvp,
      lv_response       TYPE string.

CREATE OBJECT lo_proxy_client.

" 设置外部API的URL
lt_parameters = VALUE #( ( name = 'url' value = 'https://api.weather.com/current-weather' )
                        ( name = 'city' value = 'Berlin' ) ).

" 调用外部API并获取响应
lv_response = lo_proxy_client->send_request(
  i_http_method  = 'GET'
  it_parameters = lt_parameters ).

IF lo_proxy_client->is_successful( ) = abap_true.
  " 响应成功处理
  " 解析响应数据并在SAP界面中显示
  DATA: lt_weather_info TYPE TABLE OF ty_weather_info,
        ls_weather_info TYPE ty_weather_info.

  " 假设解析JSON响应数据的逻辑
  CALL METHOD lo_proxy_client->parse_json_response
    EXPORTING
      i_json_data = lv_response
    CHANGING
      ct_data    = lt_weather_info.

  LOOP AT lt_weather_info INTO ls_weather_info.
    WRITE: / 'City:', ls_weather_info-city,
             / 'Temperature:', ls_weather_info-temperature,
             / 'Condition:', ls_weather_info-condition.
  ENDLOOP.

ELSE.
  " 处理错误情况
  DATA: lv_error_message TYPE string.
  lv_error_message = lo_proxy_client->get_error_message( ).
  WRITE: 'Error:', lv_error_message.
ENDIF.

In the above example, we first create an instance of CL_PROXY_CLIENT lo_proxy_client and configure the URL and parameters of the external API to be called. Then, we use the send_request method to send a GET request to the external API and get the response data. If the response is successful, we parse the JSON response data and display the weather information on the SAP interface. If an error occurs, we handle the error condition and display an error message.

Please note that this is just a simple example, and actual applications may involve more complex data processing and error handling logic. CL_PROXY_CLIENT provides rich functionality that enables developers to customize and extend it according to their specific needs.

Summarize

CL_PROXY_CLIENT is a powerful tool in SAP ABAP development for handling communication and integration with external network services. It encapsulates network communication details, supports multiple protocols, and provides data format conversion and error handling functions, thereby simplifying integration with external systems. Through examples, we learned how to use CL_PROXY_CLIENT to call external APIs and process response data. In actual development, it can help enterprises achieve seamless integration with external systems and improve business efficiency and data reliability.

Guess you like

Origin blog.csdn.net/i042416/article/details/132861082