binstalk_downloader/remote/
request_builder.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::fmt;

use bytes::Bytes;
use futures_util::{Stream, StreamExt};
use reqwest::Method;

use super::{header, Client, Error, HttpError, StatusCode, Url};

pub use reqwest::Body;

#[cfg(feature = "json")]
pub use serde_json::Error as JsonError;

#[derive(Debug)]
pub struct RequestBuilder {
    pub(super) client: Client,
    pub(super) inner: reqwest::RequestBuilder,
}

impl RequestBuilder {
    pub fn bearer_auth(self, token: &dyn fmt::Display) -> Self {
        Self {
            client: self.client,
            inner: self.inner.bearer_auth(token),
        }
    }

    pub fn header(self, key: &str, value: &str) -> Self {
        Self {
            client: self.client,
            inner: self.inner.header(key, value),
        }
    }

    pub fn body(self, body: impl Into<Body>) -> Self {
        Self {
            client: self.client,
            inner: self.inner.body(body.into()),
        }
    }

    pub async fn send(self, error_for_status: bool) -> Result<Response, Error> {
        let request = self.inner.build()?;
        let method = request.method().clone();
        Ok(Response {
            inner: self.client.send_request(request, error_for_status).await?,
            method,
        })
    }
}

#[derive(Debug)]
pub struct Response {
    inner: reqwest::Response,
    method: Method,
}

impl Response {
    pub async fn bytes(self) -> Result<Bytes, Error> {
        self.inner.bytes().await.map_err(Error::from)
    }

    pub fn bytes_stream(self) -> impl Stream<Item = Result<Bytes, Error>> {
        let url = Box::new(self.inner.url().clone());
        let method = self.method;

        self.inner.bytes_stream().map(move |res| {
            res.map_err(|err| {
                Error::Http(Box::new(HttpError {
                    method: method.clone(),
                    url: Url::clone(&*url),
                    err,
                }))
            })
        })
    }

    pub fn status(&self) -> StatusCode {
        self.inner.status()
    }

    pub fn url(&self) -> &Url {
        self.inner.url()
    }

    pub fn method(&self) -> &Method {
        &self.method
    }

    pub fn error_for_status_ref(&self) -> Result<&Self, Error> {
        match self.inner.error_for_status_ref() {
            Ok(_) => Ok(self),
            Err(err) => Err(Error::Http(Box::new(HttpError {
                method: self.method().clone(),
                url: self.url().clone(),
                err,
            }))),
        }
    }

    pub fn error_for_status(self) -> Result<Self, Error> {
        match self.error_for_status_ref() {
            Ok(_) => Ok(self),
            Err(err) => Err(err),
        }
    }

    pub fn headers(&self) -> &header::HeaderMap {
        self.inner.headers()
    }

    #[cfg(feature = "json")]
    pub async fn json<T>(self) -> Result<T, Error>
    where
        T: serde::de::DeserializeOwned,
    {
        let bytes = self.error_for_status()?.bytes().await?;
        Ok(serde_json::from_slice(&bytes)?)
    }
}