Over a million developers have joined DZone.

Watson Service Chaining With OpenWhisk (Part 3 of 3)

DZone 's Guide to

Watson Service Chaining With OpenWhisk (Part 3 of 3)

Now that you've begun chaining Watson services as an OpenWhisk sequence, it's time to expose them to the Internet as a RESTful endpoint.

· Cloud Zone ·
Free Resource

By now (after reading Part 1 and Part 2 in this series), you should be aware of what OpenWhisk is and know how to leverage OpenWhisk sequences to chain Watson services. Also, you should have created Swift and Node.js actions for transforming the JSON to required formats.

In this post, you will learn how to expose an action or a sequence (chain of actions) as a RESTful endpoint via OpenWhisk API Gateway and OpenWhisk CLI.

You should have set up OpenWhisk CLI on your machine.

If not follow the instructions below:

  • On a browser, move to Getting Started on Bluemix OpenWhisk and click on Download OpenWhisk CLI.
  • Following the steps on the CLI (left pane), set your OpenWhisk Namespace and Authorization Key.
  • Open Terminal (Ctrl + Alt + T) or Command Prompt on your system, then type wsk.

  • On your Terminal, run the command: wsk action list.

You should see all the actions you created via the browser.

Note: OpenWhisk actions, triggers, and rules belong in a namespace, and, optionally, a package.

  1. For convenience, the namespace can be left off if it is the user’s default namespace.
  2. The fully qualified name of an entity is /namespaceName[/packageName]/entityName. Notice that / is used to delimit namespaces, packages, and entities. Also, namespaces must be prefixed with a /.
  3. In Bluemix, an organization+space pair corresponds to an OpenWhisk namespace. For example, the organization BobsOrg and space dev would correspond to the OpenWhisk namespace /BobsOrg_dev.
  • After you create your action, you can run it in the cloud in OpenWhisk with the ‘invoke’ command. You can invoke actions with a blocking invocation (i.e., request/response style) or a non-blocking invocation by specifying a flag in the command. A blocking invocation request will wait for the activation result to be available. The wait period is the lesser of 60 seconds or the action’s configured time limit.
wsk action invoke --blocking {action-name}

  • Let’s expose a Watson sequence that has already been created with OpenWhisk API Gateway:
wsk api-experimental create /api-demo/v1 /watsonsequence get <namespace> /MyWatsonSequence

  • You should see an OK response and an API link, which is now ready to be used in any client application and can be tested via cURL or REST clients like Postman.
curl –g <url generated in the above step>?message=”hello”

Node.js Query Console App Using JavaScript Client Library for the OpenWhisk Platform

In this section, you will create a Node.js app, which will be deployed to Bluemix via SDK for Node.js. It uses an OpenWhisk JavaScript client library (a wrapper around OpenWhisk APIs).

  • Connect and log into Bluemix:
cf  api https://api.ng.bluemix.net
cf login -u {username} -o {org_name} -s {space_name}

  • Clone the code repo into a folder on your machine:
git clone https://github.com/VidyasagarMSC/openwhisk-nodejs-webui.git

  • cd openwhisk-nodejs-webui
  • Run the command:
npm install

  • Launch an IDE of your choice (I use Atom) and open the cloned repo folder (File->Open)
  • Open manifest.yml file and give a unique name and host.
  • Create a new file (File->New File) and save it as .env. Add the below values to the .env file and save.
OpenWhisk_HOST = "openwhisk.ng.bluemix.net"
OpenWhisk_AuthKey=" "

wsk property get --auth

Note: If you see "Permission denied", use sudo on your Linux or macOS machine.

  • Once you run the push command, based on the artifacts provided in manifest.yml file, a Node.js runtime is created on IBM Bluemix (IBM’s cloud platform) by pulling the Node.js buildpack.
  • Wait until you this message:
requested state: started
instances: 1/1
usage: 256M x 1 instances
urls: InterconnectOpenWhiskApp.mybluemix.net
last uploaded: Fri Feb 17 11:43:14 UTC 2017
stack: cflinuxfs2
buildpack: SDK for Node.js(TM) (ibm-node.js-4.7.2, buildpack-v3.10-20170119-1146)

state     since                    cpu    memory       disk         details
#0   running   2017-02-17 05:14:16 PM   0.0%   1M of 256M   1.3M of 1G

  • Test your app running on Bluemix with the URLs mentioned in the above step by replacing localhost:6007 with your Bluemix endpoint.

To summarize, in this lab, you have created actions (Node.js and Swift) and a Watson sequence (action chaining) connecting Watson the packages and actions you created. You exposed the sequence and actions as a REST endpoint. Also, you have created a Node.js webui app, which uses npm packages.

cloud ,ibm watson ,openwhisk ,rest application ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}