Crate easy_msr_api

Crate easy_msr_api 

Source
Expand description

§MSR API Rust 封装库

这是一个为MSR API(明日方舟音乐API)提供Rust封装的库,支持直接API调用和Swagger-UI文档。

§功能特性

  • 核心API封装:完整的MSR API功能封装,可直接调用
  • 可选Web路由:提供Axum Web路由集成,包含Swagger UI文档界面
  • 类型安全:完整的DTO类型定义

§使用方式

§1. 使用默认客户端(推荐)

use easy_msr_api::MSRApiClient;
 
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 使用默认的MSR API地址
    let client = MSRApiClient::new();
     
    let albums = client.get_all_albums().await?;
    println!("专辑数量: {}", albums.data.len());
     
    Ok(())
}

§2. 作为库直接调用API

use easy_msr_api::client::remote::RemoteApiClient;
 
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 创建API客户端
    let client = RemoteApiClient::new("https://monster-siren.hypergryph.com/api".to_string());
     
    // 直接调用API方法
    let song = client.get_song("123456".to_string()).await?;
    println!("歌曲名称: {}", song.data.name);
     
    Ok(())
}

§3. 作为Web服务使用(需要启用web feature)

use easy_msr_api::{client::remote::RemoteApiClient, web};
use std::net::Ipv4Addr;
 
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = RemoteApiClient::new("https://monster-siren.hypergryph.com/api".to_string());
    let app = web::routes(client);
     
    // 启动服务器
    let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 8080)).await?;
    println!("服务器运行在 http://localhost:8080");
    println!("Swagger UI文档: http://localhost:8080/swagger-ui/");
    axum::serve(listener, app).await?;
     
    Ok(())
}

或者直接在本项目下执行cargo run --bin server --features web

§Cargo Features

  • default: 无额外功能,仅包含核心API封装
  • web: 启用Web路由和Swagger UI界面支持

§模块结构

  • client - API客户端实现
  • config - 配置管理
  • error - 错误处理
  • Web路由层(需要启用 web feature)

Modules§

client
客户端模块
config
配置管理模块
dto
数据传输对象 (DTO)
error
错误处理模块

Structs§

MSRApiClient
默认的MSR API客户端,使用官方API地址