move async in services

This commit is contained in:
grimhilt 2023-06-19 18:04:50 +02:00
parent 4cde39dffd
commit f1d552a31c
6 changed files with 147 additions and 147 deletions

View File

@ -5,7 +5,7 @@ use regex::Regex;
use crate::services::api::ApiError;
use crate::services::req_props::{ReqProps, ObjProps};
use crate::services::download_files::DownloadFiles;
use crate::store::object;
use crate::store::object::{self, add_blob, add_tree};
use crate::commands;
use crate::utils::api::ApiProps;
use crate::global::global::{DIR_PATH, set_dir_path};
@ -51,15 +51,13 @@ pub fn clone(remote: Values<'_>) {
};
// request folder content
let mut objs = vec![];
tokio::runtime::Runtime::new().unwrap().block_on(async {
let res = ReqProps::new()
.set_request(relative_s.as_str(), &api_props)
.gethref()
.getlastmodified()
.send_req_multiple()
.await;
objs = match res {
.send_req_multiple();
let mut objs = match res {
Ok(o) => o,
Err(ApiError::IncorrectRequest(err)) => {
eprintln!("fatal: {}", err.status());
@ -74,8 +72,7 @@ pub fn clone(remote: Values<'_>) {
std::process::exit(1);
},
Err(ApiError::Unexpected(_)) => todo!()
}
});
};
// create folder
if first_iter {
@ -90,14 +87,14 @@ pub fn clone(remote: Values<'_>) {
// create folder
let p = ref_path.clone().join(Path::new(&relative_s));
if let Err(err) = DirBuilder::new().recursive(true).create(p.clone()) {
eprintln!("error: cannot create directory {}: {}", p.display(), err);
eprintln!("err: cannot create directory {}: {}", p.display(), err);
}
// add tree
let path_folder = p.strip_prefix(ref_path.clone()).unwrap();
let last_modified = folder.lastmodified.unwrap().timestamp_millis();
if object::add_tree(&path_folder, &last_modified.to_string()).is_err() {
eprintln!("error: cannot store object {}", path_folder.display());
let lastmodified = folder.lastmodified.unwrap().timestamp_millis();
if let Err(err) = add_tree(&path_folder, &lastmodified.to_string()) {
eprintln!("err: saving ref of {}: {}", path_folder.display(), err);
}
}
@ -119,22 +116,21 @@ pub fn clone(remote: Values<'_>) {
fn download_files(ref_p: PathBuf, files: Vec<ObjProps>, api_props: &ApiProps) {
for obj in files {
tokio::runtime::Runtime::new().unwrap().block_on(async {
let relative_s = &obj.clone().relative_s.unwrap();
let res = DownloadFiles::new()
.set_url(&relative_s, api_props)
.save(ref_p.clone()).await;
.save(ref_p.clone());
match res {
Ok(()) => {
let relative_p = Path::new(&relative_s);
let last_modified = obj.clone().lastmodified.unwrap().timestamp_millis();
if let Err(_) = object::add_blob(relative_p, &last_modified.to_string()) {
eprintln!("error saving reference of {}", relative_s.clone());
let lastmodified = obj.clone().lastmodified.unwrap().timestamp_millis();
if let Err(err) = add_blob(relative_p, &lastmodified.to_string()) {
eprintln!("err: saving ref of {}: {}", relative_s.clone(), err);
}
},
Err(ApiError::Unexpected(_)) => {
eprintln!("error writing {}", relative_s);
eprintln!("err: writing {}", relative_s);
},
Err(ApiError::IncorrectRequest(err)) => {
eprintln!("fatal: {}", err.status());
@ -146,7 +142,6 @@ fn download_files(ref_p: PathBuf, files: Vec<ObjProps>, api_props: &ApiProps) {
std::process::exit(1);
}
}
});
}
}

View File

@ -57,14 +57,12 @@ struct New {
impl PushChange for New {
fn can_push(&self) -> PushState {
// check if exist on server
let file_infos = tokio::runtime::Runtime::new().unwrap().block_on(async {
let res = ReqProps::new()
.set_url(&self.obj.path.to_str().unwrap())
.getlastmodified()
.send_req_single()
.await;
.send_req_single();
match res {
let file_infos = match res {
Ok(obj) => Ok(Some(obj)),
Err(ApiError::IncorrectRequest(err)) => {
if err.status() == 404 {
@ -74,8 +72,7 @@ impl PushChange for New {
}
},
Err(_) => Err(()),
}
});
};
if let Ok(infos) = file_infos {
if let Some(info) = infos {
@ -92,12 +89,10 @@ impl PushChange for New {
fn push(&self) {
let obj = &self.obj;
tokio::runtime::Runtime::new().unwrap().block_on(async {
let res = UploadFile::new()
.set_url(obj.path.to_str().unwrap())
.set_file(obj.path.clone())
.send_with_err()
.await;
.send_with_err();
match res {
Err(ApiError::IncorrectRequest(err)) => {
@ -110,7 +105,6 @@ impl PushChange for New {
}
_ => (),
}
});
// update tree
add_blob(&obj.path.clone(), "todo_date");
@ -128,14 +122,12 @@ struct Deleted {
impl PushChange for Deleted {
fn can_push(&self) -> PushState {
// check if exist on server
let file_infos = tokio::runtime::Runtime::new().unwrap().block_on(async {
let res = ReqProps::new()
.set_url(&self.obj.path.to_str().unwrap())
.getlastmodified()
.send_with_err()
.await;
.send_with_err();
match res {
let file_infos = match res {
Ok(obj) => Ok(Some(obj)),
Err(ApiError::IncorrectRequest(err)) => {
if err.status() == 404 {
@ -145,8 +137,7 @@ impl PushChange for Deleted {
}
},
Err(_) => Err(()),
}
});
};
if let Ok(infos) = file_infos {
if let Some(inf) = infos {
@ -164,11 +155,9 @@ impl PushChange for Deleted {
fn push(&self) {
let obj = &self.obj;
tokio::runtime::Runtime::new().unwrap().block_on(async {
let res = DeletePath::new()
.set_url(obj.path.to_str().unwrap())
.send_with_err()
.await;
.send_with_err();
match res {
Err(ApiError::IncorrectRequest(err)) => {
@ -181,7 +170,6 @@ impl PushChange for Deleted {
}
_ => (),
}
});
// update tree
rm_blob(&obj.path.clone());

View File

@ -21,10 +21,15 @@ impl DeletePath {
self.api_builder.send().await
}
pub async fn send_with_err(&mut self) -> Result<String, ApiError> {
let res = self.send().await.map_err(ApiError::RequestError)?;
pub fn send_with_err(&mut self) -> Result<String, ApiError> {
let res = tokio::runtime::Runtime::new().unwrap().block_on(async {
self.send().await
}).map_err(ApiError::RequestError)?;
if res.status().is_success() {
let body = res.text().await.map_err(ApiError::EmptyError)?;
let body = tokio::runtime::Runtime::new().unwrap().block_on(async {
res.text().await
}).map_err(ApiError::EmptyError)?;
Ok(body)
} else {
Err(ApiError::IncorrectRequest(res))

View File

@ -38,7 +38,8 @@ impl DownloadFiles {
}
}
pub async fn save(&mut self, ref_p: PathBuf) -> Result<(), ApiError> {
pub fn save(&mut self, ref_p: PathBuf) -> Result<(), ApiError> {
tokio::runtime::Runtime::new().unwrap().block_on(async {
let p = ref_p.join(PathBuf::from(self.relative_ps.clone()));
let res = self.send().await.map_err(ApiError::RequestError)?;
if res.status().is_success() {
@ -50,6 +51,7 @@ impl DownloadFiles {
} else {
Err(ApiError::IncorrectRequest(res))
}
})
}
fn write_file(path: PathBuf, content: &Vec<u8>) -> io::Result<()> {

View File

@ -115,25 +115,30 @@ impl ReqProps {
self.api_builder.send().await
}
pub async fn send_with_err(&mut self) -> Result<String, ApiError> {
let res = self.send().await.map_err(ApiError::RequestError)?;
pub fn send_with_err(&mut self) -> Result<String, ApiError> {
let res = tokio::runtime::Runtime::new().unwrap().block_on(async {
self.send().await
}).map_err(ApiError::RequestError)?;
if res.status().is_success() {
let body = res.text().await.map_err(ApiError::EmptyError)?;
let body = tokio::runtime::Runtime::new().unwrap().block_on(async {
res.text().await
}).map_err(ApiError::EmptyError)?;
Ok(body)
} else {
Err(ApiError::IncorrectRequest(res))
}
}
pub async fn send_req_multiple(&mut self) -> Result<Vec<ObjProps>, ApiError> {
match self.send_with_err().await {
pub fn send_req_multiple(&mut self) -> Result<Vec<ObjProps>, ApiError> {
match self.send_with_err() {
Ok(body) => Ok(self.parse(body, true)),
Err(err) => Err(err),
}
}
pub async fn send_req_single(&mut self) -> Result<ObjProps, ApiError> {
match self.send_with_err().await {
pub fn send_req_single(&mut self) -> Result<ObjProps, ApiError> {
match self.send_with_err() {
Ok(body) => {
let objs = self.parse(body, false);
let obj = objs[0].clone();

View File

@ -34,10 +34,15 @@ impl UploadFile {
self.api_builder.send().await
}
pub async fn send_with_err(&mut self) -> Result<String, ApiError> {
let res = self.send().await.map_err(ApiError::RequestError)?;
pub fn send_with_err(&mut self) -> Result<String, ApiError> {
let res = tokio::runtime::Runtime::new().unwrap().block_on(async {
self.send().await
}).map_err(ApiError::RequestError)?;
if res.status().is_success() {
let body = res.text().await.map_err(ApiError::EmptyError)?;
let body = tokio::runtime::Runtime::new().unwrap().block_on(async {
res.text().await
}).map_err(ApiError::EmptyError)?;
Ok(body)
} else {
Err(ApiError::IncorrectRequest(res))