cleaner clone
This commit is contained in:
@@ -4,6 +4,12 @@ use reqwest::{Response, Error, IntoUrl, Method};
|
||||
use std::env;
|
||||
use dotenv::dotenv;
|
||||
|
||||
pub enum ApiError {
|
||||
IncorrectRequest(reqwest::Response),
|
||||
EmptyError(reqwest::Error),
|
||||
RequestError(reqwest::Error),
|
||||
}
|
||||
|
||||
pub struct ApiBuilder {
|
||||
client: Client,
|
||||
request: Option<RequestBuilder>,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::services::api::ApiBuilder;
|
||||
use crate::services::api::{ApiBuilder, ApiError};
|
||||
use reqwest::{Method, IntoUrl, Response, Error};
|
||||
|
||||
pub struct DownloadFiles {
|
||||
@@ -16,4 +16,14 @@ impl DownloadFiles {
|
||||
pub async fn send(&mut self) -> Result<Response, Error> {
|
||||
self.api_builder.send().await
|
||||
}
|
||||
|
||||
pub async fn send_with_err(mut self) -> Result<Vec<u8>, ApiError> {
|
||||
let res = self.send().await.map_err(ApiError::RequestError)?;
|
||||
if res.status().is_success() {
|
||||
let body = res.bytes().await.map_err(ApiError::EmptyError)?;
|
||||
Ok(body.to_vec())
|
||||
} else {
|
||||
Err(ApiError::IncorrectRequest(res))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::services::api::ApiBuilder;
|
||||
use crate::services::api::{ApiBuilder, ApiError};
|
||||
use reqwest::{Method, IntoUrl, Response, Error};
|
||||
|
||||
pub struct ListFolders {
|
||||
@@ -16,4 +16,32 @@ impl ListFolders {
|
||||
pub async fn send(&mut self) -> Result<Response, Error> {
|
||||
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)?;
|
||||
if res.status().is_success() {
|
||||
let body = res.text().await.map_err(ApiError::EmptyError)?;
|
||||
Ok(body)
|
||||
} else {
|
||||
Err(ApiError::IncorrectRequest(res))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_with_res(self) -> String {
|
||||
match self.send_with_err().await {
|
||||
Ok(body) => body,
|
||||
Err(ApiError::IncorrectRequest(err)) => {
|
||||
eprintln!("fatal: {}", err.status());
|
||||
std::process::exit(1);
|
||||
},
|
||||
Err(ApiError::EmptyError(_)) => {
|
||||
eprintln!("Failed to get body");
|
||||
String::from("")
|
||||
}
|
||||
Err(ApiError::RequestError(err)) => {
|
||||
eprintln!("fatal: {}", err);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user