add trait ApiCall

This commit is contained in:
grimhilt
2023-10-21 21:47:48 +02:00
parent 07f6405b26
commit 9ea1d01c27
13 changed files with 292 additions and 210 deletions

View File

@@ -1,25 +1,32 @@
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use reqwest::{Method, Response, Error};
use reqwest::Method;
use crate::services::api::{ApiBuilder, ApiError};
use crate::services::api_call::ApiCall;
pub struct UploadFile {
api_builder: ApiBuilder,
}
impl UploadFile {
pub fn new() -> Self {
impl ApiCall for UploadFile {
fn new() -> Self {
UploadFile {
api_builder: ApiBuilder::new(),
}
}
pub fn set_url(&mut self, url: &str) -> &mut UploadFile {
fn set_url(&mut self, url: &str) -> &mut UploadFile {
self.api_builder.build_request(Method::PUT, url);
self
}
fn send(&mut self) -> Result<Option<String>, ApiError> {
self.api_builder.send(true)
}
}
impl UploadFile {
pub fn set_file(&mut self, path: PathBuf) -> &mut UploadFile {
// todo large file
// todo small files
@@ -29,23 +36,4 @@ impl UploadFile {
self.api_builder.set_body(buffer);
self
}
pub async fn send(&mut self) -> Result<Response, Error> {
self.api_builder.send().await
}
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 = tokio::runtime::Runtime::new().unwrap().block_on(async {
res.text().await
}).map_err(ApiError::EmptyError)?;
Ok(body)
} else {
Err(ApiError::IncorrectRequest(res))
}
}
}