coc.rs
A Clash of Clans API wrapper written in Rust!
Key Features
- Asynchronous code
- Entire coverage of Clash of Clans API
- Email and password Login
- Ability to login with multiple accounts to handle many concurrent requests
- API Events to track changes
- Clash of Stats support
Getting Started
Installing
Add the version from here in your Cargo.toml
[dependencies]
coc-rs = "0.8.3"
Or with cargo add
cargo add coc-rs --features=all
Quick Example
#[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);
}
Error Handling
#[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 => {}, APIError::FailedGetIP(err) => {}, APIError::LoginFailed(err) => {}, APIError::RequestFailed(err) => {}, APIError::InvalidHeader(err) => {}, APIError::BadUrl(err) => {}, APIError::BadParameters => {}, APIError::AccessDenied => {}, APIError::NotFound => {}, APIError::RequestThrottled => {}, APIError::UnknownError => {}, APIError::InMaintenance => {}, APIError::BadResponse(err, err_code) => {}, APIError::InvalidParameters(err) => {}, APIError::InvalidTag(err) => {}, APIError::EventFailure(err) => {}, }
}
};
}
Basic Events
First a struct should be created that will implement the trait EventHandler
,
this is similar to how the serenity discord library does event handling.
struct Handler;
#[async_trait]
impl events::EventHandler for S {
async fn player(&self, _old_player: Option<player::Player>, _new_player: player::Player) {
println!("Player change detected!")
}
async fn handle_error(
&self,
_error: APIError,
_tag: Option<String>,
_event_type: EventType,
) {
println!("Houston we have a problem!")
}
}
Next in the main function, we will create the main function,
login and add the Player and clan tags we want to keep pulling the data from API.
#[tokio::test]
async fn main() {
let task = tokio::spawn(async move {
let mut event_builder = events::EventsListenerBuilder::new(client);
event_builder.add_player("#2PP").add_players(vec!["#CVJLQOLR"])
.build(Handler) .start() .await;
});
task.await?;
}
Note: Each endpoint has a different cache refresh time.
Each event will be fired at the exact time of new cache data in the API.
Features
To enable the cos
feature (to use the Clash of Stats API), add this to your Cargo.toml
[dependencies]
coc-rs = { version = "0.8.3", features = ["cos"] }
To enable the extra
feature (which gives you extra tools), add this to your Cargo.toml
[dependencies]
coc-rs = { version = "0.8.3", features = ["extra"] }
To enable the tracing
feature (which provides built-in debugging/tracing tools),
add this to your Cargo.toml
[dependencies]
coc-rs = { version = "0.8.3", features = ["tracing"] }
Or for all 3
[dependencies]
coc-rs = { version = "0.8.3", features = ["all"] }
- Alternately with
cargo add
cargo add coc-rs --features cos
Tests
src/lib.rs
contains examples as tests for every endpoint.
Contributing
Contributing is fantastic and much welcomed!
If you have an issue, feel free to open an issue and start working on it.
Disclaimer
This content is not affiliated with, endorsed, sponsored, or specifically
approved by Supercell and Supercell is not responsible for it.
For more information see Supercell's Fan Content Policy.