Merge remote-tracking branch 'origin/v1' into chore/merge-v1-into-v2

This commit is contained in:
FabianLars
2024-04-15 18:23:45 +02:00
65 changed files with 1075 additions and 371 deletions
+16 -4
View File
@@ -41,6 +41,8 @@ pub enum Error {
Request(#[from] reqwest::Error),
#[error("{0}")]
ContentLength(String),
#[error("request failed with status code {0}: {1}")]
HttpErrorCode(u16, String),
}
impl Serialize for Error {
@@ -98,13 +100,17 @@ async fn upload(
file_path: &str,
headers: HashMap<String, String>,
on_progress: Channel,
) -> Result<serde_json::Value> {
) -> Result<String> {
// Read the file
let file = File::open(file_path).await?;
let file_len = file.metadata().await.unwrap().len();
// Create the request and attach the file to the body
let client = reqwest::Client::new();
let mut request = client.post(url).body(file_to_body(on_progress, file));
let mut request = client
.post(url)
.header(reqwest::header::CONTENT_LENGTH, file_len)
.body(file_to_body(on_progress, file));
// Loop trought the headers keys and values
// and add them to the request object.
@@ -113,8 +119,14 @@ async fn upload(
}
let response = request.send().await?;
response.json().await.map_err(Into::into)
if response.status().is_success() {
response.text().await.map_err(Into::into)
} else {
Err(Error::HttpErrorCode(
response.status().as_u16(),
response.text().await.unwrap_or_default(),
))
}
}
fn file_to_body(channel: Channel, file: File) -> reqwest::Body {