OData – Back to basics?

The title is somewhat misleading – but nevertheless it’s a great way to kickstart OData.
This are the notes on “Back to basics: OData – the Open Data Protocol” by DJ Adams.

For an overview on OData see: SAP Community: Daniel Purucker – Howto OData – High level overview
Community-Site: https://community.sap.com/topics/gateway
CAP: https://cap.cloud.sap/docs/
OData specification: https://www.odata.org/
MS Learn OData: https://learn.microsoft.com/en-us/odata/

Part 1 – Introduction

Repo: https://github.com/SAP-samples/odata-basics-handsonsapdev

npm install

Check the installation:

git version
node -v
ui5 --version
cf --version
cds --version

If needed install the missing components:

npm install --global @ui5/cli
npm install --global @sap/cds-dk

Then, after deploying to a local SQLite database, start the service up:

cds deploy --to sqlite
cds run

Consider using a dev-container:

My Repo: https://github.com/dapuru/devcontainer
My Doku: https://blog.daniel-purucker.com/vscode-devcontainer-part-1/
Or use: https://github.com/features/codespaces as well as https://www.gitpod.io/

Part 2 – Basics operations

Open OData-service in: http://localhost:4004

Useful Plugins (included in the dev-container):
CDS graphical modeler in VSCode
CDS graphical modeler in BAS
OData CDSL modeler – to visualize edmx-files

Important files:
– /db/data/schema.cds
defines the entities
– /srv/main.cds
says: using northwind from ‘../db/schema’;
The service is definded as “service NorthwindModel” – CAP creates a service named northwind-model from this.

Requests in UI5 when calling the odata-service

  • GET
  • the model metadata

  • HEAD
  • CSRF-Token for CORS

  • POST
  • $batch to request the data GET *bundled*. – Oposite than single requests, when “groupId”: “$direct” is set in the UI5 manifest.jsonfor the model.

Link: Details on HTTP-Protocol… to come

Operations – CRUD+Q:
– *Read*: select a single entity (old style and new style – 3 is the id here)

url/entity(3)
url/entity/3

– *Query*: select multiple entities (top 3) or filter

url/entity?$top=3
url/entity?$filter=Discontinued%20eq%20false

Part 3 – System query options

Look at the oasis-open.org specification

Examples for system query options:

– $count=true
get the number of returned records included in the result set
– url/entity/$count
get the number of records as integer
– url/entity/item/$value
get the “raw” data-value
– $top and $skip
server-side paging
– url/entity?$expand=item
show relations inline – in the same resource representation
eg.: /Products(1)?$expand=Category
– $filter

See a definition of all system query options:
Specification – URL conventions

System query options vs. custom query options:
https://learn.microsoft.com/en-us/odata/concepts/queryoptions-overview

Examples:
https://www.odata.org/getting-started/basic-tutorial/

Part 4 – All things $filter

https://github.com/SAP-samples/odata-basics-handsonsapdev/blob/main/filter.md

Part 5 – actions and functions

https://docs.oasis-open.org/odata/odata/v4.0/odata-v4.0-part3-csdl.html
https://cap.cloud.sap/docs/guides/providing-services#actions-and-functions

– Function
read-only, quote from the sepc: “functions **MUST NOT** have observables side effects”. http: GET.
Types: bound vs. unbound (see: [https://cap.cloud.sap/docs/cds/cdl#actions](https://cap.cloud.sap/docs/cds/cdl#actions))
Functions are *defined* at service level.

function randomProduct() returns Product;

Functions are *implemented* at service level eg. in java-script. Here in srv/main.js
https://github.com/SAP-samples/odata-basics-handsonsapdev/blob/main/srv/main.js
Call via: url/service/function-name()
here: http://localhost:4004/northwind-model/randomProduct()

– Action
does something – CreateUpdateDelete on the server, quote from the spec: “actions **MAY** have observable side effects”. http: POST.
Definition and Implementation same as in Functions.

Part 6 – Wrapping up

Annotations

Exploring Annotations

How to control the level of metadata:

$format=application/json;odata.metadata=none
$format=application/json;odata.metadata=minimal
$format=application/json;odata.metadata=full

(What controls the level of metadata in an OData response?

Consume on-prem ABAP-based OData service

– using the Cloud connector
– eg use ES5 system as described in this Cloud connector Code Jam (Archive)

Consuming an on-prem ABAP-based OData service
How to guide – Connectivity Setup with ABAP and SAP Cloud Connector

Consume cloud ABAP OData service
todo

You may also like...

1 Response

  1. November 22, 2022

    […] The last blog-posts laid the theoretical foundation for the OData protocol: https://blogs.sap.com/2022/01/22/howto-odata-high-level-overview/ https://blog.daniel-purucker.com/odata-back-to-basics/ […]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.