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 6 - Create a new Northbreeze category

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!)

You've made various read-only requests so far; now it's time to create some data.

Background

For this task we return briefly to the Northbreeze service from Task 2. Until now, mostly due to the nature of the deliberate simplicity of the tasks in this Developer Challenge, the actions you've been performing from an HTTP perspective have been read-only.

This time you'll write some data, albeit data that won't last long (the Northbreeze service runs in-memory and has been set up not to use a permanent persistence layer). Then you'll read it back, with a minor difference, to examine what the response looks like. It's that response, compressed, that you'll need for the hash.

The Northwind service traditionally has products, which you've encountered already. Those products are organized into categories, of which there are just a few. A category (taking the first one) looks like this:

{
  "CategoryID": 1,
  "CategoryName": "Beverages",
  "Description": "Soft drinks, coffees, teas, beers, and ales"
}

You can get a feel for the distribution of products across categories by asking for the count of products by category ID which is expressed like this (showing the path, with whitespace, for easier reading):

/odata/v4/northbreeze/Products
  ?$apply=groupby(
    (Category_CategoryID),
    aggregate($count as productcount)
  )

This returns a response that looks like this:

{
  "@odata.context": "$metadata#Products(Category_CategoryID,productcount)",
  "value": [
    {
      "productcount@odata.type": "#Decimal",
      "productcount": 12,
      "Category_CategoryID": 1,
      "@odata.id": null
    },
    {
      "productcount@odata.type": "#Decimal",
      "productcount": 12,
      "Category_CategoryID": 2,
      "@odata.id": null
    },
    {
      "productcount@odata.type": "#Decimal",
      "productcount": 13,
      "Category_CategoryID": 3,
      "@odata.id": null
    },
    {
      "productcount@odata.type": "#Decimal",
      "productcount": 10,
      "Category_CategoryID": 4,
      "@odata.id": null
    },
    {
      "productcount@odata.type": "#Decimal",
      "productcount": 7,
      "Category_CategoryID": 5,
      "@odata.id": null
    },
    {
      "productcount@odata.type": "#Decimal",
      "productcount": 6,
      "Category_CategoryID": 6,
      "@odata.id": null
    },
    {
      "productcount@odata.type": "#Decimal",
      "productcount": 5,
      "Category_CategoryID": 7,
      "@odata.id": null
    },
    {
      "productcount@odata.type": "#Decimal",
      "productcount": 12,
      "Category_CategoryID": 8,
      "@odata.id": null
    }
  ]}

As you can see, the OData V4 aggregate facilities, and CAP's support for them, is pretty neat!

Your task

Your task is to create a new category, and then request it. In other words, you must contruct an appropriate HTTP request to form an OData CREATE operation, to add a new category entity into the entity set. You can see from above that a category has just three properties:

  • CategoryID - this is the key and must be an integer value
  • CategoryName - a short name for the category
  • Description - a brief summary of what the category is

You must supply the following values for these properties when creating your new category:

  • CategoryID - your SAP Community ID number (see the Hints and tips section)
  • CategoryName - your SAP Community ID in lower case
  • Description - the static text August Developer Challenge

You can tell that the CategoryID property must be a number, because of how it's defined in the OData service's metadata document.

Once you've created your new category, you must then construct an appropriate HTTP request to form an OData READ operation to request a representation of your category. There are some important points here:

  • you must use an OData READ operation and not an OData QUERY operation
  • you must exclude the Description property, i.e. you should request (and expect) only the category properties CategoryID and CategoryName
  • the representation of the response must be in JSON

The representation of the response is the value you need to hash and share as a new reply to this discussion thread, as always, and as described in Task 0. Note that you'll have to URL encode the entire value, as you will be sending it within the value=('...') part of the hash service URL.

You should ensure that the order of the properties in the JSON object structure returned is sorted (it should already be, but double check or just add that processing to whatever mechanism you build to do this) and that there is no extraneous whitespace in the structure at all.

Here's an example of what a response representation will look like - note the single line and no whitespace, and that the CategoryName value is all lower case:

{"@odata.context":"some-context-specification","CategoryID":53,"CategoryName":"qmacro"}

Hints and tips

You already know about your SAP Community ID. For example, qmacro is the SAP Community ID for DJ Adams. To get your SAP Community ID number, which is unique, just go to your profile page, and get the number from your profile page's URL. For example, the profile page for DJ Adams is at https://groups.community.sap.com/t5/user/viewprofilepage/user-id/53 and looks like this:

qmacro-profile-page.png

You can see the SAP Community ID number in the URL - it's 53. Go to your profile page and get the number from the resulting URL for your profile.

What about the OData READ vs OData QUERY operation question? Both use the same HTTP method (GET) but the structure of the data returned is different. Given that the data and its structure is what you need to ensure is correct before hashing it, it's important you think about how you would request that single new category.

For discussion

What is the definition of the CategoryID property, according to the metadata document?

What is the difference between the result of an OData QUERY operation and the result of an OData READ operation? How does this relate to OData's origins? What is the significant difference in data structure returned?

Did you try to request a different representation of your category, such as XML? What happened?

97 REPLIES 97

andyl
Explorer
0 Kudos

I'm having problem hashing my response, it seems the '#' character in my @odata.context value is the cause:

{"@odata.context":"$metadata#Categories(CategoryID,CategoryName)/$entity","CategoryID":xxx,"CategoryName":"xxx"}

{ "error": { "code": "404", "message": "Invalid resource path \"challenge.hash(value='{\"@odata.context\":\"$metadata\"" } }

Any ideas?

qmacro
Developer Advocate
Developer Advocate
0 Kudos

Did you URL encode it before making the hash service request?

devrajsinghr
Active Participant
0 Kudos

869e0baa6fcec47b61450219cad805323f4e71f244f7102174137923bbecfc0a

jasshauer
Explorer
0 Kudos

fbcfc90e7420055cd3a74181ebbef748414178013459b95d4a92edd5375d04d3

RafkeMagic
Active Contributor
0 Kudos

ab25f131da56ebad0fe299849d3ebc125cb51a04a3f835cd074b8762c2656241

lehuynhnam
Explorer
0 Kudos

1d447aa10adfb04ec93c43afaadc5a61e07700460f69c0d67c3fc9a184ab1825

sandeepmalhotra
Participant
0 Kudos

2fa0dbf2629f1473bf136fdd36e1ee98a1202b6b85611359f0011dc1be36b137

DenisGaland
Participant
0 Kudos

d52d48634bf34276527f906b5b10e9926d8ef984bef9451c8a82469aa60cd6b3

andrew_chiam
Explorer
0 Kudos

7603b4d415e82391a81f32c8a56730164ce7860c244e37a7cceaf7b5747a91b5

nex
Explorer
0 Kudos

ebc612b4c6d61675dfd3a437b83e86cdeb5211a7e2a6ef887d387885c30142b4

adrian-ngo
Explorer
0 Kudos
3691f9eb1e59cc581b4bcef04f0928c17ccd6f64643cd1e051b1bc5fe91ccadf

OlgenH
Participant
0 Kudos

c2bf98cade10b96ccaf7bebe6bfaec1c137a269c2232d6db605c8861c0fc5554

RaulVega
Participant
0 Kudos

f9b6b95ea04fcc0f297c3154f5dc575d2d5a570aa37793047df63cb9982be378

SubaR
Explorer
0 Kudos

628c0343985413031ceab2524d45828eaa3350327b01c99a6aa0383f920f759a

sainithesh21
Active Participant
0 Kudos

35f17eaf5b342ed90fa0c57972f19f4ffbc4a5e4cb510b225040c15f9c41fe78

qmacro
Developer Advocate
Developer Advocate

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

a010da1db34d0b2d8f99c8452ef6d34f8584c04494b65e784e4dae86a34241a5

Vaibhav_Sapra
Participant
0 Kudos

fc49bd4bc31e6fd784dbbbd17db6c5a019bb7533195c6f40de058e1f86f74575