feat(service): started service

This commit is contained in:
grimhilt
2024-09-14 21:33:11 +02:00
parent cd7b225145
commit e780279acd
15 changed files with 202 additions and 45 deletions

81
src/services/req_props.rs Normal file
View File

@@ -0,0 +1,81 @@
use crate::services::service::Service;
use crate::store::object::Obj;
pub enum Props {
CreationDate,
GetLastModified,
GetETag,
GetContentType,
RessourceType,
GetContentLength,
GetContentLanguage,
DisplayName,
FileId,
Permissions,
Size,
HasPreview,
Favorite,
CommentsUnread,
OwnerDisplayName,
ShareTypes,
ContainedFolderCount,
ContainedFileCountm,
}
impl From<&Props> for &str {
fn from(variant: &Props) -> Self {
match variant {
Props::CreationDate => "<d:creationdate />",
Props::GetLastModified => "<d:getlastmodified />",
_ => todo!("Props conversion not implemented"),
}
}
}
pub struct ReqProps {
service: Service,
properties: Vec<Props>,
}
impl ReqProps {
pub fn new(path: &str) -> Self {
ReqProps {
service: Service::new(),
properties: Vec::new(),
}
}
pub fn get_properties(mut self, properties: Vec<Props>) -> Self {
self.properties.extend(properties);
self
}
pub fn get_property(&mut self, property: Props) {
self.properties.push(property);
}
fn get_body(&self) -> String {
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>"#,
);
for property in &self.properties {
xml.push_str(property.into());
}
xml.push_str(r#"</d:prop></d:propfind>"#);
xml
}
pub fn send(&mut self) {
self.service
.propfind(String::from(
"http://localhost:8080/remote.php/dav/files/admin/Documents",
))
.send();
}
}
impl From<Obj> for ReqProps {
fn from(obj: Obj) -> Self {
ReqProps::new("")
}
}

63
src/services/service.rs Normal file
View File

@@ -0,0 +1,63 @@
use core::ops::{Deref, DerefMut};
use reqwest::blocking::{Client, ClientBuilder, RequestBuilder};
use reqwest::{header::HeaderMap, Method, Url};
const USER_AGENT: &str = "Nextsync";
pub struct ClientConfig {
client: Option<ClientBuilder>,
}
impl ClientConfig {
pub fn new() -> Self {
ClientConfig {
client: Some(Client::builder().user_agent(USER_AGENT)),
}
}
pub fn default_headers(mut self, headers: HeaderMap) -> Self {
self.client = Some(self.client.take().expect("Client was already built").default_headers(headers));
self
}
pub fn build(&mut self) -> Client {
self.client.take().expect("Cannot build the client twice").build().unwrap()
}
}
pub struct Service {
client: ClientConfig,
method: Option<Method>,
url: Option<String>,
headers: HeaderMap,
body: Option<String>,
}
impl Service {
pub fn new() -> Self {
Service {
client: ClientConfig::new(),
method: None,
url: None,
headers: HeaderMap::new(),
body: None,
}
}
pub fn propfind(&mut self, url: String) -> &mut Self {
self.method = Some(Method::from_bytes(b"PROPFIND").expect("Cannot be invalid"));
self.url = Some(url);
self
}
pub fn send(&mut self) {
dbg!(self.client
.build()
.request(
self.method.clone().expect("Method must be set"),
self.url.clone().expect("Url must be set"),
)
.bearer_auth("rK5ud2NmrR8p586Th7v272HRgUcZcEKIEluOGjzQQRj7gWMMAISFTiJcFnnmnNiu2VVlENks")
.send());
}
}