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!

148 REPLIES 148

0 Kudos

e8dd07547b7e628bf74acc300753688b755a70e4f6f7bb79dc9d7dd5c9c3b82b

encarrero
Participant
0 Kudos

94db4d0ba27248445f04e6dee0ecbb5781c28f11e1a52b54f0a1a8eec0845ff0

Ashok459
Participant
0 Kudos

8a991cfd93056a4af91a91b20815f087a6d0aaf85945372a75920bb859ff86a3

thomas_jung
Developer Advocate
Developer Advocate

66b979bf93e4d6db7fbc641d1354b2b40cd986f5da85d0154b7a255b97ec91ec

stephancalmindo
Explorer
0 Kudos

8f1082acdb448aa00c9d7a20c287d2c1e5bb47619b03d2a2f346a906cb8c4107

martinstenzig
Contributor
0 Kudos

374411565d1c07c8185210fb62442365b4fdd9b1b371c3b9f8bc5b7d07890d9d

ibibhu
Product and Topic Expert
Product and Topic Expert
0 Kudos

99ce990b6d8a454f60fefcaa8c14382a662c2da41803a8720b715212b23cddcd

kjyothiraditya
Participant
0 Kudos
c70627add3398fcfddb79d2fb066356bae0094e9a0bd8113795d488c51772a94

yassine
Explorer
0 Kudos

f05f403935d026fa532001f7bd6567cbfe512aaf0dd3e1e2b3e4243898260754

nmkarthikeya
Active Participant
0 Kudos

7dfca63542cb8c07e67b5508e5e6aef3f4f94094a4d3bef4b6511350660a3f4b

PriyankaChak
Active Contributor
0 Kudos

9bcad559a929770e67821f870533ceef14c594ad185478f92a88c0a4c60ddaa8

flo_conrad
Explorer
0 Kudos

45f8fff39fa351d2a3c69fe9568a4ebb6a7fb4cc4e9d2da6c3274340a584dba5

AbapAndy728
Explorer
0 Kudos

cf7fc357cc9b1125c5debe01b80837e6c556e9f14b416b2e94fbfbd2e3962be9

turanaydin
Explorer
0 Kudos

4b7f0091fe6ed513e9bdff3dc3169c472f49753c9a94fb6da5f4f6790ff62793

Jitendra_Kansal
Product and Topic Expert
Product and Topic Expert
0 Kudos

3f83a22fb9926bcbf41ae629a9b607f568649bbb6f068c225929880596abd4c2

Regards, Jitendra

satya-dev
Participant
0 Kudos

1249f3517bfbd10e64851624bf2db414ca536eb21de1a91da66b3da7d67c12a7

MioYasuatke
Active Contributor
0 Kudos

04750d1fa658168905141be991df86c29b6971d2908eaa50a70d323ec98996ea

sabarna17
Contributor
0 Kudos

40fdfb92964a0203ccd00c00f076e3a4e9bad2675fa3b623e298e1827f1eda20

qmacro
Developer Advocate
Developer Advocate
0 Kudos

This is lovely, thanks for sharing.

jasshauer
Explorer
0 Kudos

023bf0cf0748383e35cf36bdf5e20e2c2a8fdb2d3170c052c7d074cf6bbba88a

0 Kudos

never used actions in odatav4 before 🙂 thanks for little learning task

qmacro
Developer Advocate
Developer Advocate
0 Kudos

Great! Actions and functions are fascinating, even if they allow for non-RESTful operations in the end 😉

FooThePolarBear
Explorer
0 Kudos

e9dd3c18d6f3b994d51742e73c029f87afac29c401676105b03467be0d5f80e5

ManojSutar
Explorer
0 Kudos

d9656e9387128da357354cad97f9aabaaaf73580c4c37be02c57d15cb0e2bdfb

TiagoAlmeida
Participant
0 Kudos

d4418d228dd7d47980fcdd1f94add2435a23b180324272cf329e5b2e3d1a037c

Suneeth_P
Participant
0 Kudos

f9b35ebf27029944badc33add278220359bf52b3b3482ca93a6320b741ce373f

jens_borau
Explorer
0 Kudos

392285c6174aa574ab729a2d628cd3113eb211f57d1e9c6e4ba307596bb92bff

szeteng00
Explorer
0 Kudos

48df06aead71121b625cfac90aea681163a3e4a5d7149afa2ab2966d585ce5e0

SyambabuAllu
Contributor
0 Kudos

77e60b2ba9779d39687fb9bfe41e6e8c87408c5620138f76d7368f99f4df22a7

DenisGaland
Participant
0 Kudos

82cd37ffa390ea37b05cd945618c865ac487f12ab0e63b3a34bb4a70866d8475

ramana_salapu
Explorer
0 Kudos

3367607988590114560c5d91631b12100afa230f4a3e522087b3bcca2cb46d59

ilyass
Explorer
0 Kudos

8a0b658bd6cb72c1a34c0959f0bb4fffd28e909cf5cc29288e76ccee3b6adbc2

MarcelloUrbani
Active Contributor
0 Kudos

9b06c695c3dd61654f383ea75ddec5df205c98bc3649f1677325f786a37d6de9

ofilho
Discoverer
0 Kudos

d355ffa4a307cd9bcec1d85e3c1aa859f71821256aa83e9661eda148edd36647

hunamm
Explorer
0 Kudos

eec2bfd35cb4a746145514e72e823f6ef58ddb801aaa38c7c871ee2171e4c186

vinaychowdary
Explorer
0 Kudos

6dfa697e5660ba069efce1ac1dd4b2d5a7f8fc3e0f9dd241b4d33a4260af7416

tly
Explorer
0 Kudos

a06a6e9e91df34921c5e533311f85d46a99c8d33cb45ed37e34a4f20121abc6a

jmuiruri
Product and Topic Expert
Product and Topic Expert
0 Kudos

3802f91dbc03aef487d0096b1dd69b0a71ef524c94ccef8507bf8ee8fdcdb53f

erickgrilo
Explorer
0 Kudos

65d09696ba014e8bb6bf5123070a9bcf78729cba643aaab208d2e108d9c9b978