femtorinth/api_functions/
mods.rs

1use crate::data_structures::{Mod, ModID, ModSearchResults, SearchSorting, API_PREFIX};
2
3/// This API call searches Modrinth for your query with 2 additional optional parameters and
4/// returns a `ModSearchResults` struct.
5///
6/// This function takes a `query`, an `Option<SearchSorting>` for how to sort the searches (by
7/// default its sorted by relevance) and a `Option<usize>` as the limit which indicates how
8/// many results to return at max.
9pub fn search_mods(
10    query: String,
11    sorting: Option<SearchSorting>,
12    limit: Option<usize>,
13) -> Result<ModSearchResults, crate::Error> {
14    let mut request = format!("{}/api/v1/mod?query={}", API_PREFIX, query);
15    let sort: &'static str;
16
17    if let Some(sorter) = sorting {
18        sort = match sorter {
19            SearchSorting::Relevance => "relevance",
20            SearchSorting::Downloads => "downloads",
21            SearchSorting::Updated => "updated",
22            SearchSorting::Newest => "newest",
23        };
24    } else {
25        sort = "relevance";
26    }
27    request = format!("{}&index={}", request, sort);
28
29    if let Some(limit) = limit {
30        request = format!("{}&limit={}", request, limit);
31    }
32
33    Ok(ureq::get(request.as_str()).call()?.into_json()?)
34}
35
36/// This API call gets the full details of a mod using its Mod ID, have a look at the `ModID`
37/// struct.
38pub fn mod_get(mod_id: ModID) -> Result<Mod, crate::Error> {
39    let request = format!("{}/api/v1/mod/{}", API_PREFIX, mod_id.0);
40    Ok(ureq::get(request.as_str()).call()?.into_json()?)
41}