Logic Machine Forum
Rewrite Curl to LUA? - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Rewrite Curl to LUA? (/showthread.php?tid=993)



Rewrite Curl to LUA? - zoltan - 14.09.2017

Hello,

Can anyone tell me if this sample script can be "transplanted" to LM?

Code:
<?php
$username = 'xxx@xxx.xxx';
$password = 'xxx;
$serial = 'xxx';
$nonce;
$jsessionid;
$token;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://my.zipato.com/zipato-web/v2/user/init";,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
$json = json_decode($response, true);
$nonce = $json['nonce'];
$jsessionid = $json['jsessionid'];
$password = sha1($password);
$token = $nonce . $password;
$token = sha1($token);
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://my.zipato.com/zipato-web/v2/user/login?token=$token&username=$username";,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cookie: JSESSIONID = $jsessionid;"    
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://my.zipato.com/zipato-web/v2/box/reboot/$serial";,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cookie: JSESSIONID = $jsessionid;"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
echo 'success';
}
?>

If yes, can you give some guidance if not the full code?
I'm using ZipaTile as room thermostat (and lights, Sonos, etc. UI ), but sometimes it fails to send temperature data. The idea is to check periodically the group update time from resident script, and if it is to long issue a reboot command.

Thanks, Z


RE: Rewrite Curl to LUA? - admin - 15.09.2017

Try this:
Code:
username = 'xxx@xxx.xxx'
password = 'xxx'
serial = 'xxx'

https = require('ssl.https')
ltn12 = require('ltn12')
sha1 = require('encdec').sha1
json = require('json')

function dorequest(url, headers)
  local tbl = {}
  local res, err = https.request({
    url = url,
    headers = headers,
    sink = ltn12.sink.table(tbl)
  })

  if res then
    return table.concat(tbl)
  else
    return nil, err
  end
end

res, err = dorequest('https://my.zipato.com/zipato-web/v2/user/init')
log('init', res, err)

if res then
  data = json.decode(res)
  password = sha1(password)
  token = sha1(data.nonce .. password)

  headers = {
    ['accept'] = 'application/json',
    ['cookie'] = 'JSESSIONID=' .. data.jsessionid .. ';',
  }

  url = 'https://my.zipato.com/zipato-web/v2/user/login?token=' .. token .. '&username=' .. username
  res, err = dorequest(url, headers)
  log('login', res, err)

  if res then
    url = 'https://my.zipato.com/zipato-web/v2/box/reboot/' .. serial
    res, err = dorequest(url, headers)
    log('reboot', res, err)
  end
end



RE: Rewrite Curl to LUA? - zoltan - 15.09.2017

Thanks Admin,

It works as advertised Smile


RE: Rewrite Curl to LUA? - DGrandes - 21.08.2018

Hi,

I´ve tried to traslate this code to LM with your example but something is wrong.

Can anyone help me?

Code:

<?php
// set variables
$queryUrl = "http://api.kairos.com/detect";
$imageObject = '{"image":"YOUR_IMAGE_URL"}';
$APP_ID = "YOUR_APP_ID";
$APP_KEY = "YOUR_APP_KEY";
$request = curl_init($queryUrl);
// set curl options
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request,CURLOPT_POSTFIELDS, $imageObject);
curl_setopt($request, CURLOPT_HTTPHEADER, array(
"Content-type: application/json",
"app_id:" . $APP_ID,
"app_key:" . $APP_KEY
)
);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($request);
// show the API response
echo $response;
// close the session
curl_close($request);

Thanks


RE: Rewrite Curl to LUA? - admin - 21.08.2018

Code:
http = require('socket.http')
ltn12 = require('ltn12')
json = require('json')

function dorequest(url, headers, data)
  local resp = {}

  headers['Content-Length'] = #data

  local res, err = http.request({
    url = url,
    method = 'POST',
    headers = headers,
    sink = ltn12.sink.table(resp),
    source = ltn12.source.string(data)
  })

  if res then
    return table.concat(resp)
  else
    return nil, err
  end
end

url = 'http://api.kairos.com/detect'
imageurl = 'http://...'
app_id = 'YOUR_APP_ID'
app_key = 'YOUR_APP_KEY'

headers = {
  ['Content-Type'] = 'application/json',
  ['app_id'] = app_id,
  ['app_key'] = app_key,
}

data = json.encode({
  ['image'] = imageurl
})

res, err = dorequest(url, headers, data)
log(res, err)



RE: Rewrite Curl to LUA? - DGrandes - 22.08.2018

Thanks!!

It works fine!