Skip to content

Latest commit

 

History

History
164 lines (132 loc) · 5.13 KB

README.md

File metadata and controls

164 lines (132 loc) · 5.13 KB

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 eetay.natan@samanage.com

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]) {...}
)