Skip to main content

leyline/core/request/
context.rs

1use super::RequestBuilder;
2use crate::cookie::Jar;
3use crate::core::into_url::IntoUrl;
4use crate::core::{PrefixRead, Result};
5
6impl RequestBuilder {
7    pub fn cookie_jar(mut self, jar: Jar) -> Self {
8        self.session = self.session.with_cookie_jar(jar);
9        self
10    }
11
12    pub fn initiator(mut self, page: impl IntoUrl) -> Self {
13        match page.into_url_with_base(self.session.base_url()) {
14            Ok(page) => self.initiator = Some(page),
15            Err(err) => self.fail(err),
16        }
17        self
18    }
19
20    pub fn tag(mut self, tag: impl Into<String>) -> Self {
21        self.tag = Some(tag.into());
22        self
23    }
24
25    pub fn error_for_status(mut self) -> Self {
26        self.status_errors = true;
27        self
28    }
29
30    pub async fn read_until<F>(self, limit: usize, done: F) -> Result<PrefixRead>
31    where
32        F: FnMut(&[u8], usize) -> bool,
33    {
34        self.stream().send().await?.read_prefix(limit, done).await
35    }
36
37    pub async fn download(
38        self,
39        path: impl AsRef<std::path::Path>,
40        limit: Option<u64>,
41    ) -> Result<u64> {
42        let deadline = self.session.deadline(self.timeouts.as_ref());
43        self.stream()
44            .send()
45            .await?
46            .error_for_status_with_body(&deadline)
47            .await?
48            .download_to(path, limit)
49            .await
50    }
51}