lots of important stuff (#1883)

* lots of important stuff

* Update Constants.js
This commit is contained in:
Gus Caplan
2017-09-04 10:49:44 -05:00
committed by Crawl
parent 3a503ef56e
commit 18e3801bb7
15 changed files with 212 additions and 253 deletions

73
src/rest/RESTManager.js Normal file
View File

@@ -0,0 +1,73 @@
const UserAgentManager = require('./UserAgentManager');
const handlers = require('./handlers');
const APIRequest = require('./APIRequest');
const routeBuilder = require('./APIRouter');
const { Error } = require('../errors');
const Constants = require('../util/Constants');
class RESTManager {
constructor(client, tokenPrefix = 'Bot') {
this.client = client;
this.handlers = {};
this.userAgentManager = new UserAgentManager(this);
this.rateLimitedEndpoints = {};
this.globallyRateLimited = false;
this.tokenPrefix = tokenPrefix;
this.versioned = true;
}
get api() {
return routeBuilder(this);
}
getAuth() {
const token = this.client.token || this.client.accessToken;
const prefixed = !!this.client.application || (this.client.user && this.client.user.bot);
if (token && prefixed) return `${this.tokenPrefix} ${token}`;
else if (token) return token;
throw new Error('TOKEN_MISSING');
}
get cdn() {
return Constants.Endpoints.CDN(this.client.options.http.cdn);
}
destroy() {
for (const handler of Object.values(this.handlers)) {
if (handler.destroy) handler.destroy();
}
}
push(handler, apiRequest) {
return new Promise((resolve, reject) => {
handler.push({
request: apiRequest,
resolve,
reject,
});
});
}
getRequestHandler() {
const method = this.client.options.apiRequestMethod;
if (typeof method === 'function') return method;
const handler = handlers[method];
if (!handler) throw new Error('RATELIMIT_INVALID_METHOD');
return handler;
}
request(method, url, options = {}) {
const apiRequest = new APIRequest(this, method, url, options);
if (!this.handlers[apiRequest.route]) {
this.handlers[apiRequest.route] = new handlers.RequestHandler(this, this.getRequestHandler());
}
return this.push(this.handlers[apiRequest.route], apiRequest);
}
set endpoint(endpoint) {
this.client.options.http.api = endpoint;
}
}
module.exports = RESTManager;