Skip to content

Instantly share code, notes, and snippets.

@jayhuang75
Created October 24, 2020 01:07
Show Gist options
  • Save jayhuang75/064dc568e2378f9f6ac5b5dd4b330131 to your computer and use it in GitHub Desktop.
Save jayhuang75/064dc568e2378f9f6ac5b5dd4b330131 to your computer and use it in GitHub Desktop.
SMS Alert Rust
pub async fn fetch(url: &str, dir_name: &str, file_name: &str) -> Result<String, Box<dyn Error>> {
// application start time
let start = Instant::now();
// create folder if not exist
fs::create_dir_all(dir_name)?;
// download the file
let resp = reqwest::get(url).await?;
// paser to context txt
let content = resp.text().await?;
// prepare the output dest
let mut out = File::create(format!("{}/{}", dir_name, file_name)).await?;
// ouput
io::copy(&mut content.as_bytes(), &mut out).await?;
// mesurement the elapsed time
let duration = start.elapsed();
info!("[download] total download time: {:?}", duration);
Ok(format!("{}/{}", dir_name, file_name))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment