Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

SAP Developer Challenge - SAP Cloud Application Programming Model (Week 4)

nicoschoenteich
Developer Advocate
Developer Advocate
16,225

Please note that this challenge is closed. The deadline for receiving a badge upon successful completion has passed. Check out this new challenge for the month of August.

Welcome to the fourth and final week of this month's SAP Developer Challenge. This week we are going to learn about consuming remote services with the SAP Cloud Application Programming Model.

If you haven't read the announcement blog post for this challenge, please head over and do so. This week's challenge builds on top of previous three challenges, which you should have completed before starting with this one.

The Challenge

We have already come a long way with our application since week 1 of the challenge. We have a solid data model, load initial data and can track golf rounds, but still there is one central component missing - the golf players. Wouldn't it be nice to track who played a round of golf? The simplest approach here would be to create a new entity in our data model, but I think we should use this as an opportunity to learn how to consume remote services. The remote service that we are going to consume exposes the API of this SAP Community Groups platform, because the players will be you folks! We will integrate an remote service that lists everyone who completed week 1 of this challenge successfully, and these community members will be the "golf players".

This is what you have to do to successfully complete this week's challenge:

1. Check out the service that we are going to consume: https://developer-advocates-free-tier-central-hana-cloud-instan3abe9a0e.cfapps.us10.hana.ondemand.co...

2. Copy the following metadata information from the remote service into a new file RemoteService.edmx in the root of your project. CAP will need this information to know how to integrate the remote service:

 

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
    <edmx:Reference Uri="https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Capabilities.V1.xml">
        <edmx:Include Alias="Capabilities" Namespace="Org.OData.Capabilities.V1"/>
    </edmx:Reference>
    <edmx:Reference Uri="https://sap.github.io/odata-vocabularies/vocabularies/Common.xml">
        <edmx:Include Alias="Common" Namespace="com.sap.vocabularies.Common.v1"/>
    </edmx:Reference>
    <edmx:Reference Uri="https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Core.V1.xml">
        <edmx:Include Alias="Core" Namespace="Org.OData.Core.V1"/>
    </edmx:Reference>
    <edmx:DataServices>
        <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="CatalogService">
            <EntityContainer Name="EntityContainer">
                <EntitySet Name="Players" EntityType="CatalogService.Players"/>
            </EntityContainer>
            <EntityType Name="Players">
                <Key>
                    <PropertyRef Name="name"/>
                </Key>
                <Property Name="name" Type="Edm.String" Nullable="false"/>
            </EntityType>
            <Annotations Target="CatalogService.EntityContainer/Players">
                <Annotation Term="Capabilities.DeleteRestrictions">
                    <Record Type="Capabilities.DeleteRestrictionsType">
                        <PropertyValue Property="Deletable" Bool="false"/>
                    </Record>
                </Annotation>
                <Annotation Term="Capabilities.InsertRestrictions">
                    <Record Type="Capabilities.InsertRestrictionsType">
                        <PropertyValue Property="Insertable" Bool="false"/>
                    </Record>
                </Annotation>
                <Annotation Term="Capabilities.UpdateRestrictions">
                    <Record Type="Capabilities.UpdateRestrictionsType">
                        <PropertyValue Property="Updatable" Bool="false"/>
                    </Record>
                </Annotation>
            </Annotations>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>

 

3. We can now properly import this metadata into our project (CDS import API) by running the following command from the root of our project: cds import RemoteService.edmx 

4. Explore the newly generated srv/external/ directory. It contains the previously used edmx file as well as a newly generated Schema Notation (CSN) file, which is a "notation for compact representations of CDS models".

5. Inspect the changes that were made to the package.json. There was a new cds.requires.RemoteService section added to make CDS aware of the remote service we imported and are about to use in the next steps.

