feat(reqprops): deserialize response

This commit is contained in:
grimhilt 2024-09-15 16:16:38 +02:00
parent 61531f664b
commit 3af7a00b0f
3 changed files with 65 additions and 19 deletions

View File

@ -18,9 +18,12 @@ pub fn exec(args: TestArgs, config: Config) {
// println!("{:?}", config); // println!("{:?}", config);
// Ok(()) // Ok(())
let req = ReqProps::new("") tokio::runtime::Runtime::new().unwrap().block_on(async {
let mut req = ReqProps::new("")
.set_config(&config) .set_config(&config)
.get_properties(vec![Props::CreationDate, Props::GetLastModified]) .get_properties(vec![Props::CreationDate, Props::LastModified]);
.send(); req.send().await;
dbg!(req); });
// dbg!(req);
} }

View File

@ -1,15 +1,17 @@
use crate::services::service::Service; use crate::services::service::Service;
use crate::store::object::Obj; use crate::store::object::Obj;
use crate::config::config::Config; use crate::config::config::Config;
use serde::Deserialize;
use serde_xml_rs::from_str;
pub enum Props { pub enum Props {
CreationDate, CreationDate,
GetLastModified, LastModified,
GetETag, ETag,
GetContentType, ContentType,
RessourceType, RessourceType,
GetContentLength, ContentLength,
GetContentLanguage, ContentLanguage,
DisplayName, DisplayName,
FileId, FileId,
Permissions, Permissions,
@ -23,11 +25,41 @@ pub enum Props {
ContainedFileCountm, ContainedFileCountm,
} }
#[derive(Deserialize, Debug)]
struct Propstat {
#[serde(rename = "prop")]
prop: Prop,
}
#[derive(Deserialize, Debug)]
struct Prop {
#[serde(rename = "getlastmodified")]
last_modified: Option<String>,
#[serde(rename = "getcontentlength")]
content_length: Option<u64>,
}
#[derive(Deserialize, Debug)]
struct Response {
#[serde(rename = "href")]
href: String,
#[serde(rename = "propstat")]
propstat: Propstat,
}
#[derive(Deserialize, Debug)]
struct Multistatus {
#[serde(rename = "response")]
responses: Vec<Response>,
}
impl From<&Props> for &str { impl From<&Props> for &str {
fn from(variant: &Props) -> Self { fn from(variant: &Props) -> Self {
match variant { match variant {
Props::CreationDate => "<d:creationdate />", Props::CreationDate => "<d:creationdate />",
Props::GetLastModified => "<d:getlastmodified />", Props::LastModified => "<d:getlastmodified />",
_ => todo!("Props conversion not implemented"), _ => todo!("Props conversion not implemented"),
} }
} }
@ -73,8 +105,18 @@ impl ReqProps {
xml xml
} }
pub fn send(&mut self) { pub async fn send(&mut self) {
self.service.send(); dbg!("send");
let res = self.service.send().await;
dbg!("Sent");
let text = res.unwrap().text().await.unwrap();
self.parse(&text);
}
fn parse (&self, xml: &str) {
dbg!("Parsing");
let multistatus: Multistatus = from_str(&xml).unwrap();
dbg!(multistatus);
} }
} }

View File

@ -1,5 +1,5 @@
use crate::config::config::Config; use crate::config::config::Config;
use reqwest::blocking::{Client, ClientBuilder}; use reqwest::{Client, ClientBuilder};
use reqwest::{header::HeaderMap, Method, Url}; use reqwest::{header::HeaderMap, Method, Url};
const USER_AGENT: &str = "Nextsync"; const USER_AGENT: &str = "Nextsync";
@ -65,9 +65,10 @@ impl Service {
self self
} }
pub fn send(&mut self) { pub async fn send(&mut self) -> Result<reqwest::Response, reqwest::Error> {
let mut url = self let mut url = self
.config.clone() .config
.clone()
.expect("A config must be provided to service") .expect("A config must be provided to service")
.get_nsconfig() .get_nsconfig()
.get_remote("origin") .get_remote("origin")
@ -75,11 +76,11 @@ impl Service {
.expect("An url must be set on the remote"); .expect("An url must be set on the remote");
url.push_str(&self.url.clone().unwrap()); url.push_str(&self.url.clone().unwrap());
dbg!(self self.client
.client
.build() .build()
.request(self.method.clone().expect("Method must be set"), url,) .request(self.method.clone().expect("Method must be set"), url)
.bearer_auth("rK5ud2NmrR8p586Th7v272HRgUcZcEKIEluOGjzQQRj7gWMMAISFTiJcFnnmnNiu2VVlENks") .bearer_auth("rK5ud2NmrR8p586Th7v272HRgUcZcEKIEluOGjzQQRj7gWMMAISFTiJcFnnmnNiu2VVlENks")
.send()); .send()
.await
} }
} }