webdav_request/client/
webdav_client.rs

1use std::sync::Arc;
2
3use crate::client::inner::InnerClient;
4use crate::client::ALL_DROP;
5use crate::method::Method;
6use crate::multistatus::MultiStatus;
7use crate::WevDAVRequestBuilder;
8use reqwest::header::{HeaderName, HeaderValue, CONTENT_TYPE};
9use reqwest::{IntoUrl, Response};
10const DEPTH: HeaderName = HeaderName::from_static("depth");
11const DEPTH_ONE: HeaderValue = HeaderValue::from_static("1");
12const APPLICATION_XML: HeaderValue = HeaderValue::from_static("application/xml");
13#[deprecated = "Use `DavClient` instead"]
14#[derive(Default, Clone)]
15pub struct WebDAVClient {
16    inner: Arc<InnerClient>,
17}
18#[allow(deprecated)]
19unsafe impl Send for WebDAVClient {}
20
21#[allow(deprecated)]
22unsafe impl Sync for WebDAVClient {}
23
24macro_rules! into_url {
25    ($url:expr) => {
26        match $url.into_url() {
27            Ok(url) => url,
28            Err(e) => panic!("{e}"),
29        }
30    };
31}
32
33#[allow(deprecated)]
34impl WebDAVClient {
35    pub fn new(username: &str, password: &str) -> Result<Self, reqwest::Error> {
36        Ok(Self {
37            inner: Arc::new(InnerClient::new(username, password)?),
38        })
39    }
40    pub fn request(&self, method: Method, url: impl IntoUrl) -> WevDAVRequestBuilder {
41        WevDAVRequestBuilder::new(self.inner.clone(), into_url!(url), method)
42    }
43
44    #[inline(always)]
45    pub fn get(&self, url: impl IntoUrl) -> WevDAVRequestBuilder {
46        self.request(Method::GET, url)
47    }
48
49    #[inline(always)]
50    pub fn put(&self, url: impl IntoUrl) -> WevDAVRequestBuilder {
51        self.request(Method::PUT, url)
52    }
53
54    pub async fn list(
55        &self,
56        url: impl IntoUrl,
57    ) -> Result<crate::res::Collection, crate::error::Error> {
58        let response = self.all_propfind(url).await?;
59        if response.status().is_success() {
60            let xml = response.text().await?;
61            let multi_status = MultiStatus::parse(&xml)?;
62            Ok(crate::res::Collection::from(multi_status))
63        } else {
64            Err(crate::error::Error::ResponseError(response.status()))
65        }
66    }
67    #[inline(always)]
68    pub async fn all_propfind(&self, url: impl IntoUrl) -> crate::Result<Response> {
69        self.request(Method::PROPFIND, url.into_url()?)
70            .header(CONTENT_TYPE, APPLICATION_XML.clone())
71            .header(DEPTH.clone(), DEPTH_ONE.clone())
72            .body(ALL_DROP)
73            .send()
74            .await
75    }
76}