Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titlejavacsript

github

Code Block
languagejs
// Example For Get Tag View API by using request in nodejs
// written by yeony kim
// sensor1, sensor2 is applied in lake
 process.env.["NODE_TLS_REJECT_UNAUTHORIZED"] = "0";

const express = require('express');
const app = express();
const API_KEY = "YOUR_API_TOKEN";
const LAKE_ID = "YOUR_LAKE_ID";
const URL CLOUD_VENDOR= "https://" + LAKE_ID + ""CLOUD_VENDOR";
const CLOUD_REGION="CLOUD_REGION";
const URL =  `https://${CLOUD_VENDOR}.${CLOUD_REGION}.machlake.com/v1/lakes/tag"${LAKE_ID}/tag`;

//var CASE - Get Tag Information

app.get(
    '/get_tag_view_no_tag_name', function(req, res) {
        var request request = require('request');


      const QueryParams = new URLSearchParams()

        QueryParams.set("name", "sensor")
        var option = {
  /* ------------------------------------------------------------------------------------------------- */

// CASE - Get Tag Information

request.get({
    url: URL, 
    qs: {
  url: URL + "?" + QueryParams.toString(),
  name: "sensor1"
     },
    headers: {
        "x-api-key": API_KEY
    }}, 
'Content-Type': 'application/json',   function(error, response, body) {
        console.log(body);
 'x-api-key': API_KEY      // Return Format
    },    // {
   };     //    request.get(option, function(error, response, body) { "success": true,
        //      if (!error && response.statusCode === 200) {"reason": "get tag meta success",
        //     "data": {"name": "sensor01"}
   res.writeHead(200);     // }
          res.end(body);})

               console.log(body)
            } else {
                console.log('error');
                console.log(error, response)
                if (response !== undefined) {
                    res.writeHead(200);
                    res.end(body);
                    console.log(body) // {"message":"no such name : sensor","status":"error"}
                }
            }
        })
    }
)

// CASE - Get Tag Information

app.get(
    '/get_tag_view_tag_name', function(req, res) {
        var request = require('request');
        const QueryParams = new URLSearchParams()

        QueryParams.set("name", "sensor1")
        var option = {
            url: URL + "?" + QueryParams.toString(),
            headers: {
                'Content-Type': 'application/json',
                'x-api-key': API_KEY
            },
        };
        request.get(option, function(error, response, body) {
            if (!error && response.statusCode === 200) {
                res.writeHead(200);
                res.end(body);
                console.log(body)
            } else {
                console.log('error');
                console.log(error, response)
        /* ------------------------------------------------------------------------------------------------- */

// CASE - Get Tag Information when no exist tag name

request.get({
    url: URL, 
    qs: {
       if (response !== undefined) {name: "wrong_name"
    },
    headers: {
        "x-api-key": API_KEY
 res.writeHead(200);   }}, 
    function(error, response, body) {
        resconsole.endlog(body);
        // Return   Format
       console.log(body) // {"data":{"name":"sensor1"},"status":"success"}
                } code : 400 Bad Request
        // {
   }     //    }) "success": false,
  } )   app.listen(8888, function() {//     console.log('http://127.0.0.1:8888/get_tag_view_no_tag_name is result for tag name not exist in lake')"reason": "no such name : wrong_name"
        console.log('http://127.0.0.1:8888/get_tag_view_tag_name is result for tag name exsit in lake')
}) }
    })
Expand
titlepython

github

Code Block
languagepy
"""
Example For Get Tag View API by using requests in python
written by yeony kim
sensor1, sensor2 is applied in lake
"""

import requests

API_KEY = "YOUR_API_TOKEN"
LAKE_ID = "YOUR_LAKE_ID"
URL = f"https://{LAKE_ID}.machlake.com/lakes/tag"

headers = {
    'Content-Type': 'application/json',
    'x-api-key': API_KEY
}

# CASE - Get Tag Information

params = {
    'name': 'sensor'
}

response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode('utf-8'))  # {"message":"no such name : sensor","status":"error"}

# CASE - Get Tag Information

params = {
    'name': 'sensor1'
}

response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode('utf-8'))  # {"data":{"name":"sensor1"},"status":"success"}

...