Skip to content

Commit

Permalink
Set default user agent, send along custom SDK version header
Browse files Browse the repository at this point in the history
The user agent can be overridden by the user by specifying a custom
reqwest client.
  • Loading branch information
dbrgn committed Sep 7, 2024
1 parent f126b1e commit 5b89a1e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
11 changes: 9 additions & 2 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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".
///
Expand Down Expand Up @@ -102,6 +102,7 @@ pub(crate) async fn send_simple(
.post(&format!("{}/send_simple", endpoint))
.form(&params)
.header("accept", "application/json")
.header(SDK_HEADER, SDK_USER_AGENT)
.send()
.await?;
log::trace!("Received HTTP response");
Expand Down Expand Up @@ -142,6 +143,7 @@ pub(crate) async fn send_e2e(
.post(&format!("{}/send_e2e", endpoint))
.form(&params)
.header("accept", "application/json")
.header(SDK_HEADER, SDK_USER_AGENT)
.send()
.await?;
log::trace!("Received HTTP response");
Expand Down Expand Up @@ -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))?;
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
28 changes: 23 additions & 5 deletions src/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5b89a1e

Please sign in to comment.