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 - APIs - Task 3 - Have a Northbreeze product selected for you

qmacro
Developer Advocate
Developer Advocate

(Check out the SAP Developer Challenge - APIs blog post for everything you need to know about the challenge to which this task relates!)

In this task you'll learn a bit about actions and functions in OData V4.

Background

While OData V2 did have function imports, the advent of OData V4 brought about enhancements in this area and we now have a clean distinction between different types of orthogonal endpoints that can be presented in an OData service alongside the entity sets, singletons and more. See the Actions and functions sections of the OData V4 and SAP Cloud Application Programming Model talk slides for more info.

With CAP's support for OData V4's actions and functions ready for us to enjoy, it's a good opportunity to try things out here.

In addition to the entity sets we saw in the previous task, the Northbreeze OData V4 service also sports an action. If you check the service's metadata document document you'll see the evidence of this action.

First, it appears alongside the EntitySet elements within the EntityContainer element:

<EntityContainer Name="EntityContainer">
  <EntitySet Name="Products" EntityType="Northbreeze.Products">
    <NavigationPropertyBinding Path="Category" Target="Categories"/>
    <NavigationPropertyBinding Path="Supplier" Target="Suppliers"/>
  </EntitySet>
  <EntitySet Name="Suppliers" EntityType="Northbreeze.Suppliers">
    <NavigationPropertyBinding Path="Products" Target="Products"/>
  </EntitySet>
  <EntitySet Name="Categories" EntityType="Northbreeze.Categories">
    <NavigationPropertyBinding Path="Products" Target="Products"/>
  </EntitySet>
  <EntitySet Name="Summary_of_Sales_by_Years" EntityType="Northbreeze.Summary_of_Sales_by_Years"/>
  <EntitySet Name="TotalProducts" EntityType="Northbreeze.TotalProducts"/>
  <ActionImport Name="selectProduct" Action="Northbreeze.selectProduct"/>
</EntityContainer>

The action itself is described in more detail along with the EntityType elements. Here it is in full:

<Action Name="selectProduct" IsBound="false">
  <Parameter Name="communityid" Type="Edm.String"/>
  <ReturnType Type="Edm.String"/>
</Action>

You can see from this detail that:

  • it's an unbound action
  • it expects a single parameter communityid
  • it returns a result

Basically, this action expects to receive your SAP Community ID as input, and, based on that ID, will choose a product for you.

Actions vs functions: One of the key differences between actions and functions in OData V4 is that while functions may not have side effects, actions may well have side effects. This means that normally functions will be used for read-only operations, while actions can be used for operations that make some sort of change. But they don't have to. And here, we're using an action, rather than a function, mostly because actions require the POST HTTP method (because they may have side effects), rather than GET. And requiring you to use HTTP POST for this task makes things more interesting.

Your task

Your task is to call this unbound action, supplying your SAP Community ID for the communityid parameter. The response you will receive is a JSON representation, containing a value property, the value for which is the product that has been selected for you (based on your SAP Community ID).

This is the value you must hash and then post that hash as a new reply to this discussion thread, as described in Task 0 - Learn to share your task results.

Here's an example. Calling the action with qmacro as the value for communityid causes this to be returned in response (pretty printed for readability):

{
  "@odata.context": "$metadata#Edm.String",
  "value": "Rössle Sauerkraut"
}

The product name Rössle Sauerkraut is what needs to be hashed. Note, in this case, that any value passed in the URL path must be properly URL encoded. So while the value test, for example, needs no specific URL encoding, this value must be URL encoded, in order to be successfully transported in the URL path and accurately received and parsed by the hash service. In other words, the value:

Rössle Sauerkraut

needs to be URL encoded so it looks like this:

R%C3%B6ssle%20Sauerkraut

When inserted into the URL path, it will therefore look like this:

/v1/hash(value='R%C3%B6ssle%20Sauerkraut')

Hints and tips

You may want to peruse the content of the talk OData V4 and SAP Cloud Application Programming Model, in particular the short Actions and functions section.

You may also wish to watch the replay of the Hands-on SAP Dev live stream episode Back to basics: OData - the Open Data Protocol - Part 5 - Actions & functions.

You can find out what URL encoding is all about in the Wikipedia entry for URL encoding. You're likely to find either a built-in or a library function in your favorite language to do that, for example JavaScript has encodeURIComponent and Python has a quote function in urllib.parse.

Finally, you may wish to read this short post OData query operations and URL encoding the system query options with curl, written in part as a response to a great comment on the previous task.

For discussion

What's the difference between the meanings of "bound" and "unbound" in this context? What does the fact that the selectProduct action is "unbound" suggest to us?

Remember, if you're posting your thoughts on these discussion questions, remember to do it in a reply separate from your hash reply!

149 REPLIES 149

garyzuo
Explorer
0 Kudos

8d7481273a8fc527b4a2fc2e038601252a4c3c313d55660b5f0a18fa8b7b9160

