SAP API Management & HANA XS - Part III

Sandro Montemezzani - 11.03.2019

Overview

In this part of the blog series, we will fill the database with tables and connect them to an OData service.

Project Package

First, we need a package for our project in the SAP Hana Editor. To create this, we log in as SYSTEM and grant our user the following rights on the root packet: REPO.READ, REPO.EDIT_NATIVE_OBJECTS and REPO.ACTIVATE_NATIVE_OBJECTS.

Now we can log back in as COMPANYDATADEV and create the companydata package in the editor.

HANA CDS

For automatic generation of the tables, we use the HANA CDS (Core Data Service) system.

For this, we create a new file TABLES.hdbdd in our newly created project package:

namespace companydata;
@Schema: 'COMPANYDATADEV'

context TABLES {
    type BusinessKey: Integer;

    entity Companies {
    key id: BusinessKey;
        key: String(20);
        long_name: String(50);
        short_name: String(50);
    } technical configuration {
        column store;
    };

    ...

}

We also need the tables Entities, Addresses and HoursOfOperation. When saving the file, the tables are automatically inserted into the database.

Tables

Now we want to make these tables accessible via an OData service. For this we create a CompanyData.xsodata file:

service namespace "companydata" {
  "COMPANYDATADEV"."companydata::TABLES.Companies" as "companies";
  "COMPANYDATADEV"."companydata::TABLES.Entities" as "entities";
  "COMPANYDATADEV"."companydata::TABLES.HoursOfOperation" as "hours";
}

This provides the 3 endpoints /companies, /entities and /hours. To be able to activate the OData service, we need to create an .xsapp file:

{
  "exposed": true,
  "authentication": [
    {
      "method": "Basic"
    }
  ]
}

The OData service is now fully set up. To test it, you can call the metadata with:

$ curl -su COMPANYDATAUSER_READONLY:********* https://hanapXXXXXXXXXXtrial.hanatrial.ondemand.com/companydata/CompanyData.xsodata/$metadata |xmllint --format -
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
  <edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="2.0">
    <Schema xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="companydata">
      <EntityType Name="companiesType">
        <Key>
          <PropertyRef Name="id"/>
        </Key>
        <Property Name="id" Type="Edm.Int32" Nullable="false"/>
        <Property Name="key" Type="Edm.String" MaxLength="20"/>
        <Property Name="long_name" Type="Edm.String" MaxLength="50"/>
        <Property Name="short_name" Type="Edm.String" MaxLength="50"/>
      </EntityType>
      <EntityType Name="entitiesType">
        <Key>
          <PropertyRef Name="id"/>
        </Key>
        <Property Name="id" Type="Edm.Int32" Nullable="false"/>
        <Property Name="key" Type="Edm.String" MaxLength="20"/>
        <Property Name="long_name" Type="Edm.String" MaxLength="50"/>
        <Property Name="short_name" Type="Edm.String" MaxLength="50"/>
        <Property Name="company_id" Type="Edm.Int32"/>
        Property Name="address_id" Type="Edm.Int32"/>
      </EntityType>
      <EntityType Name="hoursType">
        <Key>
          <PropertyRef Name="id"/>
        </Key>
        <Property Name="id" Type="Edm.Int32" Nullable="false"/>
        <Property Name="day_of_week" Type="Edm.Int32"/>
        <Property Name="opens_at" Type="Edm.Time"/>
        <Property Name="closes_at" Type="Edm.Time"/>
        <Property Name="valid_from" Type="Edm.DateTime"/>
        <Property Name="valid_until" Type="Edm.DateTime"/>
        <Property Name="entity_id" Type="Edm.Int32"/>
      </EntityType>
      <EntityContainer Name="CompanyData" m:IsDefaultEntityContainer="true">
        <EntitySet Name="companies" EntityType="companydata.companiesType"/>
        <EntitySet Name="entities" EntityType="companydata.entitiesType"/>
        <EntitySet Name="hours" EntityType="companydata.hoursType"/>
      </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

Conclusion

In the following part, we will simplify access to the OData service via API Management.

Zurück zur Übersicht