Skip to content

Commit

Permalink
Add GZIP support (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
Deniskore authored Jul 8, 2024
1 parent ae64a04 commit b245d66
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ aws-lc-rs = ["yup-oauth2/aws-lc-rs"]
# Feature used to remove cloud-storage from the standard build.
# cloud-storage has a dependency on chrono, so the feature is there to remove this dependency by default.
bq_load_job = ["cloud-storage"]
gzip = ["flate2"]

[dependencies.flate2]
version = "1.0"
optional = true

[dependencies]
yup-oauth2 = { version = "11", default-features = false, features = ["hyper-rustls", "service-account"] }
Expand Down
26 changes: 25 additions & 1 deletion src/tabledata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ use crate::model::table_data_list_response::TableDataListResponse;
use crate::{process_response, urlencode, BIG_QUERY_V2_URL};
use reqwest::Client;

#[cfg(feature = "gzip")]
use flate2::{write::GzEncoder, Compression};
#[cfg(feature = "gzip")]
use reqwest::header::{CONTENT_ENCODING, CONTENT_TYPE};
#[cfg(feature = "gzip")]
use serde_json::to_string;
#[cfg(feature = "gzip")]
use std::io::Write;

/// A table data API handler.
#[derive(Clone)]
pub struct TableDataApi {
Expand Down Expand Up @@ -56,9 +65,24 @@ impl TableDataApi {

let access_token = self.auth.access_token().await?;

#[cfg(feature = "gzip")]
let request = {
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
encoder.write_all(to_string(&insert_request)?.as_bytes())?;
let gzipped_data = encoder.finish()?;
self.client
.post(&req_url)
.header(CONTENT_ENCODING, "gzip")
.header(CONTENT_TYPE, "application/octet-stream")
.bearer_auth(access_token)
.body(gzipped_data)
.build()?
};

#[cfg(not(feature = "gzip"))]
let request = self
.client
.post(req_url.as_str())
.post(&req_url)
.bearer_auth(access_token)
.json(&insert_request)
.build()?;
Expand Down

0 comments on commit b245d66

Please sign in to comment.