2024-01-29 21:14:05 +01:00
|
|
|
import { HetznerServer } from './classes.server.js';
|
2024-01-29 21:08:05 +01:00
|
|
|
import * as plugins from './hetznercloud.plugins.js';
|
|
|
|
|
|
|
|
|
|
export class HetznerAccount {
|
|
|
|
|
public token: string;
|
|
|
|
|
constructor(tokenArg: string) {
|
|
|
|
|
this.token = tokenArg;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 21:14:05 +01:00
|
|
|
public async getServers() {
|
|
|
|
|
return HetznerServer.getServers(this);
|
2024-01-29 21:08:05 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-18 23:45:03 +01:00
|
|
|
public async getServersByLabel(labelObject: plugins.tsclass.typeFestOwn.SecondArgument<typeof HetznerServer.getServersByLabel>) {
|
2024-02-18 23:41:50 +01:00
|
|
|
return HetznerServer.getServersByLabel(this, labelObject);
|
2024-02-18 23:23:20 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-17 21:55:25 +01:00
|
|
|
public async createServer(optionsArg: plugins.tsclass.typeFestOwn.SecondArgument<typeof HetznerServer.create>) {
|
|
|
|
|
return HetznerServer.create(this, optionsArg);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 21:08:05 +01:00
|
|
|
/**
|
|
|
|
|
* request things from the hetzner API
|
|
|
|
|
* @param methodArg
|
|
|
|
|
* @param pathArg
|
|
|
|
|
* @param payloadArg
|
|
|
|
|
*/
|
|
|
|
|
public request = async (methodArg: string, pathArg: string, payloadArg: any) => {
|
|
|
|
|
const url = `https://api.hetzner.cloud/v1${pathArg}`;
|
2024-02-17 21:55:25 +01:00
|
|
|
console.log(`Url: ${url}`);
|
|
|
|
|
console.log(`Method: ${methodArg}`);
|
|
|
|
|
console.log(`Payload: ${JSON.stringify(payloadArg, null, 2)}`);
|
2026-05-07 23:31:22 +00:00
|
|
|
const request = plugins.smartrequest.SmartRequest.create()
|
|
|
|
|
.url(url)
|
|
|
|
|
.headers({
|
2024-01-29 21:08:05 +01:00
|
|
|
Authorization: `Bearer ${this.token}`,
|
2026-05-07 23:31:22 +00:00
|
|
|
})
|
|
|
|
|
.options({
|
|
|
|
|
keepAlive: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (methodArg !== 'GET' && methodArg !== 'DELETE') {
|
|
|
|
|
request.json(payloadArg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = methodArg === 'POST'
|
|
|
|
|
? await request.post()
|
|
|
|
|
: methodArg === 'PUT'
|
|
|
|
|
? await request.put()
|
|
|
|
|
: methodArg === 'PATCH'
|
|
|
|
|
? await request.patch()
|
|
|
|
|
: methodArg === 'DELETE'
|
|
|
|
|
? await request.delete()
|
|
|
|
|
: await request.get();
|
|
|
|
|
const body = await response.json().catch(() => ({}));
|
|
|
|
|
const responseObject = {
|
|
|
|
|
statusCode: response.status,
|
|
|
|
|
body,
|
|
|
|
|
};
|
|
|
|
|
console.log(responseObject.statusCode);
|
|
|
|
|
console.log(responseObject.body);
|
|
|
|
|
return responseObject;
|
2024-01-29 21:08:05 +01:00
|
|
|
}
|
|
|
|
|
}
|