From d2325fe31f63a3965f184894b38fc3829e2c68a6 Mon Sep 17 00:00:00 2001 From: Spookdot <333.333.333@gmx.de> Date: Sat, 20 Aug 2022 00:17:55 +0200 Subject: [PATCH 1/2] Update URLs and Structs for Modrinth API v2 --- src/api.rs | 85 ++++++++++++++++++++++++++++++++--------------------- src/main.rs | 24 ++++++++++----- 2 files changed, 67 insertions(+), 42 deletions(-) diff --git a/src/api.rs b/src/api.rs index bd38f53..93ff4d5 100644 --- a/src/api.rs +++ b/src/api.rs @@ -12,24 +12,25 @@ pub struct SearchResponse { #[derive(Deserialize, Debug)] pub struct ModResult { - pub mod_id: String, // TODO parse to `local-xxxxx` with regex - pub project_type: Option, // NOTE this isn't in all search results? - pub author: String, + pub slug: String, pub title: String, pub description: String, pub categories: Vec, - pub versions: Vec, + pub display_categories: Vec, // NOTE this is not in the OpenAPI docs + pub client_side: String, + pub server_side: String, + pub project_type: String, // NOTE this isn't in all search results? pub downloads: isize, - pub page_url: String, pub icon_url: String, - pub author_url: String, + pub project_id: String, // TODO parse to 'local-xxxx' with reegex + pub author: String, + pub versions: Vec, + pub follows: isize, pub date_created: String, pub date_modified: String, pub latest_version: String, pub license: String, - pub client_side: String, - pub server_side: String, - pub host: String, + pub gallery: Vec, } impl ModResult { @@ -59,28 +60,43 @@ impl ModResult { #[derive(Deserialize, Debug)] pub struct ModInfo { - pub id: String, // TODO serialize mod id? pub slug: String, - pub team: String, // TODO serialize team id? pub title: String, pub description: String, + pub categories: Vec, + pub additional_categories: Vec, // NOTE not listed in OpenAPI docs + pub client_side: String, // TODO serialize as enum + pub server_side: String, // TODO serialize as enum pub body: String, - pub published: String, // TODO serialize datetime - pub updated: String, // TODO serialize datetime + pub issues_url: String, + pub source_url: String, + pub wiki_url: Option, + pub discord_url: String, + pub donation_urls: Vec, + pub project_type: String, + pub downloads: isize, + pub icon_url: String, + pub id: String, // TODO serialize mod id? + pub team: String, // TODO serialize team id? + pub body_url: Option, // NOTE deprecated + pub moderator_message: Option, + pub published: String, // TODO serialize as datetime + pub updated: String, // TODO serialize as datetime + pub approved: String, // NOTE not listed in OpenAPI docs, TODO serialize as datetime + pub followers: isize, pub status: String, pub license: License, - pub client_side: String, // TODO serialize as enum - pub server_side: String, // TODO serialize as enum - pub downloads: isize, - pub followers: isize, - pub categories: Vec, pub versions: Vec, - pub icon_url: Option, - pub issues_url: Option, - pub source_url: Option, - pub wiki_url: Option, - pub discord_url: Option, - pub donation_urls: Vec, + pub gallery: Vec, +} + +#[derive(Deserialize, Debug)] +pub struct GalleryEntry { + pub url: String, + pub featured: bool, + pub title: String, + pub description: String, + pub created: String, } #[derive(Deserialize, Debug)] @@ -99,22 +115,21 @@ pub struct DonationLink { #[derive(Deserialize, Debug)] pub struct ModVersion { - pub id: String, // version id - pub mod_id: String, // mod id - pub author_id: String, // user id - // NOTE modrinth docs list this as a String, but is actually a bool? - // featured: String, // user id pub name: String, pub version_number: String, pub changelog: Option, - pub changelog_url: Option, - pub date_published: String, // TODO serialize datetime - pub downloads: isize, - pub version_type: String, // TODO {alpha | beta | release} - pub files: Vec, // pub dependencies: Vec, // TODO dependency wrangling, thank you modrinth, very cool pub game_versions: Vec, + pub version_type: String, // TODO {alpha | beta | release} pub loaders: Vec, + pub featured: bool, + pub id: String, // version id + pub project_id: String, // mod id + pub author_id: String, // user id + pub date_published: String, // TODO serialize datetime + pub downloads: isize, + pub changelog_url: Option, // NOTE deprecated + pub files: Vec, } #[derive(Deserialize, Debug)] @@ -122,4 +137,6 @@ pub struct ModVersionFile { pub hashes: HashMap, pub url: String, pub filename: String, + pub primary: bool, + pub size: isize, } diff --git a/src/main.rs b/src/main.rs index 44fb4a2..e4ae0c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,10 +14,16 @@ async fn search_mods(ctx: &AppContext, search_args: &SearchArgs) -> anyhow::Resu println!("Searching with query \"{}\"...", search_args.package_name); let client = reqwest::Client::new(); - let url = format!("https://{}/api/v1/mod", ctx.config.upstream.server_address); + let url = format!("https://{}/v2/search", ctx.config.upstream.server_address); - let mut params = vec![("query", search_args.package_name.to_owned())]; if let Some(versions) = &search_args.version { - params.push(("versions", versions.join(","))); + let mut params = vec![("query", search_args.package_name.to_owned())]; + if let Some(versions) = &search_args.version { + let versions_facets = versions + .iter() + .map(|e| format!("[\"versions:{}\"]", e)) + .collect::>() + .join(","); + params.push(("facets", format!("[{}]", versions_facets))); } let url = reqwest::Url::parse_with_params(url.as_str(), ¶ms)?; @@ -96,13 +102,15 @@ async fn select_from_results( } async fn fetch_mod_info(ctx: &AppContext, mod_result: &ModResult) -> anyhow::Result { - let mod_id = &mod_result.mod_id; - println!("Fetching mod info for {} (ID: {})...", mod_result.title, mod_id); + let mod_id = &mod_result.project_id; + println!( + "Fetching mod info for {} (ID: {})...", + mod_result.title, mod_id + ); let client = reqwest::Client::new(); - let mod_id = mod_id[6..].to_owned(); // Remove "local-" prefix let url = format!( - "https://{}/api/v1/mod/{}", + "https://{}/v2/project/{}", ctx.config.upstream.server_address, mod_id ); info!("GET {}", url); @@ -116,7 +124,7 @@ async fn fetch_mod_version(ctx: &AppContext, version_id: &String) -> anyhow::Res let client = reqwest::Client::new(); let url = format!( - "https://{}/api/v1/version/{}", + "https://{}/v2/version/{}", ctx.config.upstream.server_address, version_id ); info!("GET {}", url); From 7d3a1c4a66e7c4696526238e7e7b6d4a45ecae62 Mon Sep 17 00:00:00 2001 From: Spookdot <333.333.333@gmx.de> Date: Sat, 20 Aug 2022 00:23:24 +0200 Subject: [PATCH 2/2] Fix Type on nullable types --- src/api.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/api.rs b/src/api.rs index 93ff4d5..346ebe9 100644 --- a/src/api.rs +++ b/src/api.rs @@ -68,26 +68,26 @@ pub struct ModInfo { pub client_side: String, // TODO serialize as enum pub server_side: String, // TODO serialize as enum pub body: String, - pub issues_url: String, - pub source_url: String, + pub issues_url: Option, + pub source_url: Option, pub wiki_url: Option, - pub discord_url: String, - pub donation_urls: Vec, + pub discord_url: Option, + pub donation_urls: Option>, pub project_type: String, pub downloads: isize, - pub icon_url: String, + pub icon_url: Option, pub id: String, // TODO serialize mod id? pub team: String, // TODO serialize team id? pub body_url: Option, // NOTE deprecated pub moderator_message: Option, - pub published: String, // TODO serialize as datetime - pub updated: String, // TODO serialize as datetime - pub approved: String, // NOTE not listed in OpenAPI docs, TODO serialize as datetime + pub published: String, // TODO serialize as datetime + pub updated: String, // TODO serialize as datetime + pub approved: Option, // NOTE not listed in OpenAPI docs, TODO serialize as datetime pub followers: isize, pub status: String, pub license: License, pub versions: Vec, - pub gallery: Vec, + pub gallery: Option>, } #[derive(Deserialize, Debug)] @@ -118,7 +118,7 @@ pub struct ModVersion { pub name: String, pub version_number: String, pub changelog: Option, - // pub dependencies: Vec, // TODO dependency wrangling, thank you modrinth, very cool + // pub dependencies: Option>, // TODO dependency wrangling, thank you modrinth, very cool pub game_versions: Vec, pub version_type: String, // TODO {alpha | beta | release} pub loaders: Vec,