SambaPOS API Integration with NewBook PMS/Booking System

So looking good for v.48 :smile:

Would the <> be ignored?

Something like;

function JSONTest()
{
 var url = 'https://testau.newbookpms.com/rest/auth_test';
 var username = 'sambapos';
 var password = 'xxxxxxxxxxxxx';
 var api = '{\"api_key\":\"instances_xxxxxxxxxxxxxxxx\"}';
 var response = web.PostJson(url,api,[username],password);
 var responseObject = JSON.parse(response);
 return response;
}

I don’t understand the question.

<parameters> are mandatory
[parameters] are optional

Ok so none of the ‘<>’ or ‘[ ]’ characters are required, just showing required and optional values?

   function JSONTest()
    {
     var url = 'https://testau.newbookpms.com/rest/auth_test';
     var username = 'sambapos';
     var password = 'xxxxxxxxxxxxxxxx';
     var api = '{\"api_key\":\"instances_xxxxxxxxxxxxxx\"}';
     var response = web.PostJson(url,api,username,password);
     var responseObject = JSON.parse(response);
     return response;
    }

it would be like this:

function JSONTest()
{
 var url = 'https://testau.newbookpms.com/rest/auth_test';
 var api = '{\"api_key\":\"instances_xxxxxxxxxxxxxx\"}';
 var response = web.PostJson(url,api,sambapos,xxxxxxxxxxx);
 var responseObject = JSON.parse(response);
 return responseObject.json.success;
}

Correct. It is just a way to document syntax.

Fair enough, it wouldn’t work with var values for user/password then?

Yes, it would work with variables.

It would but there is no need since its static and doesnt change. Just less lines of code.

It’s the same thing end of day right?
In theory all the info in that one is static so does it make any difference if it was just the one line and url, api, user and password were all on the one line?
Other than it’s easier to read as would then be on multiple lines?

Not sure I follow what your asking.

Oh I understand… yes it would work but using variables makes the code cleaner and easier to follow.

It could also be due to the special characters sometimes used.

You can easily test that theory.

Yes this would work.

    var response = web.PostData('https://testau.newbookpms.com/rest/auth_test','{\"api_key\":\"instances_xxxxxxxxxxxx\"}',sambapos,xxxxxxxxxxxx)

But its very messy compared to using variables.

Ohh sorry, I think I created a confusion here… I meant it will be available on next version. I mentioned it in .48 documentation.

PS: responseObject.json syntax was what test server returns. First we need to check response to ensure how json is formatted.

1 Like

Was only a curiosity as to where you draw the line between using variable and actual value :smile:
Am picking it up slowly lol
Am really hyped for .48 now, fingers crossed :-), was stressing yesterday and in a foul mood today after the lack of progress yesterday…
Really appreciate all the help very much :slight_smile:

Ok so based on what Emre said it should look like this

function JSONTest()
{
 var url = 'https://testau.newbookpms.com/rest/auth_test';
 var api = '{\"api_key\":\"instances_xxxxxxxxxxxx\"}';
 var username = 'sambapos';
 var password = 'xxxxxxxxxxxxxxx';
 var response = web.PostJson(url,api,username,password);
 return response;
}

Hehe as you can see I am still learning. It took me several months to finally learn how to script my TimeTrex stuff. I had many many blocks where I just didnt make any progress but eventually it clicked.

1 Like

I am a proponent of using variables, and feeding variables to functions of parameters.

This probably will not work…

var response = web.PostJson(url,api,sambapos,xxxxxxxxxxxx);

because it believes every parameter in there is a variable, but the last 2 are not defined.

This would work…

var response = web.PostJson(url,api,'sambapos','xxxxxxxxxxxxxx');

because the last 2 parameters are now strings.

I would stick to defining your variables…

var url =  'http://someurl';
var api = 'someapiinformation';
var user = 'sambapos';
var password = 'xxxxxxxxxxxxx';
var response = web.PostJson(url,api,user,password);

I think it is easier to understand and much cleaner, even though it occupies more lines. Better than this, IMO…

var response = web.PostJson('http://thisisthepath.com/to/my/url/somewhereonthe/internet','myapikeyisabunchofletterslikexoe9djd|DDGgsrkgSSgeWsGsdGSDg','sambapos','xoe9djd|DDGgsrkgSSgeWsGsdGSDg');

Honestly looks like a bunch of jibberish, where you don’t know which parameter does what.

2 Likes

I agree variables make it neater,cleaner and easier to debug later if you want to make changes.

Answered my question perfectly ’ ’ would be needed for actual values (good to learn early on :smile:)
Variables would probably be my preferred way to go to keep a consistency thorough ought, define all variables and then code rather than mixing - plus looks cleaner as you say :slight_smile:

I go even further a lot of the time, concatenating small parts into a single variable. Easier to adjust and read if you are trying to build something complex…

function UpdateDescription(paymenttype) {
  var paymentinfo = "";
  
  var d = new Date();
  var dt = "";
  dt +=           d.getUTCFullYear();
  dt += "-" + pad(d.getUTCMonth(),'left',2,"0");
  dt += "-" + pad(d.getUTCDay(),'left',2,"0");
  dt += " " + pad(d.getHours(),'left',2,"0");
  dt += ":" + pad(d.getUTCMinutes(),'left',2,"0");
  dt += ":" + pad(d.getUTCSeconds(),'left',2,"0");
  dt += "." + pad(d.getUTCMilliseconds(),'left',3,"0");
  return dt;
}

To me, the above is better than the following, though it does the same thing:

function UpdateDescription(paymenttype) {
  var paymentinfo = "";
  
  var d = new Date();
  var dt = d.getUTCFullYear() + "-" + pad(d.getUTCMonth(),'left',2,"0") + "-" + pad(d.getUTCDay(),'left',2,"0") + " " + pad(d.getHours(),'left',2,"0") + ":" + pad(d.getUTCMinutes(),'left',2,"0") + ":" + pad(d.getUTCSeconds(),'left',2,"0") + "." + pad(d.getUTCMilliseconds(),'left',3,"0");
  return dt;
}

Now I will let you into my weird brain for a moment, because my first code-snippet has been reformatted to the following… yes, I am slightly crazy sometimes, but I realized it was “messy” so I “justified” the code to be uber-readable now!

function UpdateDescription(paymenttype) {
  var paymentinfo = "";
  
  var d = new Date();

  var dt = "";
  dt +=       pad( d.getUTCFullYear()     ,'left',4,"0");
  dt += "-" + pad( d.getUTCMonth()        ,'left',2,"0");
  dt += "-" + pad( d.getUTCDay()          ,'left',2,"0");
  dt += " " + pad( d.getHours()           ,'left',2,"0");
  dt += ":" + pad( d.getUTCMinutes()      ,'left',2,"0");
  dt += ":" + pad( d.getUTCSeconds()      ,'left',2,"0");
  dt += "." + pad( d.getUTCMilliseconds() ,'left',3,"0");
  return dt;
}

Am not even going to try and understand that at this point lol
YEY :slight_smile: think this is my first post I started to reach 100 LOL