-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
131 lines (115 loc) · 3.09 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
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
'use strict'
var request = require('request')
var extend = require('extend')
var VERSION = require('./package.json').version
function SunsetWx (options) {
this.options = extend(true, {
email: null,
password: null,
key: null,
request_options: {
base_url: 'https://sunburst.sunsetwx.com/v1/',
headers: {'User-Agent': 'node-sunsetwx/' + VERSION}
}
}, options)
this.options.request_options.json = true // no overwriting this one
this.token = null
this.expiry = null
this.authRequest = null
// default request wrapper w/ user-agent, json, and user options
this.request = request.defaults(this.options.request_options)
}
SunsetWx.prototype.register = function (callback) {
this.request({
method: 'POST',
uri: '/register',
form: {
email: this.options.email,
password: this.options.password,
key: this.options.key
}
}, callback)
}
SunsetWx.prototype.login = function (callback) {
var that = this
this.request({
method: 'POST',
uri: '/login',
form: {
email: this.options.email,
password: this.options.password
}
}, function (err, response, body) {
if (err) { return callback(err) }
// on successful login, update token and expiration info,
// and create new request wrapper with auth header
that.token = body.token
that.expiry = Date.now() + (body.token_exp_sec * 1000)
that.authRequest = that.request.defaults({
headers: {'Authorization': 'Bearer ' + that.token}
})
callback(null, response, body)
})
}
SunsetWx.prototype.logout = function (callback) {
var that = this
this.__autoAuthRequest({
method: 'POST',
uri: '/logout'
}, function (err, response, body) {
if (err) { return callback(err) }
// on successful logout, wipe token, expiry, and request wrapper
that.token = null
that.expiry = null
that.authRequest = null
callback(null, response, body)
})
}
SunsetWx.prototype.deleteAccount = function (callback) {
this.__autoAuthRequest({
method: 'DELETE',
uri: '/account'
}, callback)
}
SunsetWx.prototype.passwordReset = function (callback) {
this.request({
method: 'POST',
uri: '/account/password/reset',
form: {
email: this.options.email
}
}, callback)
}
SunsetWx.prototype.coordinates = function (params, callback) {
this.__autoAuthRequest({
method: 'GET',
uri: '/coordinates',
qs: params
}, callback)
}
SunsetWx.prototype.location = function (params, callback) {
this.__autoAuthRequest({
method: 'GET',
uri: '/location',
qs: params
}, callback)
}
SunsetWx.prototype.quality = function (params, callback) {
this.__autoAuthRequest({
method: 'GET',
uri: '/quality',
qs: params
}, callback)
}
SunsetWx.prototype.__autoAuthRequest = function (params, callback) {
var that = this
if (!this.token || (this.expiry - Date.now()) / 1000 / 60 / 60 < 1) {
this.login(function (err, response, body) {
if (err) { return callback(err) }
that.authRequest(params, callback)
})
} else {
this.authRequest(params, callback)
}
}
module.exports = SunsetWx