Skip to Content
Technical Articles

Consuming OData v4 Services using SAP Cloud SDK for JavaScript

Shortly after our announcement the OData v4 support in SAP Cloud SDK for Java, we released the JavaScript library for OData v4. In this blog post, I will demonstrate some of the new capabilities for generating client libraries and using OData services. First, we’ll have a look at how to get started with the tools SAP Cloud SDK provides. Then we will see some examples of OData v4 requests and in the last part, I’ll give you the links to some valuable documents and the available channels for reaching out to the SAP Cloud SDK team.

Version 4 of the OData protocol brings a lot of improvements and new capabilities and is already supported by several APIs. The SAP Cloud SDK team worked hard to provide stable support for consuming OData v4 and leveraging some new features.

SAP Cloud SDK makes it very easy for application developers to communicate with OData endpoints by providing the tools needed to generate tailored client libraries for use in Java and JavaScript (TypeScript) apps. The advantage of using such a client library is that it comes with packaged classes representing the operations and related entities of the given OData endpoint. These classes enable type-safe access for Java and TypeScript while SAP Cloud SDK takes care of the technical details and the communication channel between your app and the OData endpoint, such as authentication, destination handling, security, etc.

Before you read on: SAP Cloud SDK User Survey Q3/2020

As part of our continuous effort to improve the SAP Cloud SDK, we’d like to ask you to participate in a 10-minute survey. This is the link to the survey.

The survey is anonymous. Feel free to forward this link to other members of your team who are also using the SAP Cloud SDK.

Thank you for your participation! Your answers will help us to make the SAP Cloud SDK even better.

Generating Project Assets

If you are starting on a greenfield or just want to play with the latest SAP Cloud SDK tools you can easily start by generating a fresh and ready-to-run project on your local machine. This will allow you to directly write your business logic and execute it locally or push it to SAP Cloud Platform.

The SAP Cloud SDK command-line interface (CLI) is a free open source product and very easy to install using the npm command. Note that on UNIX based systems like macOS you might need to run the below command as root:

$ npm install -g @sap-cloud-sdk/cli

After you’ve installed the CLI you can use it to generate a Node.js project and start coding:

$ sap-cloud-sdk init [PROJECTDIR]

The SAP Cloud SDK CLI can also be very helpful if you decide to use SDK capabilities for your existing projects. Read the documentation for more details.

Generating the OData Client

In order to communicate with an OData service, we need to generate the client library for the service endpoint. Usually, APIs don’t change very often so starting up with a manual generation step using the SAP Cloud SDK CLI is perfectly fine. The CLI installation using npm is shown above.

Later, when moving towards production and everything should be automated, you can configure the generation step as part of your automated build process by invoking SAP Cloud SDK from the build script.

Every OData endpoint has an API description (.edmx file). For most SAP products, the API descriptions are also available on the SAP API Business Hub. This is the input for the OData client generator.

$ sap-cloud-sdk generate-odata-client -i service-specification/ -o odata-client/ --forceOverwrite

The generated OData client will be in the directory odata-client/, specified as value for the parameter -o in the above command.

Using the generated OData Client

Once the generation step is completed, the OData client can be used in the code. There are generated classes representing the available operations for the given service, and for the business objects, these operations work on.

The full list of protocol improvements that come with OData v4 is listed here. The SAP Cloud SDK incrementally adds support for the most used features in the SAP ecosystem. Here are examples of the first two supported capabilities.

The below examples are based on one of the reference services used in the OData documentation.

Result set expansion

The below example shows a select statement to be expanded within the results. The second select statement can be filtered with the available filtering capabilities.

/* how does expansion work in the SAP Cloud SDK for JS for OData v4 */
async function expandDemo() {
    const request = People.requestBuilder()
        .getAll()
        .select(
            People.FIRST_NAME,
            People.LAST_NAME
        )
        .expand(
            People.FRIENDS
                .select(People.USER_NAME)
                .filter(People.FIRST_NAME.equals("Scott"))
                .orderBy(asc(People.USER_NAME))
                .top(1)
                .skip(1)
        )
}

 

Complex filtering capabilities

The below snippet shows different ways to filter the result set including filter functions. Many filter functions are implemented out-of-the-box.

/* what are the filter capabilities with the SAP Cloud SDK for JS for OData v4 */
async function filterDemo() {
    const request = People.requestBuilder()
        .getAll()
        .filter(
            People.FIRST_NAME.equals("Scott"),

            People.BEST_FRIEND.filter(
                People.FIRST_NAME.equals("Russell")
            ),

            People.FRIENDS.filter(
                all(People.FIRST_NAME.equals("Russell"))
            ),

            filterFunctions.startsWith(People.FIRST_NAME, "S").equals(true)
        )
}

 

More examples will be provided as new capabilities are added to SAP Cloud SDK.

Bringing it all together

At this point, I’ve demonstrated how to generate an OData client library, construct queries, and use some new OData v4 features. As the SAP Cloud SDK adds support for additional protocol features, more details will be published on how to use them.

Now that you have a first overview of the new OData v4 support, check out the other information sources.

Do you want to learn more or have a specific question? The SAP Cloud SDK team is happy to help on cloudsdk@sap.com.

/
Be the first to leave a comment
You must be Logged on to comment or reply to a post.