esi-inmation-http-client

inmation Web API HTTP Client to perform HTTP requests using the esi-lcurl-http-client library.
This library will handle the token request automatically under the hood. Once the token is (about to getting) expired, a new one is requested with the credentials which were supplied with the constructor.

Features

Note: All functions names are also available in the ESI uppercase name convention.

Constructor

new(baseurl, options)

Create a new instance of the inmation HTTP Client with options.

local options = {
    authorization = {
        username = 'USERNAME',
        password = 'PASSWORD',
        authority = 'inmation'
    }
}

Supported authority values are inmation, ad (Active Directory) and machine

local Client = require('esi-inmation-http-client')
local baseURL = 'http://hostname:8002'
local options = {
    authorization = {
        username = 'USERNAME',
        password = 'PASSWORD',
        authority = 'inmation'
    }
}
local client = Client.new(baseURL, options)

CheckStatus

checkStatus()

local res = client:checkStatus()

Example of the response:

{
    "message": "Status is OK.",
    "dependencies":[
        {
            "status": "OK",
            "name": "ChannelAgent",
            "message": "Status is OK."
        }
    ],
    "status": "OK"
}

ExecFunction

execFunction(req)

local arg = {
    ctx = '/System/Core/APIContext',
    lib = 'my-library',
    func = 'progress'
}
local res = client:execFunction(arg)

Mass

mass(items)

See inmation.mass() on inmation Wiki for more information.

Read(items)

local items = {
    {
        p = "/System.ObjectName
    }
}
local res = client:read(items)

ReadHistoricalData

readHistoricalData(queryOrItems, start_time, end_time, intervals_no)

Read historical (processed) data via separate arguments or a single query.

local items = {
    {
        p = "/System/Core/Examples/DemoData/ProcessData/DC0815",
        aggregate = "AGG_TYPE_COUNT"
    }
}
local res = client:readHistoricalData(items, "2018-12-10T12:00:00.000Z", "2018-12-11T12:00:00.000Z", 1)

Using a query object:

local query = {
    start_time = "2018-12-10T12:00:00.000Z",
    end_time = "2018-12-11T12:00:00.000Z",
    intervals_no = 1,
    items = {
        {
            p = "/System/Core/Examples/DemoData/ProcessData/DC0815",
            aggregate = "AGG_TYPE_COUNT"
        }
    }
}
local res = client:readHistoricalData(query)

Example of the response:

{
    "start_time":"2018-12-10T12:00:00.000Z",
    "end_time":"2018-12-11T12:00:00.000Z",
    "items":[
        {
            "p":"/System/Core/Examples/DemoData/ProcessData/DC0815",
            "aggregate": "AGG_TYPE_COUNT",
            "intervals":[
                {
                    "V": 2880,
                    "Q": 9674424320,
                    "T": 1544443200000
                }
            ]
        }
    ]
}

ReadRawHistoricalData

readRawHistoricalData(query)

Read raw historical data based on a query. See inmation-api-docs for all the features of this endpoint.

local query = {
    start_time = "2018-12-10T12:00:00.000Z",
    end_time = "2018-12-10T12:00:15.000Z",
    items = {
        {
            p = "/System/Core/Examples/DemoData/ProcessData/DC0815",
        }
    }
}
return client:readRawHistoricalData(query)

Token

token(auth)

To request a new token. When the argument auth is nil, the authorization information supplied with the options in the constructor is used.
This function is mainly for testing a token. No need to call this function to maintain a valid token.

Write

write(itemValues)

Write values to object properties.

local itemValues = {
    {
        p = "/System/Core/Signal01",
        v = 100
    }
}
local res = client:write(itemValues)

Compression

This example shows how to deal with the HTTP response Content-Encoding for handling compressed data using LibDeflate pure Lua library.

local Client = require('esi-inmation-http-client')
local LibDeflate = require("LibDeflate")

local baseURL = 'http://hostname:8002'
local options = {
    authorization = {
        username = 'username',
        password = 'password'
    },
    acceptEncoding = 'deflate',
    onEncodedResponseData = function(encoded, encoding)
        if encoding == 'deflate' then
            local decompress_deflate = encoded
            local decompressed = nil
            repeat
                decompress_deflate = LibDeflate:DecompressDeflate(decompress_deflate)
                if decompress_deflate ~= nil then
                    decompressed = decompress_deflate
                end
            until(decompress_deflate == nil)
            return decompressed
        end
        return 'Unsupported encoding'
    end
}
local client = Client.new(baseURL, options)