3 个不稳定版本
0.2.0 | 2020年3月29日 |
---|---|
0.1.1 | 2018年8月13日 |
0.1.0 | 2018年8月6日 |
#4 in #gathering
89KB
2K SLoC
MTG-API 客户端
Rust 对 https://magicthegathering.io/ API 的封装
有关功能的详细文档可在此处找到: https://docs.magicthegathering.io/
此封装提供调用 API 相关的方法以及将提供的 JSON 数据转换为 Rust 对象的转换。
文档
文档可在 docs.rs 上找到: https://docs.rs/mtgapi-client
集成
# Cargo.toml
[dependencies]
mtgapi-client = "0.1"
# main.rs / lib.rs
extern crate mtgapi_client;
使用方法
// Create a client with the specified (per request) timeout
use mtgapi_client::MtgClient;
let api = MtgClient::new(100);
示例:获取第 20 - 25 页上的所有卡牌
默认情况下,卡牌请求的页面大小为 100 张卡牌。
// Create a unfiltered cards request
let mut get_cards_request = api.cards().all();
//Start at Page 20
get_cards_request.set_page(20);
let mut cards: Vec<CardDetail> = Vec::new();
//collect all cards from pages 20 to 25
for _ in 0..5 {
let response = get_cards_request.next_page().await?
let cards = response.content;
if cards.is_empty() {
break;
}
unfiltered_cards.extend(cards);
}
println!("Unfiltered Cards: {:?}", unfiltered_cards);
示例:获取匹配特定过滤器的所有卡牌
// Create a filtered cards request
let mut get_cards_request = api.cards().all_filtered(
CardFilter::builder()
.game_format(GameFormat::Standard)
.cardtypes_or(&[CardType::Instant, CardType::Sorcery])
.converted_mana_cost(2)
.rarities(&[CardRarity::Rare, CardRarity::MythicRare])
.build(),
);
let mut cards: Vec<CardDetail> = Vec::new();
//collect all cards matching the filter
loop {
let response = get_cards_request.next_page().await?
let cards = response.content;
if cards.is_empty() {
break;
}
filtered_cards.extend(cards);
}
println!("Filtered Cards: {:?}", filtered_cards);
一些示例 API 调用
// Get all sets
let sets = api.sets().all().await?.content;
println!("All Sets: {:?}", sets);
// Get an example booster pack from the specified set
let booster_cards = api.sets().booster("ktk").await?.content;
println!("Random Cards from Booster: {:?}", booster_cards);
// Get all card types
let types = api.types().all().await?.content;
println!("Types: {:?}", types);
// Get all card subtypes
let subtypes = api.subtypes().all().await?.content;
println!("Subtypes: {:?}", subtypes);
// Get all card supertypes
let supertypes = api.supertypes().all().await?.content;
println!("Supertypes: {:?}", supertypes);
// Get all game formats
let formats = api.formats().all().await?.content;
println!("Formats: {:?}", formats);
依赖关系
~23MB
~544K SLoC