forked from julianfez/homebridge-augustlock2
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi.js
134 lines (121 loc) · 4.42 KB
/
api.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const request = require('request');
var AugustApi = function AugustApi(securityToken) {
securityToken = securityToken || {};
var getBaseRequest = function getBaseRequest() {
return ({
method: null,
url: 'https://api-production.august.com/',
headers: {
'x-august-access-token': securityToken,
'x-august-api-key': '79fd0eb6-381d-4adf-95a0-47721289d1d9',
'x-kease-api-key': '79fd0eb6-381d-4adf-95a0-47721289d1d9',
'Content-Type': 'application/json'
},
json: true
});
}
var makeRequest = async function makeRequest(option) {
return (await makeRawRequest(option)).body;
}
var makeRawRequest = function makeRawRequest(option) {
return new Promise(function (resolve, reject) {
request(option, function (error, response, body) {
if (error) {
return reject(error);
}
if (response.statusCode < 200 || response.statusCode > 299) {
return reject(new Error('Http error: ' + response.statusCode, response, body));
}
resolve({ response: response, body: body });
});
});
}
this.sendCodeToPhone = function sendCodeToPhone(phoneNumber) {
//
var option = getBaseRequest();
option.url += 'validation/phone';
option.method = 'POST';
option.body = {
value: phoneNumber
};
return makeRawRequest(option);
}
this.validatePhone = function validatePhone(phoneNumber, code) {
var option = getBaseRequest();
option.url += 'validate/phone';
option.method = 'POST';
option.body = {
code: code,
phone: phoneNumber
};
return makeRawRequest(option);
}
this.sendCodeToEmail = function sendCodeToEmail(emailAddress) {
//
var option = getBaseRequest();
option.url += 'validation/email';
option.method = 'POST';
option.body = {
value: emailAddress
};
return makeRawRequest(option);
}
this.validateEmail = function validateEmail(emailAddress, code) {
var option = getBaseRequest();
option.url += 'validate/email';
option.method = 'POST';
option.body = {
code: code,
email: emailAddress
};
return makeRawRequest(option);
}
this.authenticate = function authenticate(userid, password) {
// https://api-production.august.com/session
var option = getBaseRequest();
option.url += 'session';
option.method = 'POST';
option.body = {
identifier: userid,
installId: 'E629CCCC-A9E0-40F1-8BB8-43A24830346B',
password: password
};
return makeRawRequest(option);
}
this.getLocks = function getLocks() {
// https://api-production.august.com/users/locks/mine
var option = getBaseRequest();
option.url += 'users/locks/mine';
option.method = 'GET';
return makeRequest(option);
}
this.getLock = function getLock(lockId) {
// https://api-production.august.com/locks/{lockId}
var option = getBaseRequest();
option.url += 'locks/' + encodeURIComponent(lockId.toUpperCase());
option.method = 'GET';
return makeRequest(option);
}
this.getHouses = function getHouses() {
// https://api-production.august.com/houses/mine
var option = getBaseRequest();
option.url += 'houses/mine';
option.method = 'GET';
return makeRequest(option);
}
this.getHouse = function getHouse(houseId) {
// https://api-production.august.com/houses/00000000-1111-2222-3333-444444444444
var option = getBaseRequest();
option.url += 'houses/' + encodeURIComponent(houseId.toLowerCase());
option.method = 'GET';
return makeRequest(option);
}
this.remoteOperate = function remoteOperate(lockId, state) {
// PUT https://api-production.august.com/remoteoperate/{lockId}/{state}
var option = getBaseRequest();
option.url += 'remoteoperate/' + encodeURIComponent(lockId.toUpperCase()) + '/' + encodeURIComponent(state.toLowerCase());
option.method = 'PUT';
return makeRequest(option);
}
}
module.exports = AugustApi;