Watson Service Chaining via OpenWhisk Sequence : Part 3 of 3

By now (after reading Part 1 and Part 2 in this series), you should be aware of what OpenWhisk is and leverage OpenWhisk Sequence to chain Watson services. Also, you should have created Swift and NodeJS 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 setup OpenWhisk CLI on your machine.

If not follow the instructions below,

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

  • On your terminal, run the below command

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 a 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.

For Example, you can run this command on terminal and see the output in French

Note: To pass a parameter use –param and –result to see only the response.

  • Let’s expose Watson sequence that is already created with OpenWhisk API Gateway

Note: /api-demo/v1 is your base path, /watsonsequence is the API path, get is the verb and then the action or in this case sequence with namespace returned as part of action list

  • You should see a 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.

 NodeJS Query Console app using JavaScript client library for the OpenWhisk platform

In this section, you will create a nodejs app which will be deployed to Bluemix via SDK for NodeJS and uses OpenWhisk Javascript client library (a wrapper around OpenWhisk APIs)

  • Connect and Log into Bluemix

Note: You can find username, organization name and space name on Bluemix console page (Top right corner).

  • Clone the code repo into a folder on your machine

  • cd openwhisk-nodejs-webui
  • Run the below command

  • 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, host.
  • Create a new file (File->New File) and save it as .env. Add the below values to .env file and save

Note: You can get the auth key by running the below command on terminal

  • To run the app locally,

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 nodeJS runtime is created on IBM Bluemix (IBM’s Cloud platform) by pulling nodeJS buildpack.
  • Wait until you this message on the terminal

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

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

There is more to explore in OpenWhisk

  • Automate the actions via Triggers.
  • Explore Rules.
  • Create a package and Create to Feed.
Polyglot & Pragmatic Programmer • Developer Advocate, IBM • Microsoft MVP • Intel software Innovator • DZone MVB
(Visited 30 times, 1 visits today)

Tagged as: ,

Author: Vidyasagar Machupalli

Polyglot & Pragmatic Programmer • Developer Advocate, IBM • Microsoft MVP • Intel software Innovator • DZone MVB