set depth to 0 when looking for single result in req_props
This commit is contained in:
parent
723ceb2655
commit
b08e6d3898
@ -3,6 +3,7 @@ use dotenv::dotenv;
|
||||
use reqwest::Client;
|
||||
use reqwest::RequestBuilder;
|
||||
use reqwest::{Response, Error, Method};
|
||||
use reqwest::header::{HeaderValue, CONTENT_TYPE, HeaderMap, IntoHeaderName};
|
||||
use crate::utils::api::ApiProps;
|
||||
use crate::commands::config;
|
||||
use crate::commands::clone::get_url_props;
|
||||
@ -18,6 +19,7 @@ pub enum ApiError {
|
||||
pub struct ApiBuilder {
|
||||
client: Client,
|
||||
request: Option<RequestBuilder>,
|
||||
headers: Option<HeaderMap>
|
||||
}
|
||||
|
||||
impl ApiBuilder {
|
||||
@ -25,6 +27,7 @@ impl ApiBuilder {
|
||||
ApiBuilder {
|
||||
client: Client::new(),
|
||||
request: None,
|
||||
headers: None,
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,11 +87,18 @@ impl ApiBuilder {
|
||||
},
|
||||
Some(req) => {
|
||||
self.request = Some(req.body(xml_payload));
|
||||
self.set_header(CONTENT_TYPE, HeaderValue::from_static("application/xml"));
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_header<K: IntoHeaderName>(&mut self, key: K, val: HeaderValue) -> &mut ApiBuilder {
|
||||
let map = self.headers.get_or_insert(HeaderMap::new());
|
||||
map.insert(key, val);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_body(&mut self, body: Vec<u8>) -> &mut ApiBuilder {
|
||||
match self.request.take() {
|
||||
None => {
|
||||
@ -110,7 +120,13 @@ impl ApiBuilder {
|
||||
eprintln!("fatal: incorrect request");
|
||||
std::process::exit(1);
|
||||
},
|
||||
Some(req) => req.send().await.map_err(Error::from),
|
||||
Some(req) => {
|
||||
if let Some(headers) = &self.headers {
|
||||
req.headers(headers.clone()).send().await.map_err(Error::from)
|
||||
} else {
|
||||
req.send().await.map_err(Error::from)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ use std::io::Cursor;
|
||||
use chrono::{Utc, DateTime};
|
||||
use reqwest::{Method, Response, Error};
|
||||
use xml::reader::{EventReader, XmlEvent};
|
||||
use reqwest::header::HeaderValue;
|
||||
use crate::utils::time::parse_timestamp;
|
||||
use crate::utils::api::{get_relative_s, ApiProps};
|
||||
use crate::services::api::{ApiBuilder, ApiError};
|
||||
@ -102,6 +103,11 @@ impl ReqProps {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_depth(&mut self, depth: &str) -> &mut ReqProps {
|
||||
self.api_builder.set_header("Depth", HeaderValue::from_str(depth).unwrap());
|
||||
self
|
||||
}
|
||||
|
||||
fn validate_xml(&mut self) -> &mut ReqProps {
|
||||
let mut xml = String::from(r#"<?xml version="1.0" encoding="UTF-8"?><d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns"><d:prop>"#);
|
||||
xml.push_str(&self.xml_payload.clone());
|
||||
@ -138,6 +144,8 @@ impl ReqProps {
|
||||
}
|
||||
|
||||
pub fn send_req_single(&mut self) -> Result<ObjProps, ApiError> {
|
||||
// set depth to 0 as we only need one element
|
||||
self.set_depth("0");
|
||||
match self.send_with_err() {
|
||||
Ok(body) => {
|
||||
let objs = self.parse(body, false);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user