6.  Add a new file .env  (don't forget the leading dot) to the project root. This file is usually used to define environment variables that will automatically get picked up by CDS. Although we will not store real credentials in this file (or anywhere else in this project), this file would be a good place to do that. It is important to never commit this file to any source code control system (like git).

7. Add the following code to the newly created .env, which adds the service url as "credentials" to the remote service definition:

 

cds.requires.RemoteService.credentials.url=https://developer-advocates-free-tier-central-hana-cloud-instan3abe9a0e.cfapps.us10.hana.ondemand.com/browse/

 

8. Replace the content of the srv/cat-service.cds with the following code. This service definition uses the remote service (line 2) and integrates its "Players" entity into our CatalogService (line 5):

 

using { golf } from '../db/schema';
using { RemoteService as external } from './external/RemoteService';

service CatalogService @(path:'/browse') {
  entity Players as projection on external.Players;
  entity Rounds as projection on golf.Rounds;
  entity Holes as projection on golf.Holes;
  entity Shots as projection on golf.Shots;
}

 

9. Add the following code to the main exported function of the srv/cat-service.js, which is our service handler file from last week. This code makes sure that all incoming request to the "Players" entity will in fact get forwarded to the RemoteService:

 

const remote = await cds.connect.to('RemoteService')
this.on('*', 'Players', (req) => {
    console.log('>> delegating to remote service...')
    return remote.run(req.query)
})

 

10. Start the cds server like usual and inspect the console output. You should see that CDS is connecting to the url of the RemoteService.

11. Share a screen shot of the CatalogService running on your localhost and exposing the remote "Players" entity with its data. It should look something like this:

2023-07-26_11-30-35.png

Resources

We have gathered a few helpful resources for this week's challenge. Feel free to use the comments sections if you have question or need help.

Further Learning

As this week marks the end of this SAP Developer Challenge, here is some inspiration for how you could further improve the application:

 

We hope you enjoyed this SAP Developer Challenge and learned something new. Feel free to share feedback and your experience in the comment section down below or via a private message!

Good luck and happy learning!

102 REPLIES 102

Ashok459
Participant
5,088

Here is my submission for week 4 :

Ashok_Easa_0-1690402005686.png

Regards,

Ashok

0 Kudos
3,923

Well done

AslanCakirdogan
Participant
4,975

every ending is a new beginning, no stop keep going, thank you @nicoschoenteich 

aslan1906_0-1690411099428.png

 

0 Kudos
3,874

Well done

choujiacheng
Explorer
4,762

choujiacheng_0-1690437371597.png

Hi, my browser Microsoft Edge does not have the same JSON parsing tools built-in, or at least one that I 'am aware of, but the output should be the same. Is this alright?

0 Kudos
3,868

Well done

marcinb
Explorer
4,754

hello!

This is my submission for week 4:

marcinb_0-1690438340161.png

marcinb_1-1690438355375.png

 

3,871

Well done

4,747

My submission for week 4

santoshmurmu_0-1690440924949.png

Console

santoshmurmu_1-1690440965377.png

 

 

0 Kudos
3,873

Well done

MarcelloUrbani
Active Contributor
4,709

Here is my submission:

Had to run the following to make it work (using cds-ts as I hate vanilla javascript):

npm i --save @Former Member-cloud-sdk/resilience
npm i --save @Former Member-cloud-sdk/http-client  

MarcelloUrbani_0-1690449520618.png

 

0 Kudos
3,876

Well done

v-tryputsina
Product and Topic Expert
Product and Topic Expert
4,661

I also had a problem with modules  @Former Member-cloud-sdk/resilience and @Former Member-cloud-sdk/http-client . If anybody has such problem, just install these modules.

vtryputsina_0-1690458976216.png

 

 

4,417

yes, I did the same. this was one thing that I learned in the 4th challenge.

I believe anyone who are new in CAP (like me) and setup a local project by themselves may experience this problem. 

0 Kudos
3,879

Well done

stickman_0x00
Participant
4,651

stickman_0x00_0-1690460414512.png

🙂

0 Kudos
3,879

Well done

Former Member
4,565

Last submission:

Afonso_Pimentel_0-1690475842055.png


Console:

Afonso_Pimentel_1-1690475862264.png

Thank you for the challenge, it was certainly interesting, mainly for a beginner like me!

0 Kudos
3,879

Well done

Aliaksandr_Ch
Product and Topic Expert
Product and Topic Expert
4,515

Hi @nicoschoenteich ,
Week 4 task:

Screenshot 2023-07-27 at 20.03.13.png

0 Kudos
3,878

Well done

bztoy
Participant
4,444

Hi @nicoschoenteich ,

This is my submission for week#4 challenge.

cap-code-challength-week-04-query-result-vscode.png

Once again, I have learned so much from this challenge. Thanks everybody especially the developer advocate team to bring this idea on the table to help us in SAP development learning journey.

 

Thanks,

Wises

3,900

Well done, I am glad you enjoyed it

johna69
Product and Topic Expert
Product and Topic Expert
4,484

Fun challenge thank you

Screenshot 2023-07-28 at 1.46.44 AM.png

0 Kudos
3,960

Well done

govardhansahil
Explorer
4,460

Hi @nicoschoenteich 

Here is my submission for week 4. I started learning CAPM during this challenge and it was fun. Thanks to the developer advocate team for the challenge.

govardhansahil_1-1690531703885.png

 

0 Kudos
3,962

Well done

dhegde
Participant
4,302

Hi @nicoschoenteich 

Herei s my submission for week 4 (I did complete week 1 😎)

dhegde_1-1690595387758.png

dhegde_2-1690595747517.png

 

I was getting this error while runnign the application - had to add '@sap-cloud-sdk/http-client' package to make it work

dhegde_0-1690595360988.png

Thanks @nicoschoenteich  for these challenges.  They were a great starting steps to CAP introducing many important concepts.  All the while, I couldn't help but compare it to how RAP development workflow is.    

Regards

Dhananjay

0 Kudos
3,962

I am glad you got it to work, well done

nmkarthikeya
Active Participant
4,275

My Submission for week 4. Thank you!

nmkarthikeya_0-1690608884533.png

 

0 Kudos
3,966

Well done

MeriemSouissi
Participant
3,997

My week 4 submission 

MeriemSouissi_0-1690618017910.png

My Github repo for this project : https://github.com/meriemso/cap_challenge_07_23

Thanks for this challenge !

0 Kudos
3,966

Well done

andrew_chiam
Explorer
3,987

My week 4 submission. Look forward to your verification 😃

Week 4 Challenge.PNG

0 Kudos
3,966

Well done

dinah
Product and Topic Expert
Product and Topic Expert
3,977

My week 4 submission:

dinah_0-1690755182098.png

 

0 Kudos
3,966

Well done

cd_winc
Explorer
3,973

Hi Nico,

Thanks for organizing the challenge.

Here is my submission.

w4.PNG

0 Kudos
3,968

Well done

VenugopalA
Explorer
3,971

Hi @nicoschoenteich ,

My Submission for week #04

VenugopalA_0-1690786358981.png

 

thank you.

venu