// library to create http(s) request
const request = require("request");
const SIMBLA_URI = "https://apps.simbla.com/parse/";
const APP_ID = "Your App_Id";
const USER = "UserName";
const PASSWORD = "Password";
let session, newCarsObjectId;
function login() {
return new Promise(function(resolve, reject) {
let options = {
url: SIMBLA_URI + "login",
headers: {
'X-Parse-Application-Id': APP_ID
},
json: true,
body: { username: USER, password: PASSWORD }
};
request.get(options, function(err, r, response) {
if (err)
reject(err)
else if (response.error) {
reject(response)
} else {
console.log(response);
session = response.sessionToken;
resolve(response);
}
})
})
}
function createCarsObject() {
return new Promise(function(resolve, reject) {
let options = {
url: SIMBLA_URI + "classes/Cars",
headers: {
'X-Parse-Application-Id': APP_ID,
'X-Parse-Session-Token': session
},
json: true,
body: { car_model: "Audi", car_year: "2017" }
};
request.post(options, function(err, r, response) {
if (err)
reject(err)
else if (response.error) {
reject(response)
} else {
console.log(response);
newCarsObjectId = response.objectId;
resolve(response)
}
})
})
}
//Here, note that the the URI is the regular Simbla URL plus the table in the database in
//which you want to create an object (for example, Classes/Cars).
function getCarsObject(){
return new Promise(function(resolve, reject) {
let options = {
url: SIMBLA_URI + "classes/Cars/" + newCarsObjectId,
headers: {
'X-Parse-Application-Id': APP_ID,
'X-Parse-Session-Token': session
}
};
request.get(options, function(err, r, response) {
if (err)
reject(err)
else if (response.error) {
reject(response)
} else {
console.log(response);
resolve(response)
}
})
})
}
function countCarsObjects() {
return new Promise(function(resolve, reject) {
let options = {
url: SIMBLA_URI + "classes/Cars",
headers: {
'X-Parse-Application-Id': APP_ID,
'X-Parse-Session-Token': session
},
json: true,
body: { count: 1, limit: 0 }
};
request.get(options, function(err, r, response) {
if (err)
reject(err)
else if (response.error) {
reject(response)
} else {
console.log(response);
resolve(response)
}
})
})
}
function deleteCarsObject() {
return new Promise(function(resolve, reject) {
let options = {
url: SIMBLA_URI + "classes/Cars/" + newCarsObjectId,
headers: {
'X-Parse-Application-Id': APP_ID,
'X-Parse-Session-Token': session
}
};
request.delete(options, function(err, r, response) {
if (err)
reject(err)
else if (response.error) {
reject(response)
} else {
console.log(response);
resolve(response)
}
})
})
}
login()
.then(createCarsObject)
.then(getCarsObject)
.then(countCarsObjects)
.then(deleteCarsObject)
.then(countCarsObjects)
.catch(function(err) {
console.error(err);
})