alterware-launcher/src/http.rs

37 lines
1.0 KiB
Rust
Raw Normal View History

2023-06-10 03:33:16 -04:00
use std::{fs, io::Write, path::Path, str};
pub fn get_body(url: &str) -> Vec<u8> {
let mut res: Vec<u8> = Vec::new();
2023-08-02 20:49:55 -04:00
let req = http_req::request::Request::new(&url.try_into().unwrap())
.header(
"User-Agent",
"AlterWare Launcher | github.com/mxve/alterware-launcher",
)
.send(&mut res)
.unwrap_or_else(|error| {
panic!("\n\n{}:\n{:?}", "Error", error);
});
if req.status_code() == http_req::response::StatusCode::new(302) {
let location = req.headers().get("Location").unwrap().as_str();
return get_body(location);
}
2023-06-10 03:33:16 -04:00
res
}
2023-06-10 06:38:21 -04:00
pub fn get_body_string(url: &str) -> String {
2023-06-10 03:33:16 -04:00
String::from_utf8(get_body(url)).unwrap()
}
pub fn download_file(url: &str, file_path: &Path) {
let body = get_body(url);
let mut f = fs::File::create(file_path).unwrap_or_else(|error| {
panic!("\n\n{}:\n{:?}", "Error", error);
});
f.write_all(&body).unwrap_or_else(|error| {
panic!("\n\n{}:\n{:?}", "Error", error);
});
}