In this exercise, we will create a class which leverages the HTTP connectivity classes and consume an external API from the SAP API Hub. You can view APIs of the SAP API Hub at the following URL.
https://api.sap.com/package/SAPS4HANACloud?section=Artifacts
In this exercise, we will use the Bank – Read API, which allows us to retrieve a list of bank details. This page gives you all the information about calling the API and also supplies the API key which we will need to supply when calling from our ABAP class.
https://api.sap.com/api/API_BANKDETAIL_SRV/resource
After completing these steps you will have created a class which calls the whitelisted HTTP connectivity classes.
-
Name your new class as “ZCL_API_HUB_MANAGER_XXX“. Click “Next“, then click “Finish“. Make sure to replace XXX with your group number.
-
In the PUBLIC section, define the constructor method and one other method called get_bank_details as shown here.
METHODS: constructor,
get_bank_details RETURNING VALUE(r_json) TYPE string.
- Define two private global variables as shown here.
DATA: gv_url TYPE string VALUE 'https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/'.
DATA: go_http_client TYPE REF TO if_web_http_client.
-
Next, implement the CONSTRUCTOR method. Here we will leverage the SAP delivered HTTP connectivity classes in order to estabish a connection to an external HTTP endpoint.
go_http_client = cl_web_http_client_manager=>create_by_http_destination(
i_destination = cl_http_destination_provider=>create_by_url( gv_url ) ).
-
Open the browser and go to the following URL and click “Log On“. If you don't have an account, please create one. https://api.sap.com/api/API_BANKDETAIL_SRV/resource
-
Click “Copy Key and Close“. Copy this key somewhere to use in the next step.
-
Next, implement the GET_BANK_DETAILS method. In this method, get the request object and set the header fields with the content types. Adjust the URI to include the full end point and query parameters. Also make sure that you update the APIKey value. Finally, execute the request and get the response passing it tot he returning parameter of the method.
DATA(lo_request) = go_http_client->get_http_request( ).
lo_request->set_header_fields( VALUE #(
( name = 'Content-Type' value = 'application/json' )
( name = 'Accept' value = 'application/json' )
( name = 'APIKey' value = '<insert API key here>') ) ).
lo_request->set_uri_path(
i_uri_path = gv_url && 'API_BANKDETAIL_SRV/A_BankDetail?$top=25&$format=json' ).
TRY.
DATA(lv_response) = go_http_client->execute( i_method = if_web_http_client=>get )->get_text( ).
CATCH cx_web_http_client_error.
ENDTRY.
r_json = lv_response.
After completing these steps you will have consumed an external API from the SAP API Hub, and viewed the results via the browser.
- Now return to your ZCL_HELLO_WORLD_XXX class and modify it. Add another WHEN condition to your CASE statement as shown here. Make sure to replace XXX with your group number.
when `bankdetails`.
response->set_text( new zcl_api_hub_manager_xxx( )->get_bank_details( ) ).
-
Return to the HTTP Service defintion and click the URL link.
-
When the browser opens, change the URL to include cmd=bankdetails
-
CHALLENGE! Use what you have learned and implement a new method in your ZCL_API_HUB_MANAGER_XXX class for another API. You can choose one from this page. https://api.sap.com/package/SAPS4HANACloud?section=Artifacts. Choose one that is an OData API and perhaps one that is a READ type operation
-
As your API keys are now exposed in your code, I would highly recommend that you return to your ZCL_API_HUB_MANAGER_XXX class and remove them.
You've now created a new class which calls the SAP API Hub, updated your hello World to call this new class, and viewed your results in the browser.
Continue to - Exercise 3 - ABAP RESTful Programming Model - Unmanaged