Skip to main content

Openwhisk Web actions with path parameters

What are web actions?

Web actions can be explained as OpenWhisk actions annotated to quickly enable you to build web based applications as defined in the official documentation. If we compare an Openwhisk action and a web action, an OpenWhisk action that is not a web action requires both authentication and must respond with a JSON object. In contrast a web action can be invoked even without authentication and is capable of also passing additional data such as HTTP header, Status codes and returning body content of different types.

What can we retrieve inside a web action?

When invoking web actions they are capable of receiving additional HTTP request details as they are designed to facilitate features of HTTP invocations.
  • __ow_method : The HTTP method of the request.
  • __ow_headers : The request headers.
  • __ow_user: The namespace identifying the OpenWhisk authenticated subject who created this web action.
  • __ow_path:The attributes which are passed as path parameters into the action.
  • __ow_query:The parameters which are passed into the action as query parameters.
  • __ow_body:The request body entity, as a base64 encoded string when content is binary or JSON object/array, or plain string otherwise.
We will be making use of such attributes in the web actions we are going to create in this tutorial in order to build up meaningful web actions.

Let’s get started with our web actions.

We are going to create some web actions which carry out some basic CRUD operations on a database. For this example we will be making use of a library database which store information about books which we are going to create in the inbuilt couch database installation.
Make sure you have an installation of apache openwhisk to proceed with this tutorial. If you do not have one you can simply create it by following this guide.
1. Creating the database: We would be creating a database in our couch database which comes with the openwhisk setup. Execute the below command in your setup to create a database named library-database.
curl -X PUT "http://172.17.0.1:5984/library-database?n=3&q=8" --user <Couch db username>
  • * You can find your couch db username and password in the file openwhisk_home/ansible/db_local.ini file
2. Creating the web actions:
  1. To create the web action you need to checkout the code from github. Let’s name this checkout as web-actions-samples
  2. Navigate to the location web-actions-samples and open the file named as installLibraryActions.sh
  3. Provide your couch database URL and the database name. In our case the database name would be “library-database” and by default in a local installation the couch database URL would be http://172.17.0.1:5984.
  4. Next install the web actions by running the script. This will install three web actions inside a package called library

./installLibraryActions.sh
ok: updated package library
updating: getBooksAction.js (deflated 78%)
updating: dbUtils.js (deflated 75%)
updating: package.json (stored 0%)
ok: updated action library/getBooksAction
updating: putBooksAction.js (deflated 72%)
updating: dbUtils.js (deflated 75%)
updating: package.json (stored 0%)
ok: updated action library/putBooksAction
updating: deleteBooksAction.js (deflated 74%)
updating: dbUtils.js (deflated 75%)
updating: package.json (stored 0%)
ok: updated action library/deleteBooksAction
3. Verifying the web actions : Now let’s make sure that the web actions have been successfully created in the system. For that let’s list the web actions which are under the library package by running the command below.
wsk package get --summary library -i
package /guest/library: This package manages the library API backend actions
   (parameters: *dbName, *dbUrl)
 action /guest/library/deleteBooksAction
   (parameters: none defined)
 action /guest/library/putBooksAction
   (parameters: none defined)
 action /guest/library/getBooksAction
   (parameters: none defined)
