Skip to content

Commit 5c04d70

Browse files
committed
Add a fetch helper function
1 parent 3b18d85 commit 5c04d70

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod redirect;
1717
mod render;
1818
mod rust_version;
1919
mod teams;
20+
mod utils;
2021

2122
const ZULIP_DOMAIN: &str = "https://rust-lang.zulipchat.com";
2223

src/rust_version.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::utils::fetch;
2+
13
static MANIFEST_URL: &str = "https://static.rust-lang.org/dist/channel-rust-stable.toml";
24

35
#[derive(Debug, Clone)]
@@ -6,16 +8,10 @@ pub struct RustVersion(pub String);
68
/// Fetch the latest stable version of Rust.
79
pub fn fetch_rust_version() -> anyhow::Result<RustVersion> {
810
println!("Downloading Rust version");
9-
let mut response = ureq::get(MANIFEST_URL).call()?;
10-
if !response.status().is_success() {
11-
return Err(anyhow::anyhow!(
12-
"Failed to download Rust version (HTTP status {}): {}",
13-
response.status(),
14-
response.body_mut().read_to_string()?
15-
));
16-
}
1711

18-
let manifest: toml::Value = toml::from_str(&response.body_mut().read_to_string()?)?;
12+
let response = fetch(MANIFEST_URL)?;
13+
14+
let manifest: toml::Value = toml::from_str(&response)?;
1915
let rust_version = manifest["pkg"]["rust"]["version"].as_str().unwrap();
2016
let version: String = rust_version.split(' ').next().unwrap().to_owned();
2117
Ok(RustVersion(version))

src/teams.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::utils::fetch;
12
use handlebars::{
23
Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderErrorReason,
34
};
@@ -217,18 +218,10 @@ pub struct PageData {
217218
pub fn load_rust_teams() -> anyhow::Result<RustTeams> {
218219
println!("Downloading Team API data");
219220

220-
let mut response = ureq::get(format!("{BASE_URL}/teams.json")).call()?;
221-
if !response.status().is_success() {
222-
return Err(anyhow::anyhow!(
223-
"Failed to download team API (HTTP status {}): {}",
224-
response.status(),
225-
response.body_mut().read_to_string()?
226-
));
227-
}
228-
229-
let resp: Teams = response.body_mut().read_json()?;
221+
let response = fetch(&format!("{BASE_URL}/teams.json"))?;
222+
let response: Teams = serde_json::from_str(&response)?;
230223

231-
Ok(RustTeams(resp.teams.into_values().collect()))
224+
Ok(RustTeams(response.teams.into_values().collect()))
232225
}
233226

234227
pub(crate) fn kind_to_str(kind: TeamKind) -> &'static str {

src/utils.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// Send a GET request to the given `url` and return the result as a string
2+
pub fn fetch(url: &str) -> anyhow::Result<String> {
3+
let mut response = ureq::get(url).call()?;
4+
if !response.status().is_success() {
5+
return Err(anyhow::anyhow!(
6+
"Failed to download data from `{url}` (HTTP status {}): {}",
7+
response.status(),
8+
response.body_mut().read_to_string()?
9+
));
10+
}
11+
12+
Ok(response.body_mut().read_to_string()?)
13+
}

0 commit comments

Comments
 (0)