fetch.rs 805 B

123456789101112131415161718192021222324
  1. use anyhow::{Context, Result};
  2. use url::Url;
  3. /// Convert relate to absolute
  4. pub fn relative_to_absolute(base_url: &str, relative_href: &str) -> Result<String> {
  5. let base_url = Url::parse(base_url).context(format!("Url::parse({})", base_url))?;
  6. let new_url = base_url
  7. .join(relative_href)
  8. .context(format!("join({})", relative_href))?;
  9. Ok(new_url.to_string())
  10. }
  11. // Should this always fetch it/save?
  12. pub fn fetch(client: &reqwest::blocking::Client, url: &str) -> Result<String> {
  13. let res = client.get(url).send()?;
  14. let buffer = res.text()?;
  15. Ok(buffer)
  16. }
  17. /// Extract filename from the end of a URL.
  18. pub fn filename_from_url(url: &str) -> Result<String> {
  19. let (_, filename) = url.rsplit_once('/').context("Failed to split URL.")?;
  20. Ok(filename.to_string())
  21. }