Class: WebAPIDevice

WebAPIDevice()

The WebAPIDevice class exposes several methods that provide request-like functionality. In addition, all api calls are queued, and if desired throttled and rate limited.

Constructor

new WebAPIDevice()

Example
const WebAPIDevice = require('homey-wifidriver').WebAPIDevice;

class myDevice extends WebAPIDevice {
     async onInit(){
         await super.onInit({
             apiBaseUrl: 'http://www.example-api.com/',
             throttle: 500, // 500ms between api calls
             rateLimit: {
                 max: 5,
                 per: 60000, // maximum of 5 requests per minute, all exceeding calls will be queued
             }
         }).catch(err => this.error(err));

         this.setDefaultHeaders({
             Authorization: 'Bearer 123456789'
         });

         this.apiCallGet({command: 'testRequest'})
             .then(res => {
                 console.log(res);
             })
     }
}

Extends

Methods

deregisterPollInterval(id)

Method that clears the poll interval and removes the registered interval from the list.

Parameters:
Name Type Description
id string

poll interval identifier

Inherited From:

onDeleted()

Method that will be called when device is deleted, it will abort all outstanding api calls.

Overrides:

(async) onInit(options) → {Promise}

This method needs to be called from a device.onInit(), it will create a PromiseQueue that will be used for queueing, throttling and rate limiting api calls.

Parameters:
Name Type Description
options Object
Properties
Name Type Attributes Description
apiBaseUrl string <optional>

Url used for making api calls

throttle number <optional>

Throttle value, time in ms between api calls

rateLimit number <optional>
Properties
Name Type Attributes Description
max number <optional>

Maximum number of api calls per time unit

per number <optional>

Time unit in ms

Returns:
Type
Promise

registerBackOffStrategy(options) → {BackOffStrategy|Error}

Method that creates and returns a back off strategy (Fibonacci).

Parameters:
Name Type Description
options Object

Specification for initialization backoff strategy

Properties
Name Type Attributes Default Description
onBackOffReady function

Function will be called on each back off tick

onBackOffFailed function <optional>

Function will be called when back off failed (maxTries exceeded)

randomisationFactor number <optional>
0

Randomize the interval (0 - 1)

initialDelay number <optional>
10000

After first failed init wait this long (ms)

maxDelay number <optional>
300000

Max length of time between init retries (ms)

maxTries number <optional>
infinite

Max number of initialization retries

startImmediately boolean <optional>
false

Backoff called on creation

Inherited From:
Returns:

backOffStrategy - Back off strategy instance

Type
BackOffStrategy | Error

registerPollInterval(options)

Method that adds a poll interval to the list, and starts polling on the provided interval.

Parameters:
Name Type Description
options Object
Properties
Name Type Description
id string

Custom id of poll interval

fn function

Method that will be called when polling

interval number

Time in ms between polling intervals

Inherited From: