Skip to content

Commit

Permalink
fix(upload): return type on is now a string (#976)
Browse files Browse the repository at this point in the history
* fix(upload): return type on POST is now string

A POST to a webserver can not always be expected to be a JSON response.

Success is now determined by the HTTP return code.

Upon success the body content is returned as a string.

* feat: add content-length on POST

Not all embedded devices are acceptable to receiving unspecified amounts
of data. Appending the content-length up front helps this devices
succeed.

* fix: return values unified

The return values was not used.

On POST the HTTP error code is returned as an enum.

* fix: upload, return value as string

* Update plugins/upload/src/lib.rs

Co-authored-by: Lucas Fernandes Nogueira <[email protected]>

* Update plugins/upload/src/lib.rs

Co-authored-by: Lucas Fernandes Nogueira <[email protected]>

* fix: added covector changelog file

---------

Co-authored-by: Lucas Fernandes Nogueira <[email protected]>
  • Loading branch information
lyager and lucasfernog authored Apr 15, 2024
1 parent 9144521 commit 4a5ab18
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changes/upload-returnval.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"upload": patch
"upload-js": patch
---

Return the upload response as a string and error out if the status code is not within 200-299.
4 changes: 2 additions & 2 deletions plugins/upload/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function upload(
filePath: string,
progressHandler?: ProgressHandler,
headers?: Map<string, string>,
): Promise<void> {
): Promise<string> {
const ids = new Uint32Array(1);
window.crypto.getRandomValues(ids);
const id = ids[0];
Expand All @@ -44,7 +44,7 @@ async function upload(

await listenToEventIfNeeded("upload://progress");

await invoke("plugin:upload|upload", {
return await invoke("plugin:upload|upload", {
id,
url,
filePath,
Expand Down
20 changes: 16 additions & 4 deletions plugins/upload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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 {
Expand Down Expand Up @@ -93,13 +95,17 @@ async fn upload<R: Runtime>(
url: &str,
file_path: &str,
headers: HashMap<String, String>,
) -> 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(id, window, file));
let mut request = client
.post(url)
.header(reqwest::header::CONTENT_LENGTH, file_len)
.body(file_to_body(id, window, file));

// Loop trought the headers keys and values
// and add them to the request object.
Expand All @@ -108,8 +114,14 @@ async fn upload<R: Runtime>(
}

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<R: Runtime>(id: u32, window: Window<R>, file: File) -> reqwest::Body {
Expand Down

0 comments on commit 4a5ab18

Please sign in to comment.