1 个不稳定版本

0.1.0 2022 年 12 月 23 日

#1299 in HTTP 服务器

Apache-2.0

36KB
479

Rust 应用程序 API

Rust 应用程序 API 旨在简化使用 Rust 开发 KubOS 任务应用程序。

它提供了以下功能

  • 初始化日志记录
  • 提供发送 GraphQL 请求到服务的辅助函数

示例

use failure::{bail, Error};
use kubos_app::*;
use log::*;

fn main() -> Result<(), Error> {
    // Initialize logging
    logging_setup!("my-rust-app");
    
    // GraphQL request to turn on the radio
    let request = r#"mutation {
            power(state: ON) {
                success
            }
        }"#;

    // Send the GraphQL request to the radio sevice
    match query(&ServiceConfig::new("radio-service")?, request, Some(Duration::from_secs(1))) {
        Err(error) => bail!("Failed to communicate with radio service: {}", error),
        Ok(data) => {
            // Parse the response to verify that the operation was
            // successful
            if let Some(success) = data.get("power")
                .and_then(|power| power.get("success"))
            {
                match success.as_bool() {
                    Some(true) => println!("Successfully turned on radio"),
                    Some(false) => eprintln!("Failed to turn on radio"),
                    None => eprintln!("Failed to fetch radio power state")
                }
            } else {
                bail!("Failed to fetch radio power state");
            }
        }
    }
    Ok(())
}

依赖关系

~22MB
~461K SLoC