Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
ElijahM
Advisor
Advisor

This will be a short post (in terms of words, but long with pictures). I had been getting bored of generating SalesOrder and BusinessPartner demos, as sometimes that seems to be all that I ever see (not without good reason obviously), and longed to do something a little more fun. Then I stumbled across denise.nepraunig.sap and her awesome Apps built around Star Wars and Chuck Norris Jokes :grin: .

Since these were built on top of APIs, I couldn't help but want to put SAP API Management in there, and be part of the fun. So while 99% of the work was done by Denise, I still wanted to share how to put SAP API Management in between, and possibly let the community imagine a fun extension from there.

TOC

Deploying the WebIDE App

1) Start by downloading Denise's Zipped Chuck Norris App

2) Either extract the file "chucknorrisjokes" and import it into your HCP destinations. Or generate manually as below.

3) Open WebIDE in your HCP Account

4) File ~ Import ~ From File System

5) Browse to locate the zip you just downloaded, then click OK

6) Right-click the folder you imported ~ Deploy ~ Deploy to SAP HANA Cloud Platform

7) Choose project name and Application name, then click Deploy

😎 Open active version of the App to verify it works

Generating the API Proxy


Ok so far we have just run the original app, direct to the API with no changes. Let's get SAP API Management in the picture so I have a reason to post.

1) Open SAP API Management API Portal (If you aren't sure how, start here)

2) Hamburger Menu ~ Manage

3) Create New API

4) Create API screen with details
URL: http://api.icndb.com
Name/Title/API Base Path: Your choice
Click "Create"

5) Save and Deploy the API

6) Copy API Proxy URL Value:

7) Now that we have an SAP API Management API Proxy pointing to Chuck Norris API, let's pull the ol switcheroo on our HCP Destination.

Open your HCP Cockpit ~ Destinations and Edit the chucknorrisjokes destination. Replace http://api.icndb.com with the URL you just copied. (You may need to enable Default JDK truststore as well.)

Click "Save"

That's it! The app was designed to work with the relative HCP destinations, and your destination is now pointing to the SAP API Management API Proxy URL, which in turn is exposing the Chuck Norris API, so everything should work just as before. Give it a whirl, reload your Chuck Norris HCP App, and see if it still works.
If you'd like some more assurance that API Management is indeed doing something, you can return to your chucknorrisjokes API Proxy, and click "Launch Debug Viewer" ~ "Start Debug" and then run "Get a new joke" in the App. Refresh the Debug viewer and you should see a new call.

(OPTIONAL) That was fun, but so what?


As you see above, everything worked exactly the same before and after, except now for some reason we pointed to SAP API Management instead of directly to the API. Well, now I challenge you to come up with some interesting uses of the Management layer now sitting on top of the Public API. denise.nepraunig.sap has made a fairly robust App, so we can poke around and not mess things up too much, so I'll provide a simple example.


Imagine that we want to make our app available publicly, leveraging Chuck Norris jokes, but we want to be good netizens, and not overload the DB with requests. Well we can add a Quota policy to our API Proxy, to block requests above a certain threshold, something we did not have the ability to do before. Now we're getting somewhere!


1) Open SAP API Management ~ Hamburger Menu ~ Manage


2) Open the API Proxy you made for Chuck Norris jokes. Click "Launch Policy Designer"


3) Click "Edit" in the bottom-right hand corner. Then click "PreFlow" under ProxyEndpoint. Then on the right side scroll down through the policies until you find "Quota" and click the "+" next to it. Add a policy called "Quota" to Incoming Request stream. Then click "Update". The defaults for this policy will allow 2 requests per 1 minute.


4) Click "Save" then click "Launch Debug Viewer" and click "Start Debug"


5) Return to your Chuck Norris App and click "Get New Joke" 3 times. You should see the joke update the first 2 times, then nothing should happen. Wait 1 minute, and repeat.


6) Go back to the API Management API Debug Viewer. Click Refresh. You should see 2 "Green" requests followed by a "Red" request twice. SAP API Management allowed the first 2 requests then blocked the next one until the timer reset.

(OPTIONAL) Extended


It's not that satisfying to just see nothing happen when you hit the quota limit is it? So let's also have SAP API Management return a custom error when Quota is exceeded in the same style as the normal jokes.


1) Open SAP API Management ~ Hamburger Menu ~ Manage


2) Open the API Proxy you made for Chuck Norris jokes. Click "Launch Policy Designer"

3) Click "Edit" in the bottom-right hand corner. Then click "PreFlow" under ProxyEndpoint. Then on the right side scroll down through the policies until you find "Raise Fault" and click the "+" next to it. Add a policy called "raisefault" to Incoming Request stream.

4) In the "Conditional String" area add the following


(ratelimit.Quota.failed = "true")

5) In the code window replace with the following code snippet:


<!-- can be used to create custom messages in case of an error condition -->
<RaiseFault async="true" continueOnError="false" enabled="true" xmlns="http://www.sap.com/apimgmt">
  <!-- Defines the response message returned to the requesting client -->
  <FaultResponse>
  <Set>
  <!-- Sets or overwrites HTTP headers in the response message -->
  <Headers/>
  <Payload contentType="application/json">\{ "type": "success", "value": \{ "id": 999, "joke": "Chuck Norris gets annoyed when people ask about him too much.", "categories": ["nerdy"] } }</Payload> <StatusCode>200</StatusCode>
  <!-- sets the reason phrase of the response -->
  <ReasonPhrase>OK</ReasonPhrase>
  </Set>
  </FaultResponse>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>

Or replace the message with something else more entertaining.

6) Click "Update". Then click "Save".


7) Return to your Chuck Norris App and click "Get New Joke" 3 times. You should see the joke update the first 2 times, then you should get your custom joke message.This message should be the only value returned upon clicking "Get New Joke" until 1 minute has elapsed.


Now you can get started enhancing the App in further ways, like changing the name (if you think someone is more awesome than Chuck Norris) on the fly (using Assign message) or whatever catches your fancy, and have a less boring app to show off to people when talking about APIs :smile:

Thanks again to denise.nepraunig.sap for providing such a solid App basis to start from.


If you would like to start learning more about what you can do with SAP API Management, I suggest looking at the repository of information SAP API Management - Overview & Getting started which will continue to be updated as more enablement content is added.

For questions, feedback, concerns, feel free to leave a comment, or send us an E-Mail.


Also follow us online
SAP API Management SAP.com | SCN | YouTube

5 Comments