From 08a094f334cc615fe080f4639d9e6f27b7d17404 Mon Sep 17 00:00:00 2001 From: Michael Gorven Date: Tue, 30 Apr 2024 07:02:43 -0700 Subject: [PATCH] Compress requests with zstd --- lightspark/Cargo.toml | 5 +++-- lightspark/src/request/requester.rs | 24 +++++++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lightspark/Cargo.toml b/lightspark/Cargo.toml index 46e161a..eccf00f 100644 --- a/lightspark/Cargo.toml +++ b/lightspark/Cargo.toml @@ -20,8 +20,8 @@ client = ["base", "objects", "dep:reqwest"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -reqwest = { version = "0.11", default-features = false, features = ["blocking", "json", "rustls-tls"], optional = true } -futures = "0.3" +reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "rustls-tls", "zstd"], optional = true } +futures = "0.3" base64 = "0.21.0" serde_json = "1.0.94" serde = { version = "1.0.155", features = ["derive"] } @@ -40,6 +40,7 @@ pbkdf2 = "0.12.2" rsa = { version ="0.9.2", features = ["sha2"] } cbc = "0.1.2" async-trait = "0.1.73" +zstd = "0.13" [dev-dependencies] tokio = { version = "1.12.0", features = ["full"] } diff --git a/lightspark/src/request/requester.rs b/lightspark/src/request/requester.rs index ea0350e..c543698 100644 --- a/lightspark/src/request/requester.rs +++ b/lightspark/src/request/requester.rs @@ -156,28 +156,38 @@ impl Requester { variables: Option, signing_key: Option, ) -> Result { - let body = build_graphql_request_body(operation, variables, signing_key.is_some())?; + let payload = build_graphql_request_body(operation, variables, signing_key.is_some())?; let mut headers = HeaderMap::new(); headers.insert("Content-Type", HeaderValue::from_static("application/json")); - if let Some(op) = body["operationName"].as_str() { + if let Some(op) = payload["operationName"].as_str() { if let Ok(value) = HeaderValue::from_str(op) { headers.insert("X-GraphQL-Operation", value); } } + let json_string = to_string(&payload) + .map_err(|_| Error::GraphqlError("Failed to serialise payload".to_owned()))?; + if let Some(key) = signing_key { - let json_string = - to_string(&body).map_err(|_| Error::GraphqlError("Body malformat.".to_owned()))?; - let payload: Vec = json_string.into_bytes(); - let signing = key.sign_payload(&payload).map_err(Error::CryptoError)?; + let signing = key + .sign_payload(&json_string.as_bytes()) + .map_err(Error::CryptoError)?; headers.insert( "X-Lightspark-Signing", HeaderValue::from_str(signing.as_str()).map_err(|_| Error::InvalidHeaderValue)?, ); } + let body = if json_string.len() > 1024 { + headers.insert("Content-Encoding", HeaderValue::from_static("zstd")); + zstd::bulk::compress(json_string.as_bytes(), 0) + .map_err(|_| Error::GraphqlError("Failed to compress payload".to_owned()))? + } else { + json_string.into_bytes() + }; + let url = match &self.base_url { Some(base_url) => base_url.clone(), None => DEFAULT_BASE_URL.to_owned(), @@ -187,7 +197,7 @@ impl Requester { .client .post(url) .headers(headers) - .json(&body) + .body(body) .send() .await .map_err(|e| Error::ReqwestError(e.to_string()))?;