Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial commit for alarm extension #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions doorbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Doorbot {
this.counter = 0;
this.userAgent = options.userAgent || 'android:com.ringapp:2.0.67(423)';
this.token = options.token || null;
this.oauthToken = options.oauthToken || null;
this.api_version = options.api_version || API_VERSION;

if (!this.username) {
Expand All @@ -70,7 +71,13 @@ class Doorbot {

fetch(method, url, query, body, callback) {
logger('fetch:', this.counter, method, url);
var d = parse('https://api.ring.com/clients_api' + url, true);
let useAuthToken = false;
let base = 'https://api.ring.com/clients_api' + url;
if (url.indexOf('http') > -1) {
base = url;
useAuthToken = true;
}
var d = parse(base, true);
logger('query', query);
delete d.path;
delete d.href;
Expand All @@ -87,6 +94,9 @@ class Doorbot {
logger('fetch-data', d);
d.method = method;
d.headers = d.headers || {};
if (useAuthToken) {
d.headers['Authorization'] = "Bearer " + this.oauthToken;
}
if (body) {
body = stringify(body);
d.headers['content-type'] = 'application/x-www-form-urlencoded';
Expand Down Expand Up @@ -230,16 +240,18 @@ class Doorbot {
res.on('end', () => {
let e = null;
let json = null;
let oauthToken = null;
let token = null;
try {
json = JSON.parse(data);
} catch (je) {
logger('JSON parse error', data);
logger(je);
e = new Error('JSON parse error from ring, check logging..');
}
let token = null;
if (json && json.access_token) {
token = json.access_token;
oauthToken = token;
logger('authentication_token', token);
}
if (!token || e) {
Expand Down Expand Up @@ -294,6 +306,7 @@ class Doorbot {
var self = this;
setTimeout(() => {
self.token = token;
self.oauthToken = oauthToken;
self.authenticating = false;
if (self.authQueue.length) {
logger(`Clearing ${self.authQueue.length} callbacks from the queue`);
Expand Down Expand Up @@ -430,3 +443,5 @@ class Doorbot {
module.exports = function(options) {
return new Doorbot(options);
};

module.exports.Doorbot = Doorbot;
23 changes: 22 additions & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('doorbot tests', () => {

it('should export stuff', () => {
assert.ok(RingAPI);
assert.ok(RingAPI.Doorbot);
});

it('authenticate', (done) => {
Expand All @@ -26,7 +27,7 @@ describe('doorbot tests', () => {
authentication_token: 'TOKEN'
}
});
const ring = RingAPI({
const ring = new RingAPI.Doorbot({
email: 'test',
password: 'test',
api_version: 11
Expand Down Expand Up @@ -552,6 +553,25 @@ describe('doorbot tests', () => {
});
});

it('work for alarm API', (done) => {
nock('https://app.ring.com').get('/api/v1/rs/connections')
.matchHeader('Authorization', 'Bearer OAUTH_TOKEN')
.reply(200, {
foo: {}
});
const ring = RingAPI({
username: 'test',
password: 'test'
});
ring.token = 'TOKEN';
ring.oauthToken = 'OAUTH_TOKEN';
ring.fetch('GET', 'https://app.ring.com/api/v1/rs/connections', {}, null, (e, json) => {
assert.equal(e, null);
assert.ok(json.foo);
done();
});
});

it('should error on a timeout', (done) => {
nock('https://api.ring.com').get('/clients_api/doorbots/12345/health')
.query({ auth_token: 'TOKEN', api_version: 9 })
Expand Down Expand Up @@ -613,4 +633,5 @@ describe('doorbot tests', () => {
ring.set_doorbot_dnd({ id: 1234 }, null, () => {});
}, /Number argument required/);
});

});