This commit is contained in:
hydrabolt
2016-04-16 22:58:49 +01:00
commit 9956e43c8e
43 changed files with 1634 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
'use strict';
const request = require('superagent');
const Constants = require('../../util/Constants');
const UserAgentManager = require('./UserAgentManager');
const RESTMethods = require('./RESTMethods');
class RESTManager{
constructor(client) {
this.client = client;
this.queue = [];
this.userAgentManager = new UserAgentManager(this);
this.methods = new RESTMethods(this);
}
makeRequest(method, url, auth, data, file) {
/*
file is {file, name}
*/
let apiRequest = request[method](url);
if (auth) {
if (this.client.store.token) {
apiRequest.set('authorization', this.client.store.token);
} else {
throw Constants.Errors.NO_TOKEN;
}
}
if (data) {
apiRequest.send(data);
}
if (file) {
apiRequest.attach('file', file.file, file.name);
}
apiRequest.set('User-Agent', this.userAgentManager.userAgent);
return new Promise((resolve, reject) => {
apiRequest.end((err, res) => {
if (err) {
reject(err);
} else {
resolve(res ? res.body || {} : {});
}
});
});
}
};
module.exports = RESTManager;