Dynamic HTTP or Odata Adapter – SAP Cloud Integrat…

In this blog, I aim to simplify the use of dynamic resources/querying in Odata or HTTP.

To make it clearer for you, let’s imagine the following scenario:

You need to perform a GET request in Odata on the S4hana Public Cloud, S4hana Private Cloud, or S4hana On-Premise, and you want to use the query options in a very dynamic and simple way.

Examples of queries:

https://myXXXXXX-api.s4hana.cloud.sap/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('17300002')

https://myXXXXXX-api.s4hana.cloud.sap/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner?$filter=BusinessPartnerGrouping eq 'BP03'

Many times, we want to call SAP Cloud Integration – CPI from a SAP or non-SAP system, passing these parameters. How can we make CPI accept this without giving an HTTP 404 error?

This is what I will teach you step-by-step in this blog, using the Odata Adapter and HTTP Adapter.

Let’s get to work!

Spoiler alert: the end of our journey

001.jpg

Step 1º:

The first thing we will do is put an asterisk ‘*’ in the Address field of our HTTP adapter.

Example: /testURL*

002.jpg

With this in the CPI monitor, we will have a URL like the one below:

https://9265fbe0trial.it-cpitrial05-rt.cfapps.us10-001.hana.ondemand.com/http/testURL*

Step 2º:

Configure the Router component.

Here we are using a Header to check whether to execute oData or HTTP; this step is entirely optional. I did it this way for didactic purposes, to show that it is possible to use both the odata adapter and the HTTP adapter. In this blog, you will see the difference between the two.

003.jpg

Step 3º:

Create a local Integration Process.

004.jpg

Step 4º:

Inside the local Integration Process, we will create a router. In this Router, we will check a standard CPI Header. If CPI is called with the URL below, it fills the CamelHttpPath.

https://myXXXXXX-api.s4hana.cloud.sap/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('17300002')

In this case, we are searching for a record by its key field. So we will use the READ route.

005.jpg

Step 5º:

We will add a Request Reply in the iflow.

006.jpg

Step 6º:

We will add the Odata adapter.

If you don’t have an S4hana environment to use, you can use the Sandbox from SAP Business Accelerator Hub.

Link to API Business Partner: https://api.sap.com/api/API_BUSINESS_PARTNER/overview

If you have doubts about using the SAP Business Accelerator Hub, here are links to explanatory videos:

– Link in Portuguese (Brazil) – Video 1

– Link in Portuguese(Brazil) – Video 2

– Link in English – Video 1

– Link in English – Video 2

In the Address field, you either put the address of your S4hana environment or the SAP Sandbox.

https://myXXXXXX-api.s4hana.cloud.sap/sap/opu/odata/sap/API_BUSINESS_PARTNER

https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner

You should create your credentials in CPI, whether they are Basic, Oauth2, or even API Key (the latter should be passed in the header as Authorization).

007.jpg

In the Processing tab, select Operation Detail as READ(GET).

In the Resource Path, add the entity plus the header that contains the key field data.

Example: A_BusinessPartner${header.CamelHttpPath}

008.jpg

Step 7º:

Now we will practically repeat the step to create another Request Reply and odata adapter.

The only additional point is that we will add a Groovy script to check if the CamelHttpQuery header is empty. If it is empty, we will create a property called isEmptyHeader and set it to ‘true’; otherwise, it will be ‘false’.

009(1).jpg

009(2).jpg

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
    
def Message processData(Message message) {    
 //Headers
 def headers = message.getHeaders();
 def isEmptyHeader = headers.get("CamelHttpQuery") == null || 
 headers.get("CamelHttpQuery").toString().isEmpty()

 message.setProperty("isHeaderEmpty", isEmptyHeader)

return message;
}

the Router we check this property.

009(3).jpg

If it follows the empty route, then we will create this header with a blank value using the content modifier.

009(4).jpg

Notice that in the Connection tab, it is identical to the previous one.

009(5).jpg

In the Processing tab, add the standard CPI header CamelHttpQuery.

This header is filled automatically when we send a URL like the one below, which is done when we pass a FILTER in the URL.

https://myXXXXXX-api.s4hana.cloud.sap/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner?$filt… eq ‘BP03’

009(6).jpg

The oData part is now done. Now let’s see how it works with HTTP.

Step 8º:

Now we will create the Local Integration Process to use with the HTTP adapter.

Here is a detail: it is necessary to copy a header to a property because we need to delete the value of this header.

010.jpg

In this Content Modifier, we are moving a header to a property.

011.jpg

In this Content Modifier, the CamelHttpPath header is being deleted. This step is mandatory; if not done, it will cause an error in the HTTP call. I recommend you test without deleting it to understand why we remove it.

012.jpg

Step 9º:

Now we will add a Request Reply and use the HTTP adapter.

013.jpg

In the HTTP adapter, we will configure it as follows. It will be the address of your S4hana endpoint or Sandbox, as we saw earlier, plus the property we created in the Content Modifier.

https://myXXXXXX-api.s4hana.cloud.sap/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner${prop…

Change the Method to GET.

Add the credentials to access the environment. If using an API Key, add the Authorization header.

014.jpg

Our development is now complete.

Now let’s test using POSTMAN.

Test 1º:

We will call the Odata adapter without passing any parameters or filters. In this case, we want the Odata to return all the records in the database.

015.jpg016.jpg

Test 2º:

Calling by passing the key field in the URL. In this case, it will follow the READ route.

017.jpg018.jpg

Test 3º:

In this test, the Filter is being tested in the URL.

018(1).jpg

With this, the iFlow went through the QUERY route.

018(2).jpg

Test 4º:

In this test, we are testing the HTTP adapter without sending any filter or key. We expect the return to be all BP records from S4hana.

019.jpg020.jpg

Test 5º:

In this test, we will test the HTTP adapter by passing a Key in the URL to return only one record. The adapter should perform a “READ“.

Important: In this test, the CamelHttpPath header is filled, which is why we delete it in the Content Modifier. The HTTP adapter uses it automatically if it exists. Test to form your own conclusions!

021.jpg022.jpg

Test 6º:

We will test the HTTP adapter by passing a filter in the URL.

023.jpg024.jpg

Do you want continue learning about SAP Cloud Integration, then follow me in the Social Medias:

👉YouTube Channel:

https://www.youtube.com/@NeylonSAP

👉Instagram:

https://www.instagram.com/neylonsap/

👉Linkedin:

https://www.linkedin.com/in/neylonsap/

👉Course Page Linkedin:

https://www.linkedin.com/company/neylon-silva-cpi/?viewAsMember=true

Source link

Leave a Comment