move async in services
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -38,18 +38,20 @@ impl DownloadFiles {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn save(&mut self, ref_p: PathBuf) -> Result<(), ApiError> {
|
||||
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() {
|
||||
let body = res.bytes().await.map_err(ApiError::EmptyError)?;
|
||||
match DownloadFiles::write_file(p, &body.to_vec()) {
|
||||
Err(_) => Err(ApiError::Unexpected(String::from(""))),
|
||||
Ok(_) => Ok(()),
|
||||
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() {
|
||||
let body = res.bytes().await.map_err(ApiError::EmptyError)?;
|
||||
match DownloadFiles::write_file(p, &body.to_vec()) {
|
||||
Err(_) => Err(ApiError::Unexpected(String::from(""))),
|
||||
Ok(_) => Ok(()),
|
||||
}
|
||||
} else {
|
||||
Err(ApiError::IncorrectRequest(res))
|
||||
}
|
||||
} else {
|
||||
Err(ApiError::IncorrectRequest(res))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn write_file(path: PathBuf, content: &Vec<u8>) -> io::Result<()> {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user