12个稳定版本
1.1.4 | 2021年3月26日 |
---|---|
1.1.3 | 2021年2月20日 |
1.1.0 | 2021年1月9日 |
1.0.5 | 2020年12月27日 |
#852 in 异步
26 每月下载量
69KB
1K SLoC
KSoft.rs 
纯Rust编写的KSoft API包装器
异步客户端用法
Cargo.toml
[dependencies.ksoft]
version = "1.1.4"
[dependencies.tokio]
version = "1.0"
features = ["macros"]
使用示例
use ksoft::Client;
#[tokio::main]
async fn main() {
let client = Client::new("TOKEN HERE"); //create the client
if let Ok(meme) = client.images.random_meme().await { //try to get a random meme handling the possible error
//Do some logical stuff here...
} else {
//Error handling stuff
}
}
此外,还有一个额外的错误管理工具,它是ApiResponse,它的行为与Result相同,因为它是对它的重命名,用于区分HTTP错误和API错误或失败的响应
pub type ApiResponse<S, E> = Result<S, E>;
ApiResponse示例
use ksoft::Client;
#[tokio::main]
async fn main() {
let client = Client::new("TOKEN HERE"); //create the client
if let Ok(image) = client.images.get_image("image id here").await { //image var will be ApiResponse<Image, ImageError>
match image {
Ok(image) => {
//Do something with the image
},
Err(why) => { //In this case, why will be an ImageError struct
//Do some handling stuff
}
}
} else {
//Error handling stuff
}
}
阻塞客户端用法
这是一个可选功能,供那些希望在没有异步上下文的情况下使用阻塞客户端的人使用
这两个功能 不能 同时启用
Cargo.toml
[dependencies.ksoft]
version = "1.1.4"
default-features=false
features = ["blocking"]
使用示例
use ksoft::blocking::Client;
fn main() {
let client = Client::new("TOKEN HERE"); //create the client
if let Ok(meme) = client.images.random_meme() { //try to get a random meme handling the possible error
//Do some logical stuff here...
} else {
//Error handling stuff
}
}
依赖项
~6–18MB
~267K SLoC