4. Invoking the web actions: Before we invoke the web action we need to get the web url which corresponds to the action we just created. For that let’s run the command below which would return us with the web url.
wsk action get library/putBooksAction --url -i
ok: got action putBooksAction
https://172.17.0.1/api/v1/web/guest/library/putBooksAction
Now that we have the URL we can simply invoke this web actions using the PUT HTTP method.
curl -X PUT -k https://172.17.0.1/api/v1/web/guest/library/putBooksAction/books/harry-potter
{"message":"The book harry-potter was successfully added"}
You can verify that this was added into your database by simply retrieving the details of the book. Make sure you get the url of the getBooksAction in the same way you retrieved the putBooksAction mentioned above to get the details of the book we just created.
curl -X GET -k https://172.17.0.1/api/v1/web/guest/library/getBooksAction/books/harry-potter
{"message":"Successfully retrieved the document","doc":"{\"_id\":\"harry-potter\",\"_rev\":\"9-9bbdeb76e82ae8296afeb3fe2e7b3ef2\",\"description\":\"This is a sample book\",\"name\":\"harry-potter\"}\n"}
As you can see in the above example we invoked this action with a path parameter in the format /books/{book-name}. How did our action correctly handle this request? It’s pretty simply. If we open up the code of the action file “putBooksAction.js” which was used to create the putBooksAction we can see a logic which handles the matching of path parameters and HTTP methods using the HTTP attributes embedded into a web action as described above. The web action receives these information at the time of the invocation.
If we log the args object passed into our web action we can see the following information. You can see things like the method and path etc.
{
   "dbUrl":"http://172.17.0.1:5984",
   "dbName":"library-database",
   "__ow_method":"get",
   "__ow_headers":{
      "host":"controllers",
      "user-agent":"curl/7.47.0",
      "accept":"*/*"
   },
   "__ow_path":"/books/harry-potter"
}
In our action code logic we have used web action attributes of the object passed into the web action during the invocation. args.__ow_path, args.__ow_method to route to the correct database operations. With this we then facilitate our web actions to be used with path parameters with different HTTP methods. Likewise we can extend this to use query parameters or act upon various requests payloads as well. The flexibility lies in our hands when creating our web actions to meet our service requirements.
Read more about web actions and their capabilities in the official documentation on web actions. Hope this post has helped you to get an understanding on how to write web actions which consists of path parameters and various HTTP methods.

References.

Comments

  1. wow really superb you had posted one nice piece of information through this.
    Digital Profile Mapping

    ReplyDelete

Post a Comment

Popular posts from this blog

WSO2 Cloud

Introduction WSO2 Cloud consists of three commercial cloud offerings namely the API cloud, Integration Cloud and the Identity Cloud. WSO2 Integration Cloud WSO2 Integration Cloud is a public hosted version of the powerful, 100% open-source WSO2 Enterprise Integrator. It allows you to host your cloud-to-cloud, cloud-to-on-premise integrations and API backends on a scalable cloud platform WSO2 API Cloud WSO2 API Cloud is an enterprise-ready, self-service platform that enables you to expose business capabilities as managed APIs and leverage off-the-shelf potential for advertising and selling business APIs. The API Cloud supports all aspects of an API’s lifecycle, from its creation to publication and retirement. The API Cloud is based on WSO2 API Manager , WSO2’s complete solution for designing and publishing APIs and managing a developer community. [2] WSO2 Identity Cloud WSO2 Identity Cloud leverages the functionality of WSO2 Identity Server’s identity and ac...

Exposing a SOAP service as a REST API

In this post i will be explaining how we can transform a SOAP based backend to receive requests in a restful manner through the WSO2 API Cloud. Steps. First log into the WSO2 Cloud and navigate to the API Cloud. In the API cloud select the option to add a new API. We will be creating an API to demonstrate an invocation to the backend soap service ws.cdyne.com/phoneverify/phoneverify.asmx?wsdl Give a name, context and version to the API and add a resource name with a GET Method. The resource name can be anything which you like since we will invoke the actual service usiing a custom sequence. Mention the URI template as indicated in the below screenshot. Next go to the implement tab. And select the endpoint type as HTTP/SOAP Endpoint and specify the endpoint as http://ws.cdyne.com/phoneverify/phoneverify.asmx. There is an important step we need to do here. We need to set the SOAP version for this request. In order to do that we need to select the advanced option ...

Adding error sequence to a custom sequence

In my previous posi i explained how to call an endpoint using the call mediator. Usually when errors are thrown from the call mediator we can handle them using a custom sequence. When call mediators fails the execution stops hence handling errors is very essential. For this post I will be modifying the same sequence which i created in the blog post [1] The change which we need to do is we need to modify the sequence definition to map with the error sequence as shown below. You can find the modified sequence here . <sequence xmlns="http://ws.apache.org/ns/synapse" name="countrySequenceWithError" onError="countryErrorSequence"> <property name="uri.var.countryCode" expression="$url:countryCode"/> <property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>     <call blocking="true">         <endpoint>             <http method="get" uri-temp...