Skip to content

Commit

Permalink
add useOwnCredits to start, use json body instead of form-data
Browse files Browse the repository at this point in the history
  • Loading branch information
matthi4s committed May 5, 2023
1 parent 9bda67d commit 280a72d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 124 deletions.
102 changes: 2 additions & 100 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "exaroton",
"version": "1.8.3",
"version": "1.9.0",
"description": "exaroton API client",
"homepage": "https://exaroton.com",
"main": "index.js",
Expand All @@ -11,7 +11,6 @@
"node": ">=12.0.0"
},
"dependencies": {
"form-data": "^3.0.1",
"got": "^11.8.2",
"ws": "^7.4.5"
}
Expand Down
10 changes: 5 additions & 5 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,22 @@ class Client {
async request(request) {
request.client = this;
const url = this.baseURL + request.getEndpoint();
const headers = Object.assign({
"authorization": "Bearer " + this.#apiToken,
"user-agent": this.#userAgent
}, request.headers);

let gotOptions = {
method: request.method,
retry: 0,
headers: headers,
responseType: request.responseType
};

if (request.hasBody()) {
gotOptions.body = request.getBody();
}

gotOptions.headers = Object.assign({
"authorization": "Bearer " + this.#apiToken,
"user-agent": this.#userAgent
}, request.headers);

let response;
try {
if (request.hasOutputStream()) {
Expand Down
17 changes: 2 additions & 15 deletions src/Request/Request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const FormData = require('form-data');
const {createReadStream} = require('fs');
const {createWriteStream} = require("fs");

Expand Down Expand Up @@ -149,20 +148,8 @@ class Request {
return this.data;
}

let body = new FormData();
for (let key in this.data) {
if (!this.data.hasOwnProperty(key)) {
continue;
}
if (Array.isArray(this.data[key])) {
for (let element of this.data[key]) {
body.append(key + "[]", element);
}
continue;
}
body.append(key, this.data[key]);
}
return body;
this.setHeader("content-type", "application/json");
return JSON.stringify(this.data);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Request/Server/StartServerRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ const ServerRequest = require('./ServerRequest');

class StartServerRequest extends ServerRequest {
endpoint = "servers/{id}/start";
method = "POST";

/**
* StartServerRequest constructor
*
* @param {string} id
* @param {boolean} useOwnCredits
*/
constructor(id, useOwnCredits = false) {
super(id);
this.data = {
useOwnCredits: useOwnCredits
};
}
}

module.exports = StartServerRequest;
5 changes: 3 additions & 2 deletions src/Server/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ class Server extends EventEmitter {
/**
* Start the server
*
* @param {boolean} useOwnCredits
* @return {Promise<Response>}
* @throws {RequestError}
*/
async start() {
return this.#client.request(new StartServerRequest(this.id));
async start(useOwnCredits = false) {
return this.#client.request(new StartServerRequest(this.id, useOwnCredits));
}

/**
Expand Down

0 comments on commit 280a72d

Please sign in to comment.