Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Exercise 3 - Service Consumption Model

In this exercise we will explore how to use the Service Consumption Model concept to consume various services in ABAP.

Exercise 3.1 Create a Service Consumption Model Definition for an OData Service

After completing these steps you will have created a Service Consumption Model definition for an OData Service exposed from the SAP API Hub.

  1. Return to the SAP API Hub, and specifically to this page which shows the overview of the Bank Details API we've been working with.
    https://api.sap.com/api/API_BANKDETAIL_SRV/overview
    Click on the "API Specification" link.

  2. Next downbload the EDMX file to your desktop.

  3. Right-click on your group package, and choose "New", then "Other ABAP Repository Object"

  4. Click "Business Services", then choose "Service Consumption Model", then click "Next".

  5. Give the name as "ZSCM_BANK_DETAILS_XXX" where XXX is your group number. Also, give a meaningful description, then click "Next".

  6. Click the "Browse" button.

  7. Navigate to the location where you saved the EDMX file eariler.

  8. Change the name of the ABAP Artifact Name which had been generated for you. Name the object as ZA_BANKDETAILS_XXX where XXX is your group number. Then click "Next".

  9. Once again, click "Next".

  10. Now click "Finish".

  11. The Service Consumption Model objects have now been created for you. When you take a look at the SCM definition, you can see that it provides code snippets for you to use in your code for different CRUD operations. Continue to the next step.

  12. Use what you have learned and create a new class called ZCL_SCM_XXX where XXX is your group number. Make sure to include the IF_OO_ADT_CLASSRUN interface and add the shell of the MAIN method implementation as shown below.

  13. In the private section, add the following lines of code. Make sure to replace XXX with your group number. We want to encapsulate the logic into a functional method call which contains a returning parameter for the list of bank details.

    TYPES tt_za_bankdetails TYPE STANDARD TABLE OF za_bankdetails_xxx WITH EMPTY KEY.
    METHODS get_bank_details_scm RETURNING VALUE(rt_table) TYPE tt_za_bankdetails.
  1. Now add the shell of the method implementation as shown below.

  2. Return to the Service Consumption Model definition and choose the "Read List" operation, then click "Copy to Clipboard".

  3. Now, paste the code into the GET_BANK_DETAILS_SCM method. Update the view name as shown. Make sure to replace XXX with your group number.

  4. Copy the following code as the first lines within the TRY statement. Here we will utilize the standard HTTP client classes provided by SAP. We will pass the URL and get an instance of the HTTP client object, where we then set the API key. Make sure to insert your API key here from the API hub.

        DATA: lv_url TYPE string VALUE 'https://sandbox.api.sap.com/'.
        lo_http_client = cl_web_http_client_manager=>create_by_http_destination(
                        i_destination = cl_http_destination_provider=>create_by_url( lv_url ) ).

        lo_http_client->get_http_request( )->set_header_fields( VALUE #(
             (  name = 'APIKey' value = '<insert API key here>') ) ).
  1. Next, modify the next statement where the client proxy object is created. Update the service root parameter value as shown here.
        lo_client_proxy = cl_web_odata_client_factory=>create_v2_remote_proxy(
          EXPORTING
            iv_service_definition_name = 'ZSCM_BANK_DETAILS_XXX'
            io_http_client             = lo_http_client
            iv_relative_service_root   = '/s4hanacloud/sap/opu/odata/sap/API_BANKDETAIL_SRV' ).
  1. Next, change the value passed to the SET_TOP method to 500. And finally, change the importing parameter value to RT_TABLE. RT_TABLE is the returning parameter of your method, so we are simply passing the results of the GET_BUSINESS_DATA method back to the caller so that we can write out the results to the console.

  2. Next, add the following line of code to the MAIN method implementatino. Here we are simply writing out the results to the console.

  3. Next, save and activate your work.

  4. Now execute your class, but hitting F9. You should see a list of bank details in the console.

  5. Return to your class and uncomment the following lines of code. Make sure to update the LT_RANGE_BANKCOUNTRY range table definition as shown here. Be sure to replace XXX with your group number. Also, be sure to put a period at the end of this line as well.

  6. Uncomment the following lines of code. Here we will use the filtering classes to help us filter the results.

  7. Add the following line of code, in between the two statements that you have just uncommented. Here we are simply filling the range table with a filter value. We want to filter the results where BANKCOUNTRY = DE.

        lt_range_BANKCOUNTRY = VALUE #( ( sign = 'I' option = 'EQ' low = 'DE' high = ' ' ) ).
  1. Next, uncomment these two lines of code, and since we only have one filtering node, update these two statements as shown here.

  2. You completed code should now look like this.

