1.0.19 • Published 3 months ago

@neuralnexus/ampapi v1.0.19

Weekly downloads
-
License
-
Repository
github
Last release
3 months ago

ampapi-js

License Github Github Issues Discord wakatime

Github Releases NPM v NPM

An API that allows you to communicate with AMP installations from within JavaScript/TypeScript.

Documentation for available API calls can be found by appending /API to the URL of any existing AMP installation.

Support:

Installation

NPM

npm install @neuralnexus/ampapi

Yarn

yarn add @neuralnexus/ampapi

Bun

bun install @neuralnexus/ampapi

In The Browser via CDN (WIP)

Notes

  • If you find that some return types aren't matching up, please make an issue or a pull request.

Examples

CommonAPI Example

import { CommonAPI } from "@neuralnexus/ampapi";


// If you know the module that the instance is using, specify it instead of CommonAPI
const API = new CommonAPI("http://localhost:8080/", "admin", "myfancypassword123");
await API.APILogin();

// API call parameters are simply in the same order as shown in the documentation.
API.Core.SendConsoleMessage("say Hello Everyone, this message was sent from the TypeScript API!");

const currentStatus = await API.Core.GetStatus();
const CPUUsagePercent = currentStatus.Metrics["CPU Usage"].Percent;

console.log(`Current CPU usage is: " + ${CPUUsagePercent} "%`);

Example using the ADS to manage an instance

import { ADS } from "@neuralnexus/ampapi";


const API = new ADS("http://localhost:8080/", "admin", "myfancypassword123");
await API.APILogin();

// Get the available instances
const targets = await API.ADSModule.GetInstances();

// In this example, my Hub server is on the second target
// If you're running a standalone setup, you can just use targets[0]
const target = targets[1]

let hub_instance_id = "";

// Get the available instances
const instances = target.AvailableInstances;

for (const instance of instances) {
    // Find the instance named "Hub"
    if (instance.InstanceName == "Hub") {
        hub_instance_id = instance.InstanceID;
        break;
    }
}

// Use the instance ID to get the API for the instance
const Hub = await API.InstanceLogin(hub_instance_id, "Minecraft");

// Get the current CPU usage
const currentStatus = await Hub.Core.GetStatus();
const CPUUsagePercent = currentStatus.Metrics["CPU Usage"].Percent;

// Send a message to the console
await Hub.Core.SendConsoleMessage(`say Current CPU usage is: ${CPUUsagePercent}%`)

CommonAPI Example, handling the sessionId and rememberMeToken manually (not recommended)

import { CommonAPI } from "@neuralnexus/ampapi";


try {
    const API = new CommonAPI("http://localhost:8080/");

    // The third parameter is either used for 2FA logins, or if no password is specified to use a remembered token from a previous login, or a service login token.
    const loginResult = await API.Core.Login("admin", "myfancypassword123", "", false);

    if (loginResult.hasOwnProperty("success") && loginResult.success === true) {
        console.log("Login successful");
        API.sessionId = loginResult["sessionID"];

        // API call parameters are simply in the same order as shown in the documentation.
        await API.Core.SendConsoleMessage("say Hello Everyone, this message was sent from the Python API!");
        const currentStatus = await API.Core.GetStatus();
        const CPUUsagePercent = currentStatus.Metrics["CPU Usage"].Percent;
        console.log(`Current CPU usage is: ${CPUUsagePercent}%`);
    } else {
        console.log("Login failed");
        console.log(loginResult);
    }
} catch(err) {
    // In reality, you'd handle this exception better
    throw new Error(err)
}

TypeScript - CommonAPI Example

import { CommonAPI, Status } from "@neuralnexus/ampapi";


// If you know the module that the instance is using, specify it instead of CommonAPI
const API: CommonAPI = new CommonAPI("http://localhost:8080/", "admin", "myfancypassword123");
await API.APILogin();

// API call parameters are simply in the same order as shown in the documentation.
API.Core.SendConsoleMessage("say Hello Everyone, this message was sent from the TypeScript API!");

const currentStatus: Status = await API.Core.GetStatus();
const CPUUsagePercent: number = currentStatus.Metrics["CPU Usage"].Percent;

console.log(`Current CPU usage is: " + ${CPUUsagePercent} "%`);

TypeScript - Example using the ADS to manage an instance

import { ADS, IADSInstance, Instance, Minecraft, Result, Status, UUID } from "@neuralnexus/ampapi";


const API: ADS = new ADS("http://localhost:8080/", "admin", "myfancypassword123");
await API.APILogin();

// Get the available instances
const targets: IADSInstance[] = await API.ADSModule.GetInstances();

// In this example, my Hub server is on the second target
// If you're running a standalone setup, you can just use targets[0]
const target: IADSInstance = targets[1]

let hub_instance_id: UUID;

// Get the available instances
const instances: Instance[] = target.AvailableInstances;

for (const instance of instances) {
    // Find the instance named "Hub"
    if (instance.InstanceName == "Hub") {
        hub_instance_id = instance.InstanceID;
        break;
    }
}

// Use the instance ID to get the API for the instance
const Hub: Minecraft = await API.InstanceLogin(hub_instance_id, "Minecraft");

// Get the current CPU usage
const currentStatus: Status = await Hub.Core.GetStatus();
const CPUUsagePercent: number = currentStatus.Metrics["CPU Usage"].Percent;

// Send a message to the console
await Hub.Core.SendConsoleMessage(`say Current CPU usage is: ${CPUUsagePercent}%`)

TypeScript - CommonAPI Example, handling the sessionId and rememberMeToken manually (not recommended)

import { CommonAPI, LoginResult, Status } from "@neuralnexus/ampapi";


try {
    const API: CommonAPI = new CommonAPI("http://localhost:8080/");

    // The third parameter is either used for 2FA logins, or if no password is specified to use a remembered token from a previous login, or a service login token.
    const loginResult: LoginResult = await API.Core.Login("admin", "myfancypassword123", "", false);

    if (loginResult.hasOwnProperty("success") && loginResult.success === true) {
        console.log("Login successful");
        API.sessionId = loginResult["sessionID"];

        // API call parameters are simply in the same order as shown in the documentation.
        await API.Core.SendConsoleMessage("say Hello Everyone, this message was sent from the Python API!");
        const currentStatus: Status = await API.Core.GetStatus();
        const CPUUsagePercent: number = currentStatus.Metrics["CPU Usage"].Percent;
        console.log(`Current CPU usage is: ${CPUUsagePercent}%`);
    } else {
        console.log("Login failed");
        console.log(loginResult);
    }
} catch(err) {
    // In reality, you'd handle this exception better
    throw new Error(err)
}
1.0.19

3 months ago

1.0.18

3 months ago

1.0.17

3 months ago

1.0.16

4 months ago

1.0.15

4 months ago

1.0.14

4 months ago

1.0.13

5 months ago

1.0.12

7 months ago

1.0.11

7 months ago

1.0.10

8 months ago

1.0.9

8 months ago

1.0.8

8 months ago

1.0.7

8 months ago

1.0.6

8 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago