How to use Public Endpoint in a Custom Page

This document explains how to use the public endpoint in User Portal.  The public endpoint is for public pages to access data from the IoT Platform.  The following is an example JS code for a custom page.

 

Step 1: Set PUBLIC_ENDPOINT_POLICY in Advanced Config page

Step 2: Use public endpoint in Config Dashboard page

[* Now only supporting /device_data, /last_n_values, /event_create, /event_update endpoints]

 

Endpoint Name: public_get

Description: GET method for public endpoint.

URL:  /public_get

Parameters:   GET  

Use as original endpoint except add an extra field “endpoint” with the corresponding endpoint name

Example:

params: {

“endpoint”: “device_data”,

device_id: “test_device”,

limit: 10,

      sort_by: 'observed_at',

sort_direction: ‘asc’,

stream: "sensor_data"

Response: 

Same as the original endpoint response result

Upon success: Status code 200

Example: Same as the original endpoint response result

Upon failure:    HTTP Error code for the type of error that occurred


Example:

 

$http.get("/public_get", {

params: {

“endpoint”: “device_data”,

device_id: “test_device”,

limit: 10,

      sort_by: 'observed_at',

sort_direction: ‘asc’,

stream: "sensor_data"

}).then(
    function(response) {
// this function will be called if the GET was successful
console.log("successfully got events");
    },
    function(error) {
// this function will be called if an error occurred
// with this request
console.log("error occurred while getting events: ", error);
    }
);

 

Endpoint Name: public_post

Description: POST method for public endpoint.

URL:  /public_post

Parameters:   POST    

Use as original endpoint except add an extra field “endpoint” with the corresponding endpoint name

body with the parameter

 

Example:

{

endpoint: “event_create”,

device_id: “test_device”,

      event_data:{

loc_name:”LA

},

stream: "sensor_data"

Response: 

Same as the original endpoint response result

Upon success: Status code 200

Example: Same as the original endpoint response result

Upon failure:    HTTP Error code for the type of error that occurred

Example:

 

$http.post("/public_post", {

endpoint: “event_create”,

device_id: “test_device”,

      event_data:{

loc_name:”LA

},

stream: "sensor_data


}).then(
    function(response) {
// this function will be called if the POST was successful
console.log("successfully created event");
    },
    function(error) {
// this function will be called if an error occurred
// with this request
console.log("error occurred while creating event: ", error);
    }
);