diff options
author | fxqnlr <[email protected]> | 2022-10-31 13:01:06 +0100 |
---|---|---|
committer | fxqnlr <[email protected]> | 2022-10-31 13:01:06 +0100 |
commit | 3320da719669f37dd5f55693b4d76edb27dbce02 (patch) | |
tree | 4383af17a4362a19546a4704d5450884d96bc504 /src/apis/modrinth.rs | |
download | modlist-3320da719669f37dd5f55693b4d76edb27dbce02.tar modlist-3320da719669f37dd5f55693b4d76edb27dbce02.tar.gz modlist-3320da719669f37dd5f55693b4d76edb27dbce02.zip |
modlist
Diffstat (limited to 'src/apis/modrinth.rs')
-rw-r--r-- | src/apis/modrinth.rs | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs new file mode 100644 index 0000000..ce9fdd4 --- /dev/null +++ b/src/apis/modrinth.rs | |||
@@ -0,0 +1,104 @@ | |||
1 | use serde::{Deserialize, Serialize}; | ||
2 | |||
3 | async fn get(path: String) -> Result<Vec<u8>, Box<dyn std::error::Error>> { | ||
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 | |||
24 | #[derive(Debug, Deserialize)] | ||
25 | pub struct Project { | ||
26 | pub slug: String, | ||
27 | pub title: String, | ||
28 | pub description: String, | ||
29 | pub categories: Vec<String>, | ||
30 | pub client_side: Side, | ||
31 | pub server_side: Side, | ||
32 | pub body: String, | ||
33 | pub additional_categories: Option<Vec<String>>, | ||
34 | pub project_type: Type, | ||
35 | pub downloads: u32, | ||
36 | pub icon_url: Option<String>, | ||
37 | pub id: String, | ||
38 | pub team: String, | ||
39 | pub moderator_message: Option<ModeratorMessage>, | ||
40 | pub published: String, | ||
41 | pub updated: String, | ||
42 | pub approved: Option<String>, | ||
43 | pub followers: u32, | ||
44 | pub status: Status, | ||
45 | pub license: License, | ||
46 | pub versions: Vec<String>, | ||
47 | } | ||
48 | |||
49 | #[derive(Debug, Deserialize)] | ||
50 | pub struct License { | ||
51 | pub id: String, | ||
52 | pub name: String, | ||
53 | pub url: Option<String>, | ||
54 | } | ||
55 | |||
56 | #[derive(Debug, Deserialize)] | ||
57 | pub struct ModeratorMessage { | ||
58 | pub message: String, | ||
59 | pub body: Option<String>, | ||
60 | } | ||
61 | |||
62 | #[allow(non_camel_case_types)] | ||
63 | #[derive(Debug, Deserialize)] | ||
64 | pub enum Side { | ||
65 | required, | ||
66 | optional, | ||
67 | unsupported | ||
68 | } | ||
69 | |||
70 | #[allow(non_camel_case_types)] | ||
71 | #[derive(Debug, Deserialize)] | ||
72 | pub enum Type { | ||
73 | r#mod, | ||
74 | modpack, | ||
75 | recourcepack | ||
76 | } | ||
77 | |||
78 | #[allow(non_camel_case_types)] | ||
79 | #[derive(Debug, Deserialize)] | ||
80 | pub enum Status { | ||
81 | approved, | ||
82 | rejected, | ||
83 | draft, | ||
84 | unlisted, | ||
85 | archived, | ||
86 | processing, | ||
87 | unknown | ||
88 | } | ||
89 | pub async fn project(name: &str) -> Project { | ||
90 | let url = format!("project/{}", name); | ||
91 | let data = get(url); | ||
92 | |||
93 | serde_json::from_slice(&data.await.unwrap()).unwrap() | ||
94 | } | ||
95 | |||
96 | pub async fn projects(ids: Vec<&str>) -> Vec<Project> { | ||
97 | let all = ids.join(r#"",""#); | ||
98 | let url = format!(r#"projects?ids=["{}"]"#, all); | ||
99 | println!("{}", url); | ||
100 | |||
101 | let data = get(url); | ||
102 | |||
103 | serde_json::from_slice(&data.await.unwrap()).unwrap() | ||
104 | } | ||