Git Product home page Git Product logo

samanage-api-js's Introduction

samanage-api

This is my personal API library for performing calls to The Samanage Service Platform. The code is provided as-is, without any warrenty. It is a work in progress and may not support all the options offered by the Samanage API Feel free to contact me with requests, issues or questions.

samanage-api code is reviewed with Codacy Badge and unit tested with Jest Jest

If you'd like to contribute, you are welcome to fork, and submit pull requests to the original library, or contact me at [email protected]

Try it on runKit (You will need a Samanage API token. How to get a token)

Installation

npm install samanage-api

Initialize

You will need a Samanage API token. How to get a token

var SamanageAPI = require('samanage-api')
var connection = new SamanageAPI.Connection("your-token-here")

Making a call

var success = function({data, ref, pagination_info}) {...}
var failure = function({error, info, httpStatus, ref}) {...}
var request = ... // see below different requests
connection.callSamanageAPI(request).then(success).catch(failure)

Setting value for 'ref'

The 'ref' attribute in the success and failure callbacks can be set during the call itself, like so:

var my_ref = 'whatever custom information. can also be of any type'
connection.callSamanageAPI(request, my_ref).then(success).catch(failure)

Retries

Retries are supported out of the box for certain HTTP codes which are retryable

you can configure the retry logic

var OPTS = { // see 'retry' npm module for full documentation
  retries: 3,
  factor: 2,
  minTimeout: 1 * 100,
  maxTimeout: 60 * 100,
  randomize: true,
}

// For all requests on a specific connection
connection.retry_opts = OPTS
connection.callSamanageAPI(request, ref).then(...).catch(...)

// For a particular request
request.retry_opts = OPTS 
connection.callSamanageAPI(request, ref).then(...).catch(...)

Retrieval with filters

var get_incidents = SamanageAPI.get('incident')
var request = get_incidents(
  new SamanageAPI.Filters().add({
    sort_order: 'ASC',
    sort_by: 'created_at',
    created: ['2018-01-01','2018-01-02']
  })
)
connection.callSamanageAPI(...)

Building filters

var get_incidents = SamanageAPI.get('incident')
var filters = new SamanageAPI.Filters().
  sort_by('name').
  sort_order(SamanageAPI.Filter.DESC).
  between_dates('created','2018-01-01','2018-01-02').
  per_page(100).
  page(3)
var request = get_incidents(filters)

Export with filters

var export_incidents = SamanageAPI.export('incident')
connection.callSamanageAPI(
  export_incidents(
    new SamanageAPI.Filters().between_dates('created','2017-01-01','2018-07-07')
  )
).then(function (result) {
  /* Success:
    result contains pagination info (how many incidents are there) but
    does NOT contain the incidents' data. It is sent via email
  */
}).catch(function(error) {
  // Failure
})

Update

var request = SamanageAPI.update('incident')(3, {
  name:'opened with samanage-api-js library'
})

Create

var request = SamanageAPI.create('incident')({
  name:'opened with samanage-api-js library'
})

List of all functions and constants

SamanageAPI has built in help. Open a new node console, and execute this:

var SamanageAPI = require('samanage-api')
console.log(SamanageAPI.help)
console.log(SamanageAPI.Filters.help)
console.log(SamanageAPI.Connection.help)

Getter objects

Getter objects are promises to get all items of certain type that match a specific SamanageAPI.Filter. Getters are very convenient way of retrieving things like Itsm States or Users, but may not be proper strategy for retrieving very large sets of items (e.g. all audit logs since start of time); Currently there's no check on number of items which will cause a very long retrieval process so just go ahead and experiment

Getters are defined like this:

// promise to get all ITSM states:
var itsm_states = connection.getter('itsm_state')

// promise to get all users created between certain dates
var users_filter = (new SamanageAPI.Filter()).between_dates('created','2017-01-01','2018-07-07')
var users = connection.getter('user', users_filter)

// promise to get all comments of particular incident
var comments = connection.getter('comment', (new SamanageAPI.Filters()), 'incidents/' + incident.id)

Example: Do something after both users and itsm states are retrieved

Promise.all([itsm_states, users]).then(
  function([states, users]) {...}
)

samanage-api-js's People

Contributors

eetay avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.