CLASS zcl_scm_xxx DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.

    INTERFACES if_oo_adt_classrun .
  PROTECTED SECTION.
  PRIVATE SECTION.

    TYPES tt_za_bankdetails TYPE STANDARD TABLE OF za_bankdetails_xxx WITH EMPTY KEY.
    METHODS get_bank_details_scm RETURNING VALUE(rt_table) TYPE tt_za_bankdetails.

ENDCLASS.



CLASS zcl_scm_xxx IMPLEMENTATION.

  METHOD if_oo_adt_classrun~main.

    out->write( get_bank_details_scm(  ) ).

  ENDMETHOD.

  METHOD get_bank_details_scm.


    DATA:
      lt_business_data TYPE TABLE OF za_bankdetails_xxx,
      lo_http_client   TYPE REF TO if_web_http_client,
      lo_client_proxy  TYPE REF TO /iwbep/if_cp_client_proxy,
      lo_request       TYPE REF TO /iwbep/if_cp_request_read_list,
      lo_response      TYPE REF TO /iwbep/if_cp_response_read_lst.

    DATA:
      lo_filter_factory    TYPE REF TO /iwbep/if_cp_filter_factory,
      lo_filter_node_1     TYPE REF TO /iwbep/if_cp_filter_node,
* lo_filter_node_2    TYPE REF TO /iwbep/if_cp_filter_node,
      lo_filter_node_root  TYPE REF TO /iwbep/if_cp_filter_node,
      lt_range_BANKCOUNTRY TYPE RANGE OF za_bankdetails_xxx-BankCountry.
* lt_range_BANKINTERNALID TYPE RANGE OF <element_name>.


    TRY.
        " Create http client
*DATA(lo_destination) = cl_http_destination_provider=>create_by_comm_arrangement(
*                                             comm_scenario  = '<Comm Scenario>'
*                                             comm_system_id = '<Comm System Id>'
*                                             service_id     = '<Service Id>' ).
*lo_http_client = cl_web_http_client_manager=>create_by_http_destination( lo_destination ).

        DATA: lv_url TYPE string VALUE 'https://sandbox.api.sap.com/'.
        lo_http_client = cl_web_http_client_manager=>create_by_http_destination(
                        i_destination = cl_http_destination_provider=>create_by_url( lv_url ) ).

        lo_http_client->get_http_request( )->set_header_fields( VALUE #(
             (  name = 'APIKey' value = '<insert APi key here>') ) ).

        lo_client_proxy = cl_web_odata_client_factory=>create_v2_remote_proxy(
          EXPORTING
            iv_service_definition_name = 'ZSCM_BANK_DETAILS_XXX'
            io_http_client             = lo_http_client
            iv_relative_service_root   = '/s4hanacloud/sap/opu/odata/sap/API_BANKDETAIL_SRV' ).


        " Navigate to the resource and create a request for the read operation
        lo_request = lo_client_proxy->create_resource_for_entity_set( 'A_BANKDETAIL' )->create_request_for_read( ).

        " Create the filter tree
        lo_filter_factory = lo_request->create_filter_factory( ).

        lt_range_BANKCOUNTRY = VALUE #( ( sign = 'I' option = 'EQ' low = 'DE' high = ' ' ) ).

        lo_filter_node_1  = lo_filter_factory->create_by_range( iv_property_path     = 'BANKCOUNTRY'
                                                                it_range             = lt_range_BANKCOUNTRY ).
*lo_filter_node_2  = lo_filter_factory->create_by_range( iv_property_path     = 'BANKINTERNALID'
*                                                        it_range             = lt_range_BANKINTERNALID ).

        lo_filter_node_root = lo_filter_node_1.
        lo_request->set_filter( lo_filter_node_root ).

        lo_request->set_top( 500 )->set_skip( 0 ).

        " Execute the request and retrieve the business data
        lo_response = lo_request->execute( ).
        lo_response->get_business_data( IMPORTING et_business_data = rt_table ).

      CATCH /iwbep/cx_cp_remote INTO DATA(lx_remote).
        " Handle remote Exception
        " It contains details about the problems of your http(s) connection

      CATCH /iwbep/cx_gateway INTO DATA(lx_gateway).
        " Handle Exception

    ENDTRY.

  ENDMETHOD.

ENDCLASS.
  1. Next, save and activate your work.

  2. Finally, execute the class once again. The results should now be shown with only rows where the BANKCOUNTRY = DE.

Summary

You've now created your a Service Consumption Model for consuming an external service.