ecem_yalim
Explorer
0 Kudos

a463827dcf760a0bb1b229e988400cc136b9f49cb36039faaa660944b2aa80ae

kamesh-sap
Explorer
0 Kudos

9c0063ebbdd760e9a8691dcb0395bd2f33fb873dfdf948944e29bed32d1a91af

cdias
Product and Topic Expert
Product and Topic Expert
0 Kudos

9dc71d616522053568faac49ec6230a919f5da8d16b446525a16b5ff01450906

IsmailFatih
Explorer
0 Kudos

7c33faee12ce633aee27b2c076dfcc439674d5dd5de1197871a836763a4b13e3

rohantiwari
Participant
0 Kudos

f65528ab3ee972370ad0d562df898d8583b1365cf7b41d067d79c7230628a045

salilmehta01
Associate
Associate
0 Kudos

43839e4c00e4342f930536b6daf9edaf80ae930338699886fe32f4105614587d

tobiasz_h
Active Participant
0 Kudos

907acbb5cde7a4af92cbe69b5ce9a90f31b1d8d4d9779fee5d14bf3bed0985a7

SubaR
Explorer
0 Kudos

aa74d3fddfae2c9c443fd1ff326546c61700efb31bfabcc11e4e14d58e4e999f

Nicolas
Active Contributor
0 Kudos

d6d7c2010cf0b157632a5c7f947dbe731be7fbc0b14f57c248f0f5beef60c814

Sudhakaran
Participant
0 Kudos

57086e804421574d975ef1f0dd4c72f3ac885be4bac20f28c04c364e8477ca5d

former_member136915
Product and Topic Expert
Product and Topic Expert
0 Kudos
1d7f9bff01c4cb1d0734a2a2b2c05914c2137770b19383d95be1bc96cd778909

devrajsinghr
Active Participant
0 Kudos

9e92c80bab1e0b5f7a286c565e511dfda9d3d949c1deb7790c0d01eb442047a4

I am assuming my community id is my user name - devrajsingh (Initially I used my sap user id which was Pxxxxxx)

qmacro
Developer Advocate
Developer Advocate
0 Kudos

Double check the instructions on how to reply with your hash 🙂

Cmdd
Participant
0 Kudos

9f33b69135c4ff5b8809c3c7823a7fe45cd4c0bbf9bf82ae40b91286d8a50a06

ec1
Active Participant
0 Kudos

8781d5896978b381ff1381a7b51e9991746b99d0531bf271adeef816c7716ede

RafkeMagic
Active Contributor
0 Kudos

019b987b37de51b3bc43ea8b3ad0a6cb132cf99aa69e3e0965da1128b400ff64

spassaro
Participant
0 Kudos

ca0a5143e2a02a3042dded95f70ac2f520f93e57b013d4bbdde3f6598489c91c

bugsanderrrors
Participant
0 Kudos

620b4d5017f8b474dacd14b3b0c243aad092be10e60b08800fa679da0f81179a

johna69
Product and Topic Expert
Product and Topic Expert
0 Kudos

4de3efbf98c62330f2088fcb41200b3c9bc704495c4a11d37aaeda1b0d738282

lehuynhnam
Explorer
0 Kudos

d5f5359c528a5d4548a461c4615faaf85a63d372b5f94293ac70c2c6b633d878

andrew_chiam
Explorer
0 Kudos

d0a7fabc10e71da08d1cf7b01c7290ada6c5f344d5cb9bb04779919601d1a06b

adrian-ngo
Explorer
0 Kudos

e9317e7e6079c93e2a66aadf2d248e068ecce5f152273873e3d1d62b8da4c391

OlgenH
Participant
0 Kudos

0488af4bd29141eff8836152a738075f7127070fb849c5e00e644c96df87064c

lvhengel
Participant
0 Kudos

71135d446878e5b0dbcb625d6b9de31783c52d83922e92e86ef64fc00542a3c1

FabienHenique
Participant
0 Kudos

5cc6f2a69725c57bd7b3992be5826b8725dba21f31aa922ea5a45125cb9a0ffe

sainithesh21
Active Participant
0 Kudos

69c687eb7786aad7b2fad8262016e193c026009f4fd26c5dfb215f469460532f

qmacro
Developer Advocate
Developer Advocate
0 Kudos

Hey everyone! The challenge to which this task belongs is now officially closed. Head over to the original blog post SAP Developer Challenge – APIs to check out the closing info and final statistics, and to see your name in lights! 🎉  And we thank you all for participating, you made this challenge great!

buz
Explorer
0 Kudos

f079479e19b5eddf23cbe2c78281ca60a4a01e8eaa00580aeaf6ae43bab0dbe9

Vaibhav_Sapra
Participant
0 Kudos

67bd1177b1fb52fbd98008c89eb76f231731ae75346af5c83dba18337bb07039