-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathApiHelper.js
More file actions
225 lines (208 loc) · 8.37 KB
/
ApiHelper.js
File metadata and controls
225 lines (208 loc) · 8.37 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const LocalStorageCredentialStore = require('./LocalStorageCredentialStore.js').default;
const AxiosCall = require('./AxiosCall.js').default;
/**
* Helper method for GeotabApi.
* Allows logic to remain somewhat encapsulated from the user
*/
class ApiHelper {
constructor(authentication, options){
if(typeof authentication !== 'object'){
throw new Error('authentication object not provided - must provide an authentication object to instantiate GeotabApi');
}
this.options = {
fullResponse: typeof options.fullResponse === 'undefined' ? false : options.fullResponse,
rememberMe: typeof options.rememberMe === 'undefined' ? true : options.rememberMe,
timeout: typeof options.timeout === 'undefined' ? 180 : options.timeout,
newCredentialStore: typeof options.newCredentialStore === 'undefined' ? false : options.newCredentialStore
}
this.credentialStore = this.createStore(options.newCredentialStore);
if(authentication.credentials.sessionId){
// Throwing an error if path isn't provided when using sessionID
if(typeof authentication.path === 'undefined'){
throw new Error('Path not provided - Must provide server path if providing sessionId in authentication object');
}
if(this.options.rememberMe){
this.credentialStore.set(authentication.credentials, authentication.path);
}
}
this.cred = authentication.credentials;
this.server = authentication.path;
}
/**
* Getters/Setters
* Helps reduce code when calling from GeotabApi
*/
get sessionId(){
return this.cred.sessionId;
}
get database(){
return this.cred.database;
}
get userName(){
return this.cred.userName;
}
get password(){
return this.cred.password;
}
get rememberMe(){
return this.options.rememberMe;
}
get fullResponse(){
return this.options.fullResponse;
}
/**
* Creates the credentialStore. Either an object provided by the user,
* created as a mock localStorage object (node) or a reference to
* the actual localStorage (browser)
* @param {object} newStore an Override credentialStore
*/
createStore(newStore){
return newStore || new LocalStorageCredentialStore();
}
/**
* checks the credentialStore for stored credentials
* @returns false if no store, credentials object if there are credentials
*/
getLocalCredentials(){
let store = this.credentialStore.get();
if(store){
if(store.server){
this.server = store.server;
}
return store.credentials;
}
return store;
}
/**
* Sets the local credentials
* @param {object} result credentials object using result of a promise
*/
setLocalCredentials(result, server){
this.credentialStore.set(result.credentials, server);
}
clearLocalCredentials(){
this.credentialStore.clear();
}
/**
* Gets and stores authentication response using GeotabApi.authenticate()
* @param {function} authenticate API authentication method - context binded to GeotabApi
* @param {function} callbackError optional callback error for unsuccessful authentications
* @param {boolean} rememberMe stores authentication once received
*/
async getAuthentication(authenticate, callbackError, rememberMe){
let credentials;
await authenticate(null, callbackError).then( response => {
// Response changes based on error handling.
let data = response.data ? response.data.result : response;
if(data.path !== 'ThisServer'){
this.server = data.path;
}
// Successful authentication
if(rememberMe){
this.setLocalCredentials(data.credentials, this.server);
}
credentials = data.credentials;
this.cred.sessionId = credentials.sessionId;
}).catch(err => {
throw err;
});
return credentials;
}
/**
* When the user doesn't have fullResposne enabled, we put some promises in front of the call
* to return only data.result to the user
* @param {Promise} call Axios call
*/
parseAxiosResponse(call){
call = call
.then( response => {
// Error handling has already taken errors out in either errorHandle() or AxiosCall.send()
if(response.data.result){
return response.data.result;
}
})
.catch(err => {
throw err;
});
return call;
}
/**
* Error handling - only called if fullresponse isn't enabled, and if the user hasn't provided a callback
* @param {Promise} call Axios call
*/
errorHandle(call){
// User hasn't asked for the full axios object, so we should error check
call = call
.then( response => {
if(response.status !== 200){
throw new Error(`Response ${response.status} - ${response.statusText}`);
}
return response;
})
.then( response => {
if(response.data.error){
throw new Error(`${response.data.error.name} - ${response.data.error.message}`);
}
return response;
})
.catch( err => {
throw err;
});
return call;
}
/**
* Creates a new AxiosCall and returns it to the user. If the call fails,
* and the call isn't an authentication, the call will attempt to
* refresh the credentials
*
* @param {string} method method name for the API call
* @param {Object} params method's parameters
* @param {function} callbackSuccess Optional callback for successful calls
* @param {function} callbackError Optional callback for unsuccessful calls
* @param {function} authenticate authentication function. Context binded to GeotabApi
* @param {boolean} rememberMe should store authentication results
*/
async sendAxiosCall(method, params, callbackSuccess, callbackError, authenticate, rememberMe){
let call = new AxiosCall(method, this.server, params, callbackSuccess, callbackError).send(this.options.timeout);
// We don't want to continue retrying if we fail authentication
if(method !== 'Authenticate'){
call.then(async response => {
let data = response.data;
// Checking for a failed authentication
if(data.error){
if(data.error.name === 'InvalidUserException'){
// Doesn't hurt to clear even if we're not remembering/storing them
this.clearLocalCredentials();
// Assuming there was a timeout, re-running authentication
let auth = await this.getAuthentication(authenticate, callbackError, rememberMe);
if(auth.result){ // Successful re-authentication
params.credentials = auth.result.credentials;
return new AxiosCall(method, this.server, params, callbackSuccess, callbackError).send(this.options.timeout);
} else { // Unsuccessful
return auth.error
}
}
} else {
return response;
}
}).catch(err => {
throw err;
});
} else {
call.then( response => {
if(response.data.result){
let server = response.data.result.path === 'ThisServer' ? this.server : response.data.result.path;
this.server = server;
if(this.rememberMe){
this.setLocalCredentials(response.data.result, server);
}
}
return response;
}).catch(err => {
throw err;
});
}
return call;
}
}
exports.default = ApiHelper;