diff --git a/src/api.rs b/src/api.rs index ca2f2d377..9d0bafe19 100644 --- a/src/api.rs +++ b/src/api.rs @@ -27,6 +27,7 @@ use crate::{ fn make_reqwest_client() -> Client { Client::builder() .timeout(Duration::from_secs(10)) + .user_agent(crate::SDK_USER_AGENT) .build() .expect("Could not build client") } diff --git a/src/connection.rs b/src/connection.rs index 4237dbab8..9ca88fd7f 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -5,7 +5,7 @@ use std::{borrow::Cow, collections::HashMap, str::FromStr}; use data_encoding::HEXLOWER; use reqwest::{multipart, Client, StatusCode}; -use crate::{errors::ApiError, types::BlobId}; +use crate::{errors::ApiError, types::BlobId, SDK_HEADER, SDK_USER_AGENT}; /// Map HTTP response status code to an ApiError if it isn't "200". /// @@ -99,9 +99,10 @@ pub(crate) async fn send_simple( // Send request log::trace!("Sending HTTP request"); let res = client - .post(&format!("{}/send_simple", endpoint)) + .post(format!("{}/send_simple", endpoint)) .form(¶ms) .header("accept", "application/json") + .header(SDK_HEADER, SDK_USER_AGENT) .send() .await?; log::trace!("Received HTTP response"); @@ -139,9 +140,10 @@ pub(crate) async fn send_e2e( // Send request log::trace!("Sending HTTP request"); let res = client - .post(&format!("{}/send_e2e", endpoint)) + .post(format!("{}/send_e2e", endpoint)) .form(¶ms) .header("accept", "application/json") + .header(SDK_HEADER, SDK_USER_AGENT) .send() .await?; log::trace!("Received HTTP response"); @@ -186,6 +188,7 @@ pub(crate) async fn blob_upload( .post(&url) .multipart(form) .header("accept", "text/plain") + .header(SDK_HEADER, SDK_USER_AGENT) .send() .await?; map_response_code(res.status(), Some(ApiError::BadBlob))?; @@ -209,7 +212,11 @@ pub(crate) async fn blob_download( ); // Send request - let res = client.get(&url).send().await?; + let res = client + .get(&url) + .header(SDK_HEADER, SDK_USER_AGENT) + .send() + .await?; map_response_code(res.status(), Some(ApiError::BadBlob))?; // Read response bytes diff --git a/src/lib.rs b/src/lib.rs index bdeac8a02..69a8dd5bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,6 +102,10 @@ pub use crate::receive::IncomingMessage; const MSGAPI_URL: &str = "https://msgapi.threema.ch"; +// Custom header for identifying the type of SDK used +pub(crate) const SDK_HEADER: &str = "x-threema-gateway-sdk"; +pub(crate) const SDK_USER_AGENT: &str = concat!("threema-gateway-rs/", env!("CARGO_PKG_VERSION")); + #[cfg(test)] mod tests { #[test] diff --git a/src/lookup.rs b/src/lookup.rs index 36fde88bc..7a6b121df 100644 --- a/src/lookup.rs +++ b/src/lookup.rs @@ -6,7 +6,9 @@ use crypto_box::KEY_SIZE; use data_encoding::HEXLOWER_PERMISSIVE; use reqwest::Client; -use crate::{connection::map_response_code, errors::ApiError, RecipientKey}; +use crate::{ + connection::map_response_code, errors::ApiError, RecipientKey, SDK_HEADER, SDK_USER_AGENT, +}; /// Different ways to look up a Threema ID in the directory. #[derive(Debug, PartialEq)] @@ -134,7 +136,11 @@ pub(crate) async fn lookup_pubkey( debug!("Looking up public key for {}", their_id); // Send request - let res = client.get(&url).send().await?; + let res = client + .get(&url) + .header(SDK_HEADER, SDK_USER_AGENT) + .send() + .await?; map_response_code(res.status(), None)?; // Read response body @@ -177,7 +183,11 @@ pub(crate) async fn lookup_id( debug!("Looking up id key for {}", criterion); // Send request - let res = client.get(&url).send().await?; + let res = client + .get(&url) + .header(SDK_HEADER, SDK_USER_AGENT) + .send() + .await?; map_response_code(res.status(), Some(ApiError::BadHashLength))?; // Read and return response body @@ -196,7 +206,11 @@ pub(crate) async fn lookup_credits( debug!("Looking up remaining credits"); // Send request - let res = client.get(&url).send().await?; + let res = client + .get(&url) + .header(SDK_HEADER, SDK_USER_AGENT) + .send() + .await?; map_response_code(res.status(), None)?; // Read, parse and return response body @@ -226,7 +240,11 @@ pub(crate) async fn lookup_capabilities( debug!("Looking up capabilities for {}", their_id); // Send request - let res = client.get(&url).send().await?; + let res = client + .get(&url) + .header(SDK_HEADER, SDK_USER_AGENT) + .send() + .await?; map_response_code(res.status(), Some(ApiError::BadHashLength))?; // Read response body