1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use super::*;
use crate::structures::version::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
impl Ferinth<Authenticated> {
/**
Delete the version file with the `hash`.
Only supports SHA1 hashes for now.
Optionally specify the version ID to delete the version file from, if multiple files of the same hash exist.
```no_run
# tokio_test::block_on(async {
# let modrinth = ferinth::Ferinth::<ferinth::Authenticated>::new(
# env!("CARGO_CRATE_NAME"),
# Some(env!("CARGO_PKG_VERSION")),
# None,
# env!("MODRINTH_TOKEN"),
# )?;
modrinth.version_file_delete_from_hash("795d4c12bffdb1b21eed5ff87c07ce5ca3c0dcbf", None).await?;
# Ok::<_, ferinth::Error>(()) }).unwrap()
```
*/
pub async fn version_file_delete_from_hash(
&self,
hash: &str,
version_id: Option<&str>,
) -> Result<()> {
check_sha1_hash(&[hash])?;
let mut url = API_BASE_URL.join_all(vec!["version_file", hash]);
if let Some(version_id) = version_id {
check_id_slug(&[version_id])?;
url = url.with_query("version_id", version_id);
}
self.client.delete(url).custom_send().await?;
Ok(())
}
}
impl<T> Ferinth<T> {
/**
Get the version of the version file with `hash`.
Only supports SHA1 hashes for now.
## Example
```rust
# tokio_test::block_on(async {
# let modrinth = ferinth::Ferinth::default();
// If a mod file has the hash `795d4c12bffdb1b21eed5ff87c07ce5ca3c0dcbf`, we can get the version it belongs to
let sodium_version = modrinth.version_get_from_hash("795d4c12bffdb1b21eed5ff87c07ce5ca3c0dcbf").await?;
assert_eq!(sodium_version.project_id, "AANobbMI");
# Ok::<_, ferinth::Error>(()) }).unwrap()
```
*/
pub async fn version_get_from_hash(&self, hash: &str) -> Result<Version> {
check_sha1_hash(&[hash])?;
self.client
.get(API_BASE_URL.join_all(vec!["version_file", hash]))
.custom_send_json()
.await
}
/**
Get the versions of the version files with `hashes`, only supports SHA1 hashes for now
Returns a map where the keys are the hashes given.
## Example
```rust
# tokio_test::block_on(async {
# let modrinth = ferinth::Ferinth::default();
let sodium_hash = "795d4c12bffdb1b21eed5ff87c07ce5ca3c0dcbf";
let snwylvspls_hash = "994ee99d172a5950a51ec2d08c158d270722d871";
let versions = modrinth.version_get_from_multiple_hashes(vec![
sodium_hash.into(),
snwylvspls_hash.into(),
]).await?;
assert_eq!(versions[sodium_hash].project_id, "AANobbMI");
assert_eq!(versions[snwylvspls_hash].project_id, "of7wIinq");
# Ok::<_, ferinth::Error>(()) }).unwrap()
```
*/
pub async fn version_get_from_multiple_hashes(
&self,
hashes: Vec<String>,
) -> Result<HashMap<String, Version>> {
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct HashesBody {
pub hashes: Vec<String>,
pub algorithm: HashAlgorithm,
}
check_sha1_hash(&hashes)?;
self.client
.post(API_BASE_URL.join_all(vec!["version_files"]))
.json(&HashesBody {
hashes,
algorithm: HashAlgorithm::SHA1,
})
.custom_send_json()
.await
}
/// Get the latest version for the project of the version file with `hash` based on some `filters`.
/// Only supports SHA1 hashes for now.
pub async fn version_get_latest_from_hash(
&self,
hash: &str,
filters: &LatestVersionBody,
) -> Result<Version> {
check_sha1_hash(&[hash])?;
self.client
.post(
API_BASE_URL
.join_all(vec!["version_file", hash, "update"])
.with_query_json("algorithm", HashAlgorithm::SHA1)?,
)
.json(filters)
.custom_send_json()
.await
}
/// Get the latest versions of the projects of the version files with hashes based on some `filters`.
/// Only supports SHA1 hashes for now.
pub async fn version_get_latest_from_multiple_hashes(
&self,
hashes: Vec<String>,
filters: LatestVersionBody,
) -> Result<HashMap<String, Version>> {
check_sha1_hash(&hashes)?;
self.client
.post(API_BASE_URL.join_all(vec!["version_files", "update"]))
.json(&LatestVersionsBody {
hashes,
algorithm: HashAlgorithm::SHA1,
loaders: filters.loaders,
game_versions: filters.game_versions,
})
.custom_send_json()
.await
}
}