Search This Blog

Friday 8 May 2015

Accessing your Cloud Integration API end point from Javascript

I previously created a Cloud Integration endpoint using a Bluemix Application Itself. The application was a Sprint Boot application exposing a single REST endpoint. The screen shots below show what has been added to the Catalog as private API's in my organization. The demo below shows how to access the API using a Javascript client in this case NodeJS from the command line.





Here is how we can access that API using a Javascript SDK which we can download from the Cloud Integration service itself.

1. Click on the "Cloud Integration" service itself
2. Select your API endpoint
3. Under "Access SDK's" select "Javascript SDK" and unzip it onto your file system

Note: We will use NodeJS to run this code

4. Install the required packages using the following command

> npm install

5. Ensure you import the API module as follows , as we are not using NPM for the API itself. The code is commented out so simply add it back in and change the reference to use "sdk"

// Alternatively, if you are not using npm, then import the API class module.
var sdk = require('./lib/SpringBootHelloWorldAPI.js');
console.log("Imported API class module ok");


6. The full code is as follows which enables us to display the function call output within the method itself. There are many ways to do this but just so we invoke it this is good enough.

  
// Import the SDK package.
//var sdk = require('SpringBootHelloWorldAPI');

// Alternatively, if you are not using npm, then import the API class module.
var sdk = require('./lib/SpringBootHelloWorldAPI.js');
console.log("Imported API class module ok");

// Create a new instance of the API class.
var api = new sdk.SpringBootHelloWorldAPI();

// Set the API credentials.
// TODO: replace username and password with those from the API definition.
//api.setAPICredentials('username', 'password'); // The API credentials are optional.

// Example for the HelloWorldService operation.
function example_HelloWorldService() {

 // Set up the request parameters for the HelloWorldService operation.
 var request = {};

        console.log("About to call service method");

 // Invoke the HelloWorldService operation.
 api.HelloWorldService(request, function (error, callback) {

  // Handle any errors from the HelloWorldService operation.
  if (error) {
   console.log(error);
   throw error;
  }

                func_result = callback;
                console.log(func_result);

 });

}

example_HelloWorldService();

7. Edit ./lib/SpringBootHelloWorldAPI.js and change the final line of JavaScript code to be as follows

module.exports.SpringBootHelloWorldAPI = SpringBootHelloWorldAPI;

8. Finally call the API using the Javascript client as follows
 
pas@Pass-MacBook-Pro:~/bluemix-apps/cloud-integration/client-api/springboot-hello/javascript$ node example.js
Imported API class module ok
About to call service method
{ id: 48, content: 'Hello, World!' }


No comments: