69个发布
0.8.4 | 2024年2月23日 |
---|---|
0.8.2 | 2023年6月6日 |
0.8.0 | 2023年2月14日 |
0.7.8 | 2022年12月8日 |
0.7.3 | 2022年10月21日 |
#207 in 异步
800每月下载量
215KB
5K SLoC
coc.rs
用Rust编写的 Clash of Clans API包装器!
主要功能
- 异步代码
- 完全覆盖 Clash of Clans API
- 电子邮件和密码登录
- 能够用多个账户登录以处理多个并发请求
- API事件以跟踪更改
- Clash of Stats 支持
入门
安装
在您的 Cargo.toml 中添加此处的版本
[dependencies]
coc-rs = "0.8.3"
或者使用 cargo add
cargo add coc-rs --features=all
快速示例
#[tokio::main]
async fn main() {
let credentials = CredentialsBuilder::new()
.add_credential(env::var("username").unwrap(), env::var("password").unwrap())
.build();
let client = Client::new(credentials).await;
let player = client.get_player("#2PP".to_string()).await.unwrap();
println!("Player: {:?}", player);
}
错误处理
#[tokio::main]
async fn main() {
let credentials = CredentialsBuilder::new()
.add_credential(env::var("username").unwrap(), env::var("password").unwrap())
.build();
let client = Client::new(credentials).await;
let clan = match client.get_clan("#InvalidTag".to_string()).await {
Ok(clan) => {
println!("Clan: {:?}", clan);
}
Err(err) => {
match err {
APIError::ClientNotReady => {}, // API login hasn't been initialized yet, try not to request with milliseconds of initializing a client
APIError::FailedGetIP(err) => {}, // A request is made to api.ipify.org to set your IP dynamically when making keys, ensure this url isn't blocked
APIError::LoginFailed(err) => {}, // Failed to login to a Clash of Stats account
APIError::RequestFailed(err) => {}, // Request never made it to the API
APIError::InvalidHeader(err) => {}, // you should not get this
APIError::BadUrl(err) => {}, // you should also not get this
APIError::BadParameters => {}, // bad input parameters for endpoints that have this
APIError::AccessDenied => {}, // ip changed? or accessing something you shouldn't...
APIError::NotFound => {}, // bad input "tags" or banned players result in this
APIError::RequestThrottled => {}, // slow down!
APIError::UnknownError => {}, // 🤨
APIError::InMaintenance => {}, // doofus wait until it's over!
APIError::BadResponse(err, err_code) => {}, // Catch-all error for those that don't fall in any of the above
APIError::InvalidParameters(err) => {}, // I caught your parameter mistake, not the API!
APIError::InvalidTag(err) => {}, // malformed tag
APIError::EventFailure(err) => {}, // ? maybe I should remove this..
}
}
};
}
基本事件
首先创建一个将实现 EventHandler
特性的结构体,这类似于 serenity discord 库进行事件处理的方式。
struct Handler;
#[async_trait]
impl events::EventHandler for S {
/// Next we bring the player method in scope, and define the behaviour
async fn player(&self, _old_player: Option<player::Player>, _new_player: player::Player) {
println!("Player change detected!")
}
/// To handle errors in the events task we need a separate error handler
async fn handle_error(
&self,
_error: APIError,
_tag: Option<String>,
_event_type: EventType,
) {
println!("Houston we have a problem!")
}
}
然后在主函数中,我们将创建主函数,登录并添加我们想要从API中持续拉取数据的玩家和族标签。
#[tokio::test]
async fn main() {
//...
/// see above example on how to create a client
let task = tokio::spawn(async move {
// staring the API events in a separate thread
let mut event_builder = events::EventsListenerBuilder::new(client);
event_builder.add_player("#2PP").add_players(vec!["#CVJLQOLR"])
.build(Handler) // Building the EventListener struct
.start() // starting the continuous polling of the clan/player/war endpoints
.await;
});
task.await?;
}
注意:每个端点的缓存刷新时间不同。每个事件将在API中新缓存数据的确切时间触发。
功能
要启用 cos
功能(用于使用 Clash of Stats API),将此添加到您的 Cargo.toml
[dependencies]
coc-rs = { version = "0.8.3", features = ["cos"] }
要启用 extra
功能(它为您提供了额外工具),将此添加到您的 Cargo.toml
[dependencies]
coc-rs = { version = "0.8.3", features = ["extra"] }
要启用 tracing
功能(它提供了内置的调试/跟踪工具),将此添加到您的 Cargo.toml
[dependencies]
coc-rs = { version = "0.8.3", features = ["tracing"] }
或者所有三个
[dependencies]
coc-rs = { version = "0.8.3", features = ["all"] }
- 或者使用
cargo add
cargo add coc-rs --features cos # or extra...or tracing...or all (you get it)
测试
src/lib.rs
包含每个端点的示例测试。
贡献
贡献非常棒,也非常受欢迎!如果您有任何问题,请随意打开一个问题并开始工作。
免责声明
此内容与Supercell无关,未经其授权、赞助或批准。有关更多信息,请参阅Supercell粉丝内容政策。
依赖项
~14–26MB
~407K SLoC