add_icon_with_headers

This commit is contained in:
Sam Denty 2022-10-01 21:57:23 +01:00
parent f2efe51c22
commit 0bead553c2
No known key found for this signature in database
GPG key ID: 7B4EAF7B9E291B79
4 changed files with 17 additions and 4 deletions

2
Cargo.lock generated
View file

@ -1513,7 +1513,7 @@ checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de"
[[package]] [[package]]
name = "site_icons" name = "site_icons"
version = "0.1.13" version = "0.2.0"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"clap", "clap",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "site_icons" name = "site_icons"
version = "0.1.13" version = "0.2.0"
authors = ["Sam Denty <sam@samdenty.com>"] authors = ["Sam Denty <sam@samdenty.com>"]
edition = "2018" edition = "2018"
license = "GPL-3.0" license = "GPL-3.0"

View file

@ -32,6 +32,7 @@ pub enum IconInfo {
impl IconInfo { impl IconInfo {
pub async fn load( pub async fn load(
url: Url, url: Url,
headers: HeaderMap,
sizes: Option<String>, sizes: Option<String>,
) -> Result<IconInfo, Box<dyn Error>> { ) -> Result<IconInfo, Box<dyn Error>> {
let sizes = sizes.as_ref().and_then(|s| IconSizes::from_str(s).ok()); let sizes = sizes.as_ref().and_then(|s| IconSizes::from_str(s).ok());
@ -54,7 +55,7 @@ impl IconInfo {
} }
_ => { _ => {
let res = CLIENT.get(url).send().await?; let res = CLIENT.get(url).headers(headers).send().await?;
if !res.status().is_success() { if !res.status().is_success() {
return Err("failed to fetch".into()); return Err("failed to fetch".into());
}; };

View file

@ -46,6 +46,18 @@ impl Icons {
/// Add an icon URL and start fetching it /// Add an icon URL and start fetching it
pub fn add_icon(&mut self, url: Url, kind: IconKind, sizes: Option<String>) { pub fn add_icon(&mut self, url: Url, kind: IconKind, sizes: Option<String>) {
self.add_icon_with_headers(url, HeaderMap::new(), kind, sizes)
}
/// Add an icon URL and start fetching it,
/// along with the specified headers
pub fn add_icon_with_headers(
&mut self,
url: Url,
headers: HeaderMap,
kind: IconKind,
sizes: Option<String>,
) {
// check to see if it already exists // check to see if it already exists
let mut entries = self.entries.iter_mut(); let mut entries = self.entries.iter_mut();
if let Some(existing_kind) = self if let Some(existing_kind) = self
@ -69,7 +81,7 @@ impl Icons {
return; return;
} }
let mut info = Box::pin(IconInfo::load(url.clone(), sizes)); let mut info = Box::pin(IconInfo::load(url.clone(), headers, sizes));
// Start fetching the icon // Start fetching the icon
let noop_waker = noop_waker(); let noop_waker = noop_waker();