Technical Articles
How to use the OpenAPI client Generator of SAP Cloud SDK
Note: Have a look at the SAP Cloud SDK documentation portal to obtain more insights about the SAP Cloud SDK in general
Introduction
Many SAP solutions including SAP S/4HANA expose their data via the OData protocol at the same time various services in SAP Cloud Platform use OpenAPI to deliver their services, for example, the Workflow service on SCP.
For simplifying the consumption of OData services, SAP Cloud SDK provides a mature OData type-safe client generator..
To solve a similar problem for service providing OpenAPI 2.0 and 3.0 specification a Beta version of OpenAPI client generator was released together with the SAP Cloud SDK 3.34.0.
For ease of use a maven plugin is offered that can be used to generate a client library for a given OpenAPI specification.
The SAP Cloud SDK OpenAPI client generator wraps the public open-source OpenAPI generator to seamlessly integrate it with other features of the SDK.
The generated client library is type safe and already implicitly uses SDK core features like built-in multitenancy and automated destination retrieval.
To top that, choosing to use the OpenAPI client generator means that you would also be getting best in class support for your applications directly from the SAP Cloud SDK development team.
During the recent SAP TechEd 2020, the OpenAPI client generator was showcased by my team (SAP Cloud SDK for Java). Our customers tried it out in our learning session Extend SAP S/4 HANA to meet Unique Business Needs the Cloud-Native Way under the Digital Transformation with Intelligent ERP track. You’ll find the introduction deck in our documentation and in this blog we will take a look at the exercise.
Let’s look at the example that was illustrated in the TechEd and implement an API call to SAP Omnichannel Promotion Pricing service by generating a client library from the API specification available on SAP API Business Hub.
Getting Started
- Clone this repository and open it in your IDE.
- You can either run this application locally by:
cd path/to/application/folder/application mvn spring-boot:run
- Or push the application to SCP Cloud foundry by
Logging into CFcf login
and pushing the app using
cd path/to/application/folder/application cf push
This pushes the application named “webshop” into your space inside your sub-account of SCP CF. Note that you can configure parameters for deployment in
manifest.yml
.
Generate the client library
- Open the SAP Omnichannel Promotion Pricing page on SAP API Business Hub and log on using your trial credentials. Click Download Specification and select YAML from the menu and save the downloaded file as
PriceCalculation.yaml
. - Create a new subdirectory called
api-specs
within the application folder inside the cloned project. Add the filePriceCalculation.yaml
file to this newly created folder. - Open the
pom.xml
that lies in your application folder. - Add the following client library generator plugin configuration at the end of the plugins section of your pom.xml:
<plugin> <groupId>com.sap.cloud.sdk.datamodel</groupId> <artifactId>rest-generator-maven-plugin</artifactId> <version>3.34.0</version> <configuration> <inputSpec>${project.basedir}/api-specs/PriceCalculation.yaml</inputSpec> <apiPackage>com.sap.cloud.sdk.generated.promopricing.api</apiPackage> <modelPackage>com.sap.cloud.sdk.generated.promopricing.model</modelPackage> <outputDirectory>${project.basedir}</outputDirectory> <apiMaturity>released</apiMaturity> </configuration> <executions> <execution> <id>generate-pp-client</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin>
- Run the client library generator by executing the following commands in your terminal.
cd path/to/application/folder/application mvn rest-generator:generate@generate-pp-client
Now you have generated the client library for SAP Omnichannel Promotion Pricing.
Implement the promotions controller
Next you need to write the program code for the API call.
- Open the class
PromotionController
in your IDE - Add the following imports at the top of the class file:
import com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor; import com.sap.cloud.sdk.cloudplatform.connectivity.HttpDestination; import com.sap.cloud.sdk.generated.promopricing.api.CalculationApi; import com.sap.cloud.sdk.generated.promopricing.model.LineItemDomainSpecific; import com.sap.cloud.sdk.generated.promopricing.model.Percent.ActionEnum; import com.sap.cloud.sdk.generated.promopricing.model.PriceCalculate; import com.sap.cloud.sdk.generated.promopricing.model.PriceCalculateResponse; import com.sap.cloud.sdk.generated.promopricing.model.ShoppingBasketBase;
- Insert the following lines of code inside the
calculatePromotion()
method:HttpDestination destination = DestinationAccessor.getDestination("PROMOPRICING").asHttp(); String tenantName = "techedtenant"; CalculationApi calculationApi = new CalculationApi(destination); PriceCalculateResponse ppResponse = calculationApi.calculateViaRestWithTenant(tenantName, new PriceCalculate()); PromotionResponse response = convertResponse(ppResponse); return ResponseEntity.ok(response);
- Add the method
convertResponse()
with the following implementation:private PromotionResponse convertResponse(PriceCalculateResponse ppResponse) { ShoppingBasketBase shoppingBasket = ppResponse.getPriceCalculateBody().get(0).getShoppingBasket(); BigDecimal promoValue = new BigDecimal(0); for(LineItemDomainSpecific lineItem: shoppingBasket.getLineItem()) { promoValue = promoValue.add(lineItem.getSale().getRetailPriceModifier().get(0).getPercent().getValue()); } promoValue = promoValue.divide(new BigDecimal(shoppingBasket.getLineItem().size())); return new PromotionResponse(ActionEnum.SUBTRACT.toString(), promoValue); }
Configure the SAP Promotion Pricing destination in your IDE
After completing these steps you will be able to run the example application in your IDE and access SAP Promotion Pricing.
- Run the following command in the terminal view.
export destinations='[{"type": "HTTP", "name": "S4HANA", "url": "https://odata-mock-server-hilarious-tiger.cfapps.sap.hana.ondemand.com/"},{"type": "HTTP", "name": "PROMOPRICING", "url": "https://mock-server-cx-spontaneous-fossa.cfapps.sap.hana.ondemand.com/sapomnichannelpromotionpricing/calculation"}]'
- Run the application in your IDE to see the changes.
cd path/to/application/folder/application mvn spring-boot:run
Configure the SAP Promotion Pricing destination on SAP Cloud Platform
After completing these steps you will be able to run the example application on SAP Cloud Platform and access SAP Promotion Pricing.
Configure the SAP Promotion Pricing destination on SAP Cloud Platform
- Create a destination service instance by running the following command in the terminal. Let’s call the service instance
destservice
.cf create-service destination lite destservice
- Bind the destination service instance
destservice
created in the above step to the webshop application. Note that this step will only work if you have already deployed the application to cloud foundry at least once.cf bind-service webshop destservice
- Configure the SAP Promotion Pricing connection details. Open the SAP Cloud Platform Cockpit and navigate to the subaccount overview page, select Destinations from the menu on the left, then click New Destination. The destination configuration will appear. Enter the following details and click Save.
- Name: PROMOPRICING
- URL: https://mock-server-cx-spontaneous-fossa.cfapps.sap.hana.ondemand.com/sapomnichannelpromotionpricing/calculation
Build the application again with the changes and deploy the application by running the following commands in the terminal.
cd path/to/application/folder mvn package cf push
Copy the application URL from the output of the above commands(the URL listed in routes) and open it it a new browser tab.
Conclusion
That’s it.You’ve now implemented the API call to SAP Omnichannel Promotion Pricing. To achieve this, you downloaded the API specification from SAP API Business Hub and used the SAP Cloud SDK OpenAPI client generator to generate the classes for the API call. Finally you configured the destination in your IDE and on SAP Cloud Platform and deployed an application which leverages them to SCP CF.
Please note that the OpenAPI client generator is still in Beta, so it is still not recommended for productive usage. You can try it out by generating a type-safe client for any OpenAPI 2.0 or 3.0 specification. Follow this guide in our documentation portal for a step by step process.
Feedback
Feedback regarding the OpenAPI client generator or also any other topic pertaining to the SAP Cloud SDK is always appreciated and welcome.
You can reach us on Stack Overflow or ask in the SAP community by adding the SAP Cloud SDK tag or also create issues on our external Github repository.
Also, for reporting an issue via BCP choose the XX-S4C-SDK component.
Great Blogpost!
Thanks for sharing!
Regards,
Martin