webdav_request/client/
mod.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
mod inner;
use std::future::Future;
use std::sync::Arc;

use crate::method::Method;
use crate::reader::LazyResponseReader;
use crate::res::Collection;
use crate::res::MultiStatus;
use crate::{header::HeaderMap, Body};
pub use inner::Auth;
pub use inner::InnerClient;
pub use inner::Range;
use reqwest::header::{HeaderName, HeaderValue, CONTENT_TYPE};
use reqwest::Response;

use crate::url::WebDavURL;

const ALL_DROP: &str = r#"
<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:">
<d:allprop/>
</d:propfind>
"#;

#[derive(Default, Clone)]
pub struct WebDAVClient {
    inner: Arc<InnerClient>,
}
unsafe impl Send for WebDAVClient {}

unsafe impl Sync for WebDAVClient {}

impl WebDAVClient {
    pub fn new(auth: Option<Auth>, base_url: Option<&str>) -> Result<Self, reqwest::Error> {
        Ok(Self {
            inner: Arc::new(InnerClient {
                auth,
                base_url: base_url.map(WebDavURL::new),
                inner: reqwest::Client::builder().build()?,
            }),
        })
    }
    pub fn request(&self, method: Method, url: &str) -> WevDAVRequestBuilder {
        WevDAVRequestBuilder::new(self.inner.clone(), url, method)
    }

    #[inline(always)]
    pub fn get(&self, url: &str) -> impl Future<Output = Result<Response, reqwest::Error>> {
        self.request(Method::GET, url).send()
    }

    #[inline(always)]
    pub async fn list_collection<U: AsRef<str>>(
        &self,
        url: U,
    ) -> Result<Collection, crate::error::Error> {
        let response = self.all_propfind(url.as_ref()).await?;
        if response.status().is_success() {
            let xml = response.text().await?;
            let multi_status = MultiStatus::from_str(&xml)?;
            Ok(Collection::from(multi_status))
        } else {
            Err(crate::error::Error::ResponseError(response.status()))
        }
    }
    #[inline(always)]
    pub fn all_propfind(
        &self,
        url: &str,
    ) -> impl Future<Output = Result<Response, reqwest::Error>> {
        self.request(Method::PROPFIND, url)
            .header(CONTENT_TYPE, HeaderValue::from_static("application/xml"))
            .body(ALL_DROP)
            .send()
    }
}

pub struct WevDAVRequestBuilder {
    client: Arc<InnerClient>,
    basic_auth: Option<(String, String)>,
    url: String,
    headers: HeaderMap,
    body: Option<Body>,
    method: Method,
}

impl<'c> WevDAVRequestBuilder {
    pub fn new(client: Arc<InnerClient>, url: &str, method: Method) -> Self {
        Self {
            client,
            basic_auth: None,
            headers: HeaderMap::new(),
            url: String::from(url),
            method,
            body: None,
        }
    }
    pub fn basic_auth(self, username: &str, password: &str) -> Self {
        Self {
            basic_auth: Some((username.to_owned(), password.to_owned())),
            ..self
        }
    }
    pub fn body(self, body: impl Into<Body>) -> Self {
        Self {
            body: Some(body.into()),
            ..self
        }
    }
    #[inline(always)]
    pub fn range(self, range: &Range) -> Self {
        self.header(
            HeaderName::from_static("range"),
            HeaderValue::from_bytes(format!("bytes={}-{}", range.start, range.end).as_bytes())
                .unwrap(),
        )
    }
    pub fn header(mut self, key: HeaderName, val: HeaderValue) -> Self {
        self.headers.insert(key, val);
        self
    }
    pub fn headers(mut self, headers: HeaderMap) -> Self {
        self.headers.extend(headers);
        self
    }

    pub fn build(self) -> crate::RequestBuilder {
        let builder = self.client.inner.request(
            self.method.convert(),
            self.client
                .base_url
                .as_ref()
                .map_or(self.url.clone(), |url| url.url_join(&self.url)),
        );
        let builder = if let Some(body) = self.body {
            builder.body(body)
        } else {
            builder
        };
        if let Some((usr, pass)) = &self.basic_auth {
            builder.basic_auth(usr, Some(pass))
        } else if let Some(auth) = &self.client.auth {
            builder.basic_auth(&auth.username, Some(&auth.password))
        } else {
            builder
        }
        .headers(self.headers)
    }
    pub fn into_lazy_reader(self) -> LazyResponseReader {
        LazyResponseReader::new(self.build())
    }
    pub fn send(self) -> impl Future<Output = Result<Response, reqwest::Error>> {
        self.build().send()
    }
}