diff options
Diffstat (limited to 'src/apis')
-rw-r--r-- | src/apis/modrinth.rs | 101 |
1 files changed, 76 insertions, 25 deletions
diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index ce9fdd4..3af5bbd 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs | |||
@@ -1,25 +1,6 @@ | |||
1 | use serde::{Deserialize, Serialize}; | 1 | use serde::Deserialize; |
2 | 2 | ||
3 | async fn get(path: String) -> Result<Vec<u8>, Box<dyn std::error::Error>> { | 3 | use crate::Modloader; |
4 | dbg!(&path); | ||
5 | let api = String::from("https://api.modrinth.com/v2/"); | ||
6 | //let api = String::from("localhost:8080/"); | ||
7 | //let api = String::from("https://www.rust-lang.org/"); | ||
8 | let url = format!(r#"{}{}"#, api, path); | ||
9 | |||
10 | println!("{}", &url); | ||
11 | |||
12 | |||
13 | let data = reqwest::get(r#"https://api.modrinth.com/v2/projects?ids=["kYuIpRLv","89Wsn8GD"]"#) | ||
14 | .await? | ||
15 | .bytes() | ||
16 | .await? | ||
17 | .to_vec(); | ||
18 | |||
19 | //println!("body = {:?}", data); | ||
20 | |||
21 | Ok(data) | ||
22 | } | ||
23 | 4 | ||
24 | #[derive(Debug, Deserialize)] | 5 | #[derive(Debug, Deserialize)] |
25 | pub struct Project { | 6 | pub struct Project { |
@@ -86,19 +67,89 @@ pub enum Status { | |||
86 | processing, | 67 | processing, |
87 | unknown | 68 | unknown |
88 | } | 69 | } |
89 | pub async fn project(name: &str) -> Project { | 70 | |
71 | #[derive(Debug, Deserialize)] | ||
72 | pub struct Version { | ||
73 | pub name: String, | ||
74 | pub version_number: String, | ||
75 | pub changelog: Option<String>, | ||
76 | pub game_versions: Vec<String>, | ||
77 | pub version_type: VersionType, | ||
78 | pub loaders: Vec<String>, | ||
79 | pub featured: bool, | ||
80 | pub id: String, | ||
81 | pub project_id: String, | ||
82 | pub author_id: String, | ||
83 | pub date_published: String, | ||
84 | pub downloads: u32, | ||
85 | pub files: Vec<VersionFile>, | ||
86 | } | ||
87 | |||
88 | #[allow(non_camel_case_types)] | ||
89 | #[derive(Debug, Deserialize)] | ||
90 | pub enum VersionType { | ||
91 | release, | ||
92 | beta, | ||
93 | alpha | ||
94 | } | ||
95 | |||
96 | #[derive(Debug, Deserialize)] | ||
97 | pub struct VersionFile { | ||
98 | pub hashes: Hash, | ||
99 | pub url: String, | ||
100 | pub filename: String, | ||
101 | pub primary: bool, | ||
102 | pub size: u32, | ||
103 | } | ||
104 | |||
105 | #[derive(Debug, Deserialize)] | ||
106 | pub struct Hash { | ||
107 | pub sha512: String, | ||
108 | pub sha1: String, | ||
109 | } | ||
110 | |||
111 | async fn get(api: String, path: String) -> Result<Vec<u8>, Box<dyn std::error::Error>> { | ||
112 | let url = format!(r#"{}{}"#, api, path); | ||
113 | |||
114 | dbg!(&url); | ||
115 | |||
116 | let data = reqwest::get(url) | ||
117 | .await? | ||
118 | .bytes() | ||
119 | .await? | ||
120 | .to_vec(); | ||
121 | |||
122 | Ok(data) | ||
123 | } | ||
124 | |||
125 | |||
126 | pub async fn project(api: String, name: &str) -> Project { | ||
90 | let url = format!("project/{}", name); | 127 | let url = format!("project/{}", name); |
91 | let data = get(url); | 128 | let data = get(api, url); |
92 | 129 | ||
93 | serde_json::from_slice(&data.await.unwrap()).unwrap() | 130 | serde_json::from_slice(&data.await.unwrap()).unwrap() |
94 | } | 131 | } |
95 | 132 | ||
96 | pub async fn projects(ids: Vec<&str>) -> Vec<Project> { | 133 | pub async fn projects(api: String, ids: Vec<&str>) -> Vec<Project> { |
97 | let all = ids.join(r#"",""#); | 134 | let all = ids.join(r#"",""#); |
98 | let url = format!(r#"projects?ids=["{}"]"#, all); | 135 | let url = format!(r#"projects?ids=["{}"]"#, all); |
99 | println!("{}", url); | 136 | println!("{}", url); |
100 | 137 | ||
101 | let data = get(url); | 138 | let data = get(api, url); |
102 | 139 | ||
103 | serde_json::from_slice(&data.await.unwrap()).unwrap() | 140 | serde_json::from_slice(&data.await.unwrap()).unwrap() |
104 | } | 141 | } |
142 | |||
143 | pub async fn versions(api: String, id: String, loader: Modloader, mc_version: String) -> Vec<Version> { | ||
144 | |||
145 | let loaderstr = match loader { | ||
146 | Modloader::Forge => String::from("forge"), | ||
147 | Modloader::Fabric => String::from("fabric"), | ||
148 | }; | ||
149 | |||
150 | let url = format!(r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, id, loaderstr, mc_version); | ||
151 | |||
152 | let data = get(api, url); | ||
153 | |||
154 | serde_json::from_slice(&data.await.unwrap()).unwrap() | ||
155 | } | ||