use curseforge::official::prelude::*;
use once_cell::sync::Lazy;
static PROXY_API_BASE: &str = "https://api.curse.tools/v1/cf/";
static TOKEN_VARIABLE: &str = "CURSEFORGE_API_TOKEN";
static CLIENT_OPTIONS: ClientOptions = ClientOptions {
max_connections: 1,
};
const GAME_TERRARIA: i32 = 431;
const GAME_MINECRAFT: i32 = 432;
static CLIENT: Lazy<Client> = Lazy::new(|| match std::env::var(TOKEN_VARIABLE) {
Ok(token) => {
eprintln!("Using official CurseForge API with token.");
Client::new(e::DEFAULT_API_BASE, Some(token), Some(&CLIENT_OPTIONS)).unwrap()
}
Err(_) => {
eprintln!("Using proxy for CurseForge API without token.");
Client::new(PROXY_API_BASE, None, Some(&CLIENT_OPTIONS)).unwrap()
}
});
static SAMPLE_PROJECTS: Lazy<Vec<Project>> = Lazy::new(|| {
smol::block_on(async {
use smol::pin;
use smol::stream::StreamExt;
let params = ProjectSearchParams::game(GAME_MINECRAFT);
let search = CLIENT.search_projects_iter(params);
pin!(search);
let mut projects = Vec::new();
while let Some(result) = search.next().await {
match result {
Ok(project) => projects.extend([project]),
Err(error) => panic!("{}", error),
}
}
projects
})
});
#[test]
fn game() {
smol::block_on(async {
let game = CLIENT.game(GAME_TERRARIA).await;
match &game {
Ok(_game) => (),
Err(error) => panic!("{}", error),
}
});
}
#[test]
fn games() {
smol::block_on(async {
let params = GamesParams::default();
let games = CLIENT.games(¶ms).await;
match &games {
Ok(_games) => (),
Err(error) => panic!("{}", error),
}
});
}
#[test]
fn game_versions() {
smol::block_on(async {
let versions = CLIENT.game_versions(GAME_MINECRAFT).await;
match &versions {
Ok(_games) => (),
Err(error) => panic!("{}", error),
}
});
}
#[test]
fn game_version_types() {
smol::block_on(async {
let params = GamesParams::default();
let games = CLIENT.games(¶ms).await;
match &games {
Ok(_games) => (),
Err(error) => panic!("{}", error),
}
});
}
#[test]
fn categories() {
smol::block_on(async {
let params = CategoriesParams::game(GAME_MINECRAFT);
let categories = CLIENT.categories(¶ms).await;
match &categories {
Ok(_categories) => (),
Err(error) => panic!("{}", error),
}
});
}
#[test]
fn search_projects() {
smol::block_on(async {
let params = ProjectSearchParams::game(GAME_MINECRAFT);
let result = CLIENT.search_projects(¶ms).await;
match &result {
Ok(_response) => (),
Err(error) => panic!("{}", error),
}
});
}
#[test]
fn search_projects_iter() {
assert!(!SAMPLE_PROJECTS.is_empty())
}
#[test]
fn project() {
smol::block_on(async {
let projects = &SAMPLE_PROJECTS[..500];
let project_ids = projects.iter().map(|project| project.id);
for project in project_ids {
let result = CLIENT.project(project).await;
match result {
Ok(_project) => (),
Err(error) => panic!("{}", error),
}
}
});
}
#[test]
fn projects() {
smol::block_on(async {
let projects = &SAMPLE_PROJECTS[..3000];
let project_ids = projects.iter().map(|project| project.id);
let result = CLIENT.projects(project_ids).await;
match result {
Ok(_projects) => (),
Err(error) => panic!("{}", error),
}
});
}
#[test]
fn featured_projects() {
smol::block_on(async {
let body = FeaturedProjectsBody::game(GAME_MINECRAFT);
let result = CLIENT.featured_projects(&body).await;
match result {
Ok(_featured) => (),
Err(error) => panic!("{}", error),
}
});
}
#[test]
fn project_description() {
smol::block_on(async {
let projects = &SAMPLE_PROJECTS[..150];
let project_ids = projects.iter().map(|project| project.id);
for project in project_ids {
let result = CLIENT.project_description(project).await;
match result {
Ok(_description) => (),
Err(error) => panic!("{}", error),
}
}
});
}
#[test]
fn project_file() {
use std::collections::HashMap;
smol::block_on(async {
let projects = &SAMPLE_PROJECTS[..150];
let project_files = projects
.iter()
.map(|project| (project.id, project.latest_files.iter().map(|file| file.id)))
.collect::<HashMap<_, _>>();
for (project, files) in project_files.into_iter() {
for file in files {
let result = CLIENT.project_file(project, file).await;
match result {
Ok(_file) => (),
Err(error) => panic!("{}", error),
}
}
}
});
}
#[test]
fn project_file_by_id() {
smol::block_on(async {
let projects = &SAMPLE_PROJECTS[..150];
let files = projects
.iter()
.flat_map(|project| project.latest_files.iter().map(|file| file.id));
for file in files {
let result = CLIENT.project_file_by_id(file).await;
match result {
Ok(_file) => (),
Err(error) => panic!("{}", error),
}
}
});
}
#[test]
fn project_files() {
smol::block_on(async {
let params = ProjectFilesParams::default();
let projects = &SAMPLE_PROJECTS[..3000];
let project_ids = projects.iter().map(|project| project.id);
for project in project_ids {
let result = CLIENT.project_files(project, ¶ms).await;
match result {
Ok(_projects) => (),
Err(error) => panic!("{}", error),
}
}
});
}
#[test]
fn project_files_iter() {
use smol::pin;
use smol::stream::StreamExt;
smol::block_on(async {
let params = ProjectFilesParams::default();
let projects = &SAMPLE_PROJECTS[..3000];
let project_ids = projects.iter().map(|project| project.id);
for project in project_ids {
let files = CLIENT.project_files_iter(project, params.clone());
pin!(files);
while let Some(result) = files.next().await {
match result {
Ok(_file) => (),
Err(error) => panic!("{}", error),
}
}
}
});
}
#[test]
fn project_files_by_ids() {
smol::block_on(async {
let projects = &SAMPLE_PROJECTS[..3000];
let file_ids = projects.iter().map(|project| project.main_file_id);
let result = CLIENT.project_files_by_ids(file_ids).await;
match result {
Ok(_files) => {
}
Err(error) => panic!("{}", error),
}
});
}
#[test]
fn project_file_changelog() {
use std::collections::HashMap;
smol::block_on(async {
let projects = &SAMPLE_PROJECTS[..500];
let project_files = projects
.iter()
.map(|project| (project.id, project.main_file_id))
.collect::<HashMap<_, _>>();
for (project, file) in project_files.into_iter() {
let result = CLIENT.project_file_changelog(project, file).await;
match result {
Ok(_changelog) => (),
Err(error) => panic!("{}", error),
}
}
});
}
#[test]
fn project_file_download_url() {
smol::block_on(async {
let projects = &SAMPLE_PROJECTS[..500];
let projects_files = projects.iter().filter_map(|project| {
if let Some(false) = project.allow_mod_distribution {
None
} else {
Some((project.id, project.main_file_id))
}
});
for (project, file) in projects_files {
let result = CLIENT.project_file_download_url(project, file).await;
match result {
Ok(_download) => (),
Err(error) => panic!("{}", error),
}
}
});
}