-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathsabre-dev-studio.js
More file actions
132 lines (124 loc) · 4.24 KB
/
sabre-dev-studio.js
File metadata and controls
132 lines (124 loc) · 4.24 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
/*
* sabre-dev-studio
* https://github.com/SabreLabs/sabre-dev-studio-node
*
* Copyright (c) 2014 Sabre Corp
* Licensed under the MIT license.
*/
'use strict';
var SabreDevStudio = (function() {
require('simple-errors');
function SabreDevStudio(options) {
var that = this
, loglevel = options.loglevel || 'warn'
, request = require('request')
, oauth2Factory = require('simple-oauth2')
, bunyan = require('bunyan')
, oauth2 = null
, log = bunyan.createLogger({name: 'SabreDevStudio', level: loglevel})
, errorCodes = {
400: 'BadRequest',
401: 'Unauthorized',
403: 'Forbidden',
404: 'NotFound',
406: 'NotAcceptable',
429: 'RateLimited',
500: 'InternalServerError',
503: 'ServiceUnavailable',
504: 'GatewayTimeout'
}
;
delete options.loglevel;
this.config = {
gzip: true
};
init(options);
function init(options) {
var clientID = function() {
return Buffer.from(that.config.client_id).toString('base64');
};
var clientSecret = function() {
return Buffer.from(that.config.client_secret).toString('base64');
};
var keys = ['client_id', 'client_secret', 'uri', 'access_token'];
keys.forEach(function(key) {
that.config[key] = options[key];
});
oauth2 = oauth2Factory({
clientID: clientID(),
clientSecret: clientSecret(),
site: options.uri,
tokenPath: '/v2/auth/token',
useBasicAuthorizationHeader: true
});
}
this.get = function(endpoint, options, callback, retryCount) {
this.request("GET", endpoint, "", options, callback, retryCount);
};
this.post = function(endpoint, post_body, options, callback, retryCount) {
this.request("POST", endpoint, post_body, options, callback, retryCount);
};
this.request = function(method, endpoint, post_body, options, callback, retryCount) {
if (typeof retryCount === 'undefined') {
retryCount = 1; // by default one retry
}
var headers = {};
for( var key in options) {
headers[key]= options[key];
}
options = options || {};
var cb = callback || function(error, response, data) { log.info(error, data); };
var fetchAccessToken = function(endpoint, options, cb, retryCount) {
log.info('Fetching fresh access token');
oauth2.client.getToken({}, function (error, token) {
if (error) {
var err = Error.http(error.statusCode, errorCodes[error.statusCode], error.data, error);
log.error('Error:', err);
cb(err);
} else {
that.config.access_token = token.access_token;
that.request(method, endpoint, post_body, options, cb, retryCount);
}
});
};
if (that.config.access_token) {
var requestUrl = that.config.uri + endpoint
, reqestOptions = {
gzip: that.config.gzip,
qs: options,
method: method,
body: post_body,
headers: headers,
timeout: 60 * 10000,
auth: {
"bearer": this.config.access_token
}
};
request[method.toLowerCase()](requestUrl, reqestOptions, function(error, response, data) {
if (error) {
if (error.code === 'ECONNRESET' && retryCount >= 0) {
fetchAccessToken(endpoint, options, cb, --retryCount);
} else {
if (error.data === '') { error.data = requestUrl; }
log.error('Error:', error);
cb(error, data, response);
}
} else if (response.statusCode === 200) {
cb(null, data, response);
} else if (response.statusCode === 401 && retryCount >= 0) {
fetchAccessToken(endpoint, options, cb, --retryCount);
} else if (errorCodes[response.statusCode]) {
var err = Error.http(response.statusCode, errorCodes[response.statusCode]);
cb(err, data, response);
}
});
} else {
if (retryCount >= 0) {
fetchAccessToken(endpoint, options, cb, --retryCount);
}
}
};
}
return SabreDevStudio;
})();
module.exports = SabreDevStudio;