1use crate::{Client, error::Result, models::*};
2use std::collections::HashMap;
3
4impl Client {
5 pub async fn get_project(&self, id_or_slug: &str) -> Result<Project> {
7 let url = format!("{}/project/{}", self.base_url, id_or_slug);
8 let resp = self.http.get(&url).send().await?.error_for_status()?;
9 let project = resp.json::<Project>().await?;
10 Ok(project)
11 }
12
13 pub async fn get_projects(&self, ids: &[&str]) -> Result<Vec<Project>> {
14 let url = format!("{}/projects", self.base_url);
15 let ids_str = serde_json::to_string(ids)?;
16 let url_with_query = format!("{}?ids={}", url, ids_str);
17 let resp = self.http.get(&url_with_query).send().await?.error_for_status()?;
18 let res = resp.json::<Vec<Project>>().await?;
19 Ok(res)
20 }
21
22 pub async fn get_project_dependencies(&self, id_or_slug: &str) -> Result<crate::models::Dependencies> {
23 let url = format!("{}/project/{}/dependencies", self.base_url, id_or_slug);
24 let resp = self.http.get(&url).send().await?.error_for_status()?;
25 let res = resp.json::<crate::models::Dependencies>().await?;
26 Ok(res)
27 }
28
29 pub async fn search_projects(&self, query: &str) -> Result<SearchResult> {
30 let url = format!("{}/search?query={}", self.base_url, query);
31 let resp = self.http.get(&url).send().await?.error_for_status()?;
32 let res = resp.json::<SearchResult>().await?;
33 Ok(res)
34 }
35
36 pub async fn get_version(&self, project_id: &str, version_id_or_number: &str) -> Result<Version> {
38 let url = format!("{}/project/{}/version/{}", self.base_url, project_id, version_id_or_number);
39 let resp = self.http.get(&url).send().await?.error_for_status()?;
40 let res = resp.json::<Version>().await?;
41 Ok(res)
42 }
43
44 pub async fn get_latest_version_from_hash(&self, hash: &str, loaders: &[&str], game_versions: &[&str], algorithm: Option<&str>) -> Result<Version> {
45 let algo = algorithm.unwrap_or("sha1");
46 let url = format!("{}/version_file/{}/update?algorithm={}", self.base_url, hash, algo);
47 let body = serde_json::json!({
48 "loaders": loaders,
49 "game_versions": game_versions
50 });
51 let resp = self.http.post(&url).json(&body).send().await?.error_for_status()?;
52 let res = resp.json::<Version>().await?;
53 Ok(res)
54 }
55
56 pub async fn get_latest_versions_from_hashes(&self, hashes: &[&str], loaders: &[&str], game_versions: &[&str], algorithm: Option<&str>) -> Result<HashMap<String, Version>> {
57 let algo = algorithm.unwrap_or("sha1");
58 let url = format!("{}/version_files/update?algorithm={}", self.base_url, algo);
59 let body = serde_json::json!({
60 "hashes": hashes,
61 "loaders": loaders,
62 "game_versions": game_versions
63 });
64 let resp = self.http.post(&url).json(&body).send().await?.error_for_status()?;
65 let res = resp.json::<HashMap<String, Version>>().await?;
66 Ok(res)
67 }
68
69 pub async fn list_versions(&self, project_id: &str) -> Result<Vec<Version>> {
70 let url = format!("{}/project/{}/version", self.base_url, project_id);
71 let resp = self.http.get(&url).send().await?.error_for_status()?;
72 let res = resp.json::<Vec<Version>>().await?;
73 Ok(res)
74 }
75
76 pub async fn get_version_from_hash(&self, hash: &str) -> Result<Version> {
78 let url = format!("{}/version_file/{}", self.base_url, hash);
79 let resp = self.http.get(&url).send().await?.error_for_status()?;
80 let res = resp.json::<Version>().await?;
81 Ok(res)
82 }
83
84 pub async fn get_versions_from_hashes(&self, hashes: Vec<String>) -> Result<HashMap<String, Version>> {
85 let url = format!("{}/version_files", self.base_url);
86 let body = serde_json::json!({ "hashes": hashes });
87 let resp = self.http.post(&url).json(&body).send().await?.error_for_status()?;
88 let res = resp.json::<HashMap<String, Version>>().await?;
89 Ok(res)
90 }
91}