use crate::client::Spider;
use crate::ops::{curated_surface, first_page, Call};
use crate::response::{Outcome, Page, Pages};
use crate::transport::route;
use crate::Result;
pub struct Fetch<'a> {
call: Call<'a>,
domain: String,
path: String,
}
impl std::fmt::Debug for Fetch<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Fetch")
.field("call", &self.call)
.field("domain", &"<redacted>")
.field("path", &"<redacted>")
.finish_non_exhaustive()
}
}
impl<'a> Fetch<'a> {
pub(crate) fn new(
spider: &'a Spider,
domain: impl Into<String>,
path: impl Into<String>,
) -> Fetch<'a> {
let domain = domain.into();
let path = path.into();
let rendered = path.trim_start_matches('/').to_string();
let mut call = Call::new(spider, format!("https://{domain}/{rendered}").as_str());
call.params.url = None;
Fetch {
call,
domain,
path: rendered,
}
}
pub async fn send(self) -> Result<Outcome<Page>> {
first_page(self.send_all().await?)
}
pub async fn send_all(mut self) -> Result<Outcome<Pages>> {
let domain = self.domain.clone();
let path = self.path.clone();
self.call
.run_at(route::FETCH, &[&domain, &path], |params| params.clone())
.await
}
}
curated_surface!(Fetch, page_links);