cleaner clone

This commit is contained in:
grimhilt
2023-06-05 18:12:02 +02:00
parent 1042a7ea22
commit 5b7b75846c
4 changed files with 143 additions and 76 deletions

View File

@@ -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>,

View File

@@ -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))
}
}
}

View File

@@ -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);
}
}
}
}