hetznercloud/ts/classes.account.ts

46 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-01-29 20:14:05 +00:00
import { HetznerServer } from './classes.server.js';
2024-01-29 20:08:05 +00:00
import * as plugins from './hetznercloud.plugins.js';
export class HetznerAccount {
public token: string;
constructor(tokenArg: string) {
this.token = tokenArg;
}
2024-01-29 20:14:05 +00:00
public async getServers() {
return HetznerServer.getServers(this);
2024-01-29 20:08:05 +00:00
}
2024-02-18 22:45:03 +00:00
public async getServersByLabel(labelObject: plugins.tsclass.typeFestOwn.SecondArgument<typeof HetznerServer.getServersByLabel>) {
2024-02-18 22:41:50 +00:00
return HetznerServer.getServersByLabel(this, labelObject);
2024-02-18 22:23:20 +00:00
}
2024-02-17 20:55:25 +00:00
public async createServer(optionsArg: plugins.tsclass.typeFestOwn.SecondArgument<typeof HetznerServer.create>) {
return HetznerServer.create(this, optionsArg);
}
2024-01-29 20:08:05 +00: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 20:55:25 +00:00
console.log(`Url: ${url}`);
console.log(`Method: ${methodArg}`);
console.log(`Payload: ${JSON.stringify(payloadArg, null, 2)}`);
2024-01-29 20:08:05 +00:00
const response = await plugins.smartrequest.request(url, {
method: methodArg,
headers: {
Authorization: `Bearer ${this.token}`,
},
requestBody: payloadArg,
2024-02-18 00:17:15 +00:00
keepAlive: false,
2024-01-29 20:08:05 +00:00
});
2024-02-17 20:55:25 +00:00
console.log(response.statusCode);
console.log(response.body);
2024-01-29 20:08:05 +00:00
return response;
}
}