Skip to content

Commit

Permalink
convert file to body
Browse files Browse the repository at this point in the history
  • Loading branch information
harshdoesdev committed Oct 7, 2023
1 parent 5775f32 commit e9d913d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
58 changes: 58 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description = "Hello world"

[dependencies]
clap = { version = "4.4.5", features = ["derive"] }
reqwest = { version = "0.11.20", features = ["json", "blocking"] }
reqwest = { version = "0.11.20", features = ["json", "stream"] }
tokio = { version = "1.32.0", features = ["full"] }
atty = "0.2"
indicatif = "0.17.7"
Expand All @@ -19,3 +19,5 @@ zstd = "0.12"
tempfile = "3.8.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
futures = "0.3"
tokio-util = { version = "0.7.9", features = ["codec"]}
2 changes: 1 addition & 1 deletion src/commands/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub async fn upload(
let size = output_dir_archive.metadata()?.len();

let response = output_put_req
.stream(output_dir_archive, size)?;
.stream(output_dir_archive, size).await?;

if !response.status().is_success() {
println!("Could not upload artifacts.");
Expand Down
24 changes: 17 additions & 7 deletions src/vercel/artifact.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use tokio_util::codec::{BytesCodec, FramedRead};

pub struct ArtifactOptions {
duration: Option<u64>,
tag: Option<String>,
Expand Down Expand Up @@ -97,23 +99,31 @@ pub struct ArtifactPutRequest(pub ArtifactBaseRequest);
pub struct ArtifactGetRequest(pub ArtifactBaseRequest);
pub struct ArtifactExistsRequest(pub ArtifactBaseRequest);

fn file_to_body(file: std::fs::File) -> reqwest::Body {
let stream = FramedRead::new(tokio::fs::File::from(file), BytesCodec::new());
let body = reqwest::Body::wrap_stream(stream);
body
}

impl ArtifactPutRequest {
pub fn stream(
pub async fn stream(
&mut self,
artifact: std::fs::File,
content_len: u64,
) -> vercel_cache_helper::Result<reqwest::blocking::Response> {
let client = reqwest::blocking::Client::new();
) -> vercel_cache_helper::Result<reqwest::Response> {
let client = reqwest::Client::new();

let headers = self.0.get_headers("PUT", Some(content_len));

let response = client
let response = dbg!(client
.put(&self.0.url)
.headers(headers)
.body(artifact)
.send()?;
.body(file_to_body(artifact))
.send()
.await
.expect("Something went wrong!"));

Ok(response)
Ok(dbg!(response))
}

pub async fn buffer(
Expand Down

0 comments on commit e9d913d

Please sign in to comment.