rewrite endpoints for consistancy and modularity (#1323)

* rewrite endpoints for consistancy and modularity

* clean up a bit

* add missing endpoint
This commit is contained in:
Gus Caplan
2017-03-31 12:36:09 -05:00
committed by Amish Shah
parent 1cb227d6d8
commit 1df3b84da6
5 changed files with 223 additions and 215 deletions

View File

@@ -2,14 +2,15 @@ const request = require('superagent');
const Constants = require('../../util/Constants');
class APIRequest {
constructor(rest, method, url, auth, data, files) {
constructor(rest, method, path, auth, data, files) {
this.rest = rest;
this.client = rest.client;
this.method = method;
this.url = url;
this.path = path.toString();
this.auth = auth;
this.data = data;
this.files = files;
this.route = this.getRoute(this.url);
this.route = this.getRoute(this.path);
}
getRoute(url) {
@@ -23,16 +24,17 @@ class APIRequest {
}
getAuth() {
if (this.rest.client.token && this.rest.client.user && this.rest.client.user.bot) {
return `Bot ${this.rest.client.token}`;
} else if (this.rest.client.token) {
return this.rest.client.token;
if (this.client.token && this.client.user && this.client.user.bot) {
return `Bot ${this.client.token}`;
} else if (this.client.token) {
return this.client.token;
}
throw new Error(Constants.Errors.NO_TOKEN);
}
gen() {
const apiRequest = request[this.method](this.url);
const API = `${this.client.options.http.host}/api/v${this.client.options.http.version}`;
const apiRequest = request[this.method](`${API}${this.path}`);
if (this.auth) apiRequest.set('authorization', this.getAuth());
if (this.files) {
for (const file of this.files) if (file && file.file) apiRequest.attach(file.name, file.file, file.name);
@@ -41,7 +43,7 @@ class APIRequest {
} else if (this.data) {
apiRequest.send(this.data);
}
if (!this.rest.client.browser) apiRequest.set('User-Agent', this.rest.userAgentManager.userAgent);
if (!this.client.browser) apiRequest.set('User-Agent', this.rest.userAgentManager.userAgent);
return apiRequest;
}
}