Handlers

Handlers can be used if it is necessary to further process and enrich the data received from connected devices. In the process of processing they are a key link.

Using the API, interaction with handlers can be implemented, involving the implementation of operations such as:

  1. Getting a list of handlers.
  2. Getting information about one handler.
  3. Creating a new handler.
  4. Changing a handler.
  5. Deleting a handler.
  6. Launching handler testing.

Getting a list of handlers

In order to get a complete list of handlers created by the user and available to him, you need to send a request GET /api/v1/handlers. In response, an array of objects will be received, each of which represents a configuration of a separate handler.

Request

GET /api/v1/handlers HTTP/1.1
Content-Type: application/json
Authorization: Bearer {token}

Response

HTTP/1.1 200 OK

[
    {
        "_id": "5d8ccd2cc82fdf001127da07",
        "name": "Handler 01",
        "description": "Description of handler",
        "userCode": "/**n * Makes the sum of two numbersn * @param {number} a firstn * @param {number} b secondn */nfunction process(a, b) {n  const sum = a + b;n  return { sum };n}nn/**n * @author `user_name`n */nmodule.exports = process;",
        "tagname": "hndl_S1lfE6H9Pr",
        "code": "function process(a, b) {n    var sum = a + b;n    return { sum: sum };n}nmodule.exports = process;n",
        "bindings": {
            "importOrder": [
                "a",
                "b"
            ],
            "import": {
                "a": {
                    "type": "number"
                },
                "b": {
                    "type": "number"
                }
            },
            "exportType": "object",
            "export": {
                "sum": {
                    "type": "number"
                }
            }
        },
        "libs": []
    }
]

Getting information about one handler

In order to get the configuration of a specific processor, it is enough to indicate its identification number in the start line, in the address of the resource. The response will receive the required configuration of the handler and the result of the request.

Request

GET /api/v1/handlers/:id HTTP/1.1
Content-Type: application/json
Authorization: Bearer {token}

Response

HTTP/1.1 200 OK

{
    "_id": "5d8ccd2cc82fdf001127da07",
    "name": "Handler 01",
    "description": "Description of handler",
    "userCode": "/**n * Makes the sum of two numbersn * @param {number} a firstn * @param {number} b secondn */nfunction process(a, b) {n  const sum = a + b;n  return { sum };n}nn/**n * @author `user_name`n */nmodule.exports = process;",
    "tagname": "hndl_S1lfE6H9Pr",
    "code": "function process(a, b) {n    var sum = a + b;n    return { sum: sum };n}nmodule.exports = process;n",
    "bindings": {
        "importOrder": [
            "a",
            "b"
        ],
        "import": {
            "a": {
                "type": "number"
            },
            "b": {
                "type": "number"
            }
        },
        "exportType": "object",
        "export": {
            "sum": {
                "type": "number"
            }
        }
    },
    "libs": [],
    "success": true
}

Creating a new handler

To create a new handler, you must specify the required fields and their values in the request body. At the same time, the structure of the handler itself can be formed by analogy with the json format, which is displayed in the “Code” tab, in the “Handlers” menu. In response from the server, the generated configuration of the new handler and the result of the request will be received.

Request

POST /api/v1/handlers HTTP/1.1
Content-Type: application/json
Authorization: Bearer {token}

BODY

Response

HTTP/1.1 200 OK

BODY

Changing a handler

Using the platform API, some fields of the handlers can be changed and edited, for example, such as the name, description, etc. More complex and significant actions, such as refactoring the code, adding or removing new variables, changing binders, i.e. the links between the parameters in the handler and the arguments in the model are best done in the graphical interface of the platform.

Request

PATCH /api/v1/handlers HTTP/1.1
Content-Type: application/json
Authorization: Bearer {token}

BODY

Response

HTTP/1.1 200 OK

BODY

Deleting a handler

To remove a handler, you need to send a DELETE /api/v1/handlers request. The response will receive its configuration and the result of the request.

Request

DELETE /api/v1/handlers HTTP/1.1
Content-Type: application/json
Authorization: Bearer {token}

Response

HTTP/1.1 200 OK

{
    "_id": "5d8cd5cec82fdf001127ebd8",
    "name": "Handler 03",
    "description": "sdgvdfb",
    "userCode": "/**n * Makes the sum of two numbersn * @param {number} a firstn * @param {number} b secondn */nfunction process(a, b) {n  const sum = a + b;n  return { sum };n}nn/**n * @author `elisalena99@gmail.com`n */nmodule.exports = process;",
    "_options": null,
    "owner": "5d8a18d5d0025e0012fb6a34",
    "group": "5d8a18d4d0025e0012fb6a31",
    "tagname": "hndl_HylS0SL5Dr",
    "code": "function process(a, b) {n    var sum = a + b;n    return { sum: sum };n}nmodule.exports = process;n",
    "bindings": {
        "importOrder": [
            "a",
            "b"
        ],
        "import": {
            "a": {
                "type": "number"
            },
            "b": {
                "type": "number"
            }
        },
        "exportType": "object",
        "export": {
            "sum": {
                "type": "number"
            }
        }
    },
    "libs": [],
    "time": 1569510862492,
    "success": true
}

Launching handler testing

The platform’s functionality provides such a function as testing the code prescribed in the handler. It can be implemented not only in the graphical interface, but also using the API programming interface. To do this, send the request POST /api/v1/handlers/test_code.

Request

POST /api/v1/handlers/test_code HTTP/1.1
Content-Type: application/json
Authorization: Bearer {token}

Response

HTTP/1.1 200 OK

BODY