Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compress requests with zstd #42

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lightspark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"] }
24 changes: 17 additions & 7 deletions lightspark/src/request/requester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,38 @@ impl Requester {
variables: Option<Value>,
signing_key: Option<T>,
) -> Result<Value, Error> {
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<u8> = 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(),
Expand All @@ -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()))?;
Expand Down
Loading