fetchy/
builder.rs

1use super::*;
2
3/// Builder for [`Fetch`].
4///
5/// Created by [`Fetch::builder()`].
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct FetchBuilder<'a> {
8    url: &'a str,
9    method: Method,
10    headers: Vec<Header<'a>>,
11    data: Vec<u8>,
12}
13
14impl<'a> FetchBuilder<'a> {
15    /// Overwrite HTTP method.  Default is [`Method::Get`].
16    pub fn method(mut self, method: Method) -> Self {
17        self.method = method;
18
19        self
20    }
21
22    /// Send data in the body of the request.
23    pub fn body(mut self, data: impl Into<Vec<u8>>) -> Self {
24        self.data = data.into();
25
26        self
27    }
28
29    /// Add a header to the HTTP request.
30    pub fn header(mut self, header: Header<'a>) -> Self {
31        self.headers.push(header);
32
33        self
34    }
35
36    /// Initiate the HTTP request.
37    pub fn fetch(self) -> Fetch {
38        Fetch::new(self.url, self.method, self.headers, self.data)
39    }
40
41    pub(crate) fn new(url: &'a str) -> Self {
42        Self {
43            url,
44            method: Method::Get,
45            headers: Vec::new(),
46            data: Vec::new(),
47        }
48    }
49}