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
use reqwest::{header::CONTENT_TYPE, Response};

use crate::ALL_DROP;

pub struct WebDAVClient {
    inner: reqwest::Client,
    webdav_url: String,
    username: String,
    password: String,
}

impl WebDAVClient {
    pub fn new(webdav: &str, username: &str, password: &str) -> Self {
        Self {
            inner: reqwest::Client::new(),
            webdav_url: webdav.to_owned(),
            username: username.to_owned(),
            password: password.to_owned(),
        }
    }

    pub async fn get(&self, url: &str) -> reqwest::Result<Response> {
        let url = if self.webdav_url.is_empty() {
            url.to_owned()
        } else {
            format!("{}/{}", self.webdav_url, url)
        };
        self.inner
            .get(url)
            .basic_auth(&self.username, Some(&self.password))
            .send()
            .await
    }

    pub async fn all_propfind(&self, url: &str) -> reqwest::Result<Response> {
        let url = if self.webdav_url.is_empty() {
            url.to_owned()
        } else {
            format!("{}/{}", self.webdav_url, url)
        };
        self.inner
            .request(reqwest::Method::from_bytes(b"PROPFIND").unwrap(), url)
            .basic_auth(&self.username, Some(&self.password))
            .header(CONTENT_TYPE, "application/xml")
            .body(ALL_DROP)
            .send()
            .await
    }
}