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
use crate::{Result, models::*, prelude::*};
impl Client {
/// Fetches information about a package.
///
/// ## Example
///
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> thunderstore::Result<()> {
/// let client = thunderstore::Client::new();
///
/// let a = client.get_package(("Kesomannen", "GaleModManager")).await?;
/// let b = client.get_package("Kesomannen-GaleModManager").await?;
///
/// assert_eq!(a, b);
/// # Ok(())
/// # }
/// ```
pub async fn get_package(&self, ident: impl IntoPackageIdent<'_>) -> Result<Package> {
let url = self.url(format_args!(
"/experimental/package/{}",
ident.into_id()?.path()
));
self.get_json(url).await
}
/// Fetches information about a specific version of a package.
///
/// ## Example
///
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> thunderstore::Result<()> {
/// let client = thunderstore::Client::new();
///
/// let a = client.get_version(("Kesomannen", "GaleModManager", "0.6.0")).await?;
/// let b = client.get_version("Kesomannen-GaleModManager-0.6.0").await?;
///
/// assert_eq!(a, b);
/// # Ok(())
/// # }
/// ```
pub async fn get_version(&self, ident: impl IntoVersionIdent<'_>) -> Result<PackageVersion> {
let url = self.url(format_args!(
"/experimental/package/{}",
ident.into_id()?.path()
));
self.get_json(url).await
}
/// Fetches the readme for a specific version of a package.
/// The readme is returned as a markdown string.
pub async fn get_readme(&self, ident: impl IntoVersionIdent<'_>) -> Result<String> {
let url = self.url(format_args!(
"/experimental/package/{}/readme",
ident.into_id()?.path()
));
let response: MarkdownResponse = self.get_json(url).await?;
Ok(response.markdown)
}
/// Fetches the changelog for a specific version of a package.
/// The changelog is returned as a markdown string.
///
/// Note that a package may not have a changelog, in which case [`Error::NotFound`] is returned.
pub async fn get_changelog(&self, ident: impl IntoVersionIdent<'_>) -> Result<String> {
let url = self.url(format_args!(
"/experimental/package/{}/changelog",
ident.into_id()?.path()
));
let response: MarkdownResponse = self.get_json(url).await?;
Ok(response.markdown)
}
}
#[cfg(test)]
mod tests {
use crate::Error;
use super::*;
#[tokio::test]
async fn get_package() -> Result<()> {
let client = Client::new();
client.get_package(("Kesomannen", "GaleModManager")).await?;
client.get_package("Kesomannen-GaleModManager").await?;
Ok(())
}
#[tokio::test]
async fn get_package_fails_when_not_found() -> Result<()> {
let client = Client::new();
match client.get_package(("Kesomannen", "GaleModManager2")).await {
Err(Error::NotFound) => (),
other => panic!("expected NotFound error, got {:?}", other),
}
Ok(())
}
#[tokio::test]
async fn get_version() -> Result<()> {
let client = Client::new();
client
.get_version(("Kesomannen", "GaleModManager", "0.6.0"))
.await?;
client
.get_version("Kesomannen-GaleModManager-0.6.0")
.await?;
Ok(())
}
#[tokio::test]
async fn get_changelog() -> Result<()> {
Client::new()
.get_changelog(("Kesomannen", "GaleModManager", "0.1.0"))
.await?;
Ok(())
}
#[tokio::test]
async fn get_readme() -> Result<()> {
Client::new()
.get_readme(("Kesomannen", "GaleModManager", "0.1.0"))
.await?;
Ok(())
}
}