-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (55 loc) · 1.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var md5 = require('md5');
var fetch = require('node-fetch');
const querystring = require('querystring');
var publicKey = process.env.STURENTS_PUBLIC_KEY;
var apiKey = process.env.STURENTS_API_KEY;
var landlordId = process.env.LANDLORD_ID;
const URI = "https://sturents.com/api";
const URI_HOUSES = URI + "/houses";
const URI_HOUSE = URI + "/house";
function getAuth(json){
var authString = json + apiKey;
return md5(authString);
}
function checkLandlord(){
if (!landlordId){
throw "Set LANDLORD_ID env variable";
}
}
function fetchHouses(callback){
if (!publicKey){
throw "Set STURENTS_PUBLIC_KEY env variable";
}
checkLandlord();
// HTTP GET request
fetch(URI_HOUSES + '?' + querystring.stringify({ landlord: landlordId, public: publicKey }))
.then(function(res) {
return res.text();
})
.then(function(json){
var request = JSON.parse(json);
callback(request);
})
.catch(function(err) {
console.log(err);
});
}
function addHouse(request, callback){
if (!apiKey){
throw "Set STURENTS_API_KEY env variable";
}
checkLandlord();
var json = JSON.stringify(request);
var auth = getAuth(json);
// HTTP POST request
fetch(URI_HOUSE + '?' + querystring.stringify({landlord : landlordId, auth: auth}), { method: 'POST', body: json })
.then(function(res){
return res.text();
}).then(function(json){
console.log(json);
var response = JSON.parse(json);
callback && callback(response);
});
}
exports.fetchHouses = fetchHouses;
exports.addHouse = addHouse;