#amp #api-client #api-url #api #api-calls #rust

ampapi

一个允许您在 Rust 中与 AMP 安装进行通信的 API

7 个版本

0.1.6 2024 年 1 月 6 日
0.1.5 2023 年 11 月 24 日
0.1.4 2023 年 10 月 5 日
0.1.2 2023 年 9 月 20 日

#2 in #amp

GPL-3.0 许可证

170KB
2K SLoC

ampapi-rs

License Github Github Issues Discord wakatime

Github Releases Crates.io Docs.rs

一个允许您在 Rust 中与 AMP 安装进行通信的 API。

可以通过将 /API 添加到任何现有 AMP 安装的 URL 来找到可用 API 调用的文档。

支持

安装

cargo add ampapi

示例

常用 API 示例

use ampapi::modules::CommonAPI;
use ampapi::types::Status;

fn main() {
    // If you know the module that the instance is using, specify it instead of CommonAPI
    let api = CommonAPI::new(
        String::from("https://127.0.0.1:8080/"),
        String::from("admin"),
        String::from("myfancypassword123"),
        "".to_string(),
        "".to_string()
    );

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

    let current_status: Status = api.Core.GetStatus().unwrap();
    let cpu_usage_percent: f64 = current_status.Metrics.get("CPU Usage").unwrap().get("Percent").unwrap().as_f64().unwrap();

    println!("Current CPU usage is: {}%", cpu_usage_percent);
}

使用 ADS 管理实例的示例

use ampapi::modules::{ADS, Minecraft};
use ampapi::types::{IADSInstance, Status};

fn main() {
    let api = ADS::new(
        String::from("https://127.0.0.1:8080/"),
        String::from("admin"),
        String::from("myfancypassword123"),
        "".to_string(),
        "".to_string()
    );

    // Get the available instances
    let instances_result = api.ADSModule.GetInstances().unwrap();
    let targets: Vec<IADSInstance> = instances_result.result;

    // In this example, my Hub server is on the second target
    // If you're running a standalone setup, you can just use targets[0]
    // Get the instance ID of the Hub server
    let hub_instance_id = targets[1].AvailableInstances.iter().find(|&x| x.InstanceName == "Hub").unwrap().InstanceID.clone();

    // Use the instance ID to get the API for the instance
    let hub: Minecraft = api.instance_login(hub_instance_id, "Minecraft".to_string()).unwrap().into();

    // Get the current CPU usage
    let current_status: Status = api.Core.GetStatus().unwrap();
    let cpu_usage_percent: f64 = current_status.Metrics.get("CPU Usage").unwrap().get("Percent").unwrap().as_f64().unwrap();

    // Send a message to the console
    let _ = hub.Core.SendConsoleMessage(format!("say Current CPU usage is: {}%", cpu_usage_percent)).unwrap();
}
fn main() {
    // Under implementation
}

依赖项

~4–16MB
~235K SLoC