OpenWhisk Swagger 2.0 Template & Cloudant package sdk



# Openwhisk Swagger Template

Swagger 2.0 = OpenAPI 2.0 

==

swagger: '2.0'
info:
  version: '1.0'
  title: openwhiskapihira
schemes:
  - https
consumes:
  - application/json
produces:
  - application/json
paths:
  /openwhiskapihira:
    get:
      operationId: getOpenwhiskapihira
      x-openwhisk:
        namespace: ****
        action: ''
        package: default
        url: 'https://openwhisk.ng.bluemix.net/api/v1/web/****/default/.json'
      responses:
        '200':
          description: A successful invocation response
x-ibm-configuration:
  assembly:
    execute:
      - operation-switch:
          case:
            - operations:
                - getOpenwhiskapihira
              execute:
                - invoke:
                    target-url: 'https://openwhisk.ng.bluemix.net/api/v1/web/****/default/.json'
                    verb: keep
          otherwise: []
          title: whisk-invoke
  cors:
    enabled: true
basePath: /gws/apigateway/api/***/openwhiskapihra
securityDefinitions:
  client_id:
    type: apiKey
    in: header
    name: X-IBM-Client-ID
    x-key-type: clientId
  github:
    type: oauth2
    flow: application
    tokenUrl: ''
    x-tokenintrospect:
      url: 'https://api.github.com/user'
security:
  - client_id: []
    github: []
    $$label: 'client_id, github'
x-ibm-rate-limit:
  - unit: second
    units: 1
    rate: 1000
host: service.us.apiconnect.ibmcloud.com

## Cloudant create attachment

* Create and update attachment for document in Cloudant database:
 * https://docs.cloudant.com/attachments.html#create-/-update
 **/

function main(message) {
  var cloudantOrError = getCloudantAccount(message);
  if (typeof cloudantOrError !== 'object') {
    return Promise.reject(cloudantOrError);
  }
  var cloudant = cloudantOrError;
  var dbName = message.dbname;
  var docId = message.docid;
  var attName = message.attachmentname;
  var att = message.attachment;
  var contentType = message.contenttype;
  var params = {};

  if(!dbName) {
    return Promise.reject('dbname is required.');
  }
  if(!docId) {
    return Promise.reject('docid is required.');
  }
  if(!attName) {
    return Promise.reject('attachmentname is required.');
  }
  if(!att) {
    return Promise.reject('attachment is required.');
  }
  if(!contentType) {
    return Promise.reject('contenttype is required.');
  }
  //Add document revision to query if it exists
  if(typeof message.docrev !== 'undefined') {
    params.rev = message.docrev;
  }
  var cloudantDb = cloudant.use(dbName);

  if (typeof message.params === 'object') {
    params = message.params;
  } else if (typeof message.params === 'string') {
    try {
      params = JSON.parse(message.params);
    } catch (e) {
      return Promise.reject('params field cannot be parsed. Ensure it is valid JSON.');
    }
  }

  return insert(cloudantDb, docId, attName, att, contentType, params);
}

/**
 * Insert attachment for document in database.
 */
function insert(cloudantDb, docId, attName, att, contentType, params) {
  return new Promise(function(resolve, reject) {
    cloudantDb.attachment.insert(docId, attName, att, contentType, params, function(error, response) {
      if (!error) {
        console.log("success", response);
        resolve(response);
      } else {
        console.log("error", error);
        reject(error);
      }
    });
  });
}

function getCloudantAccount(message) {
  // full cloudant URL - Cloudant NPM package has issues creating valid URLs
  // when the username contains dashes (common in Bluemix scenarios)
  var cloudantUrl;

  if (message.url) {
    // use bluemix binding
    cloudantUrl = message.url;
  } else {
    if (!message.host) {
      return 'cloudant account host is required.';
    }
    if (!message.username) {
      return 'cloudant account username is required.';
    }
    if (!message.password) {
      return 'cloudant account password is required.';
    }

    cloudantUrl = "https://" + message.username + ":" + message.password + "@" + message.host;
  }

  return require('cloudant')({
    url: cloudantUrl
  });
}


## Cloudant Manage bulk document

* Create, Update, and Delete documents in bulk:
 * https://docs.cloudant.com/document.html#bulk-operations
 **/

function main(message) {
  var cloudantOrError = getCloudantAccount(message);
  if (typeof cloudantOrError !== 'object') {
    return Promise.reject(cloudantOrError);
  }
  var cloudant = cloudantOrError;
  var dbName = message.dbname;
  var docs = message.docs;
  var params = {};

  if(!dbName) {
    return Promise.reject('dbname is required.');
  }
  if(!docs) {
    return Promise.reject('docs is required.');
  }
  var cloudantDb = cloudant.use(dbName);

  if (typeof message.params === 'object') {
    params = message.params;
  } else if (typeof message.params === 'string') {
    try {
      params = JSON.parse(message.params);
    } catch (e) {
      return Promise.reject('params field cannot be parsed. Ensure it is valid JSON.');
    }
  }

  if (typeof message.docs === 'object') {
    docs = message.docs;
  } else if (typeof message.docs === 'string') {
    try {
      docs = JSON.parse(message.docs);
    } catch (e) {
      return Promise.reject('docs field cannot be parsed. Ensure it is valid JSON.');
    }
  } else {
    return Promise.reject('docs field is ' + (typeof docs) + ' and should be an object or a JSON string.');
  }

  return bulk(cloudantDb, docs, params);
}

function bulk(cloudantDb, docs, params) {
  return new Promise(function(resolve, reject) {
    cloudantDb.bulk(docs, params, function(error, response) {
      if (!error) {
        var responseDocs = {};
        responseDocs.docs = response;
        console.log('success', response);
        resolve(responseDocs);
      } else {
        console.log('Error: ', error);
        reject(error);
      }
    });
  });
}

function getCloudantAccount(message) {
  // full cloudant URL - Cloudant NPM package has issues creating valid URLs
  // when the username contains dashes (common in Bluemix scenarios)
  var cloudantUrl;

  if (message.url) {
    // use bluemix binding
    cloudantUrl = message.url;
  } else {
    if (!message.host) {
      return 'cloudant account host is required.';
    }
    if (!message.username) {
      return 'cloudant account username is required.';
    }
    if (!message.password) {
      return 'cloudant account password is required.';
    }

    cloudantUrl = "https://" + message.username + ":" + message.password + "@" + message.host;
  }

  return require('cloudant')({
    url: cloudantUrl
  });
}

## Cloudant read changed feed

/**
 * Read changes feed from Cloudant database:
 * https://docs.cloudant.com/database.html#get-changes
 **/

function main(message) {
  var cloudantOrError = getCloudantAccount(message);
  if (typeof cloudantOrError !== 'object') {
    return Promise.reject(cloudantOrError);
  }
  var cloudant = cloudantOrError;
  var dbName = message.dbname;
  var params = {};

  if(!dbName) {
    return Promise.reject('dbname is required.');
  }

  if (typeof message.params === 'object') {
    params = message.params;
  } else if (typeof message.params === 'string') {
    try {
      params = JSON.parse(message.params);
    } catch (e) {
      return Promise.reject('params field cannot be parsed. Ensure it is valid JSON.');
    }
  }

  return changes(cloudant, dbName, params);
}

function changes(cloudant, dbName, params) {
  return new Promise(function(resolve, reject) {
    cloudant.db.changes(dbName, params, function(error, response) {
      if (!error) {
        console.log('success', response);
        resolve(response);
      } else {
        console.error('error', error);
        reject(error);
      }
    });
  });
}

function getCloudantAccount(message) {
  // full cloudant URL - Cloudant NPM package has issues creating valid URLs
  // when the username contains dashes (common in Bluemix scenarios)
  var cloudantUrl;

  if (message.url) {
    // use bluemix binding
    cloudantUrl = message.url;
  } else {
    if (!message.host) {
      return 'cloudant account host is required.';
    }
    if (!message.username) {
      return 'cloudant account username is required.';
    }
    if (!message.password) {
      return 'cloudant account password is required.';
    }

    cloudantUrl = "https://" + message.username + ":" + message.password + "@" + message.host;
  }

  return require('cloudant')({
    url: cloudantUrl
  });
}