NodeJS

Требуемые библиотеки:

Код SDK:

const axios = require('axios');

class PureServers {
    constructor(email, password) {
        this.email = email;
        this.password = password;
        this.session = null;
    }

    async request(url, method, data = {}, secured = false) {
        try {
            const res = await axios({
                url: `https://cp.pureservers.org/api/${url}`,
                method,
                ...(method === 'POST' ? { data } : {}),
                ...(secured && this.session ? {
                    headers: {
                        session: this.session
                    }
                } : {})
            });

            return { data: res?.data, headers: res?.headers, success: true };
        } catch(err) {
            return { data: err?.response?.data, headers: err?.response?.headers, success: false };
        }
    }

    async login() {
        const res = await this.request('auth/login', 'POST', { email: this.email, password: this.password });
        if(res.success) {
            this.session = res.headers.session;
            return true;
        }

        return res;
    }
}

Пример использования с получением списка серверов:

async function main() {
    const api = new PureServers('<email>', '<password>');
    const login = await api.login();
    if(login) console.log(await api.request('servers/list', 'GET', {}, true));
    else console.log('Failed to login', login);
}

main();

Last updated