use ogcapi_types::common::{Collection, Link, Linked};
pub trait ProxiedLinked {
fn rewrite_links(&mut self, target_url: &str, proxy_url: &str);
}
impl ProxiedLinked for Collection {
fn rewrite_links(&mut self, target_url: &str, proxy_url: &str) {
self.links.rewrite_links(target_url, proxy_url);
}
}
impl ProxiedLinked for Vec<Link> {
fn rewrite_links(&mut self, target_url: &str, proxy_url: &str) {
let base_url = self
.get_base_url()
.map(|mut base_url| {
base_url.set_query(None);
base_url
})
.unwrap_or(target_url.parse().unwrap());
rewrite_links(self.iter_mut(), base_url.as_str(), proxy_url);
}
}
pub fn rewrite_links<'a>(
links: impl Iterator<Item = &'a mut Link>,
target_url: &str,
proxy_url: &str,
) {
links.for_each(move |link| {
if let Some(new_link) = link
.href
.strip_prefix(target_url)
.map(|path| format!("{proxy_url}{path}"))
{
link.href = new_link;
}
});
}