Skip to content

Commit

Permalink
Merge pull request #8 from Infisical/daniel/sdk-user-agents
Browse files Browse the repository at this point in the history
User agents for SDK's
  • Loading branch information
DanielHougaard authored Jan 8, 2024
2 parents b7bcf00 + b7e332c commit 984641c
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions crates/infisical-py/infisical_client/infisical_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def __init__(self, settings: ClientSettings = None):
if settings is None:
self.inner = infisical_py.InfisicalClient(None)
else:

settings.user_agent = "infisical-python-sdk"

settings_json = json.dumps(settings.to_dict())

self.inner = infisical_py.InfisicalClient(settings_json)
Expand Down
3 changes: 2 additions & 1 deletion crates/infisical/src/api/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub async fn access_token_request(client: &mut Client) -> Result<AccessTokenSucc
let request = req_client
.post(url)
.header(reqwest::header::CONTENT_TYPE, "application/json")
.header(reqwest::header::ACCEPT, "application/json");
.header(reqwest::header::ACCEPT, "application/json")
.header(reqwest::header::USER_AGENT, client.user_agent.clone());

let response = request.body(object).send().await?;

Expand Down
2 changes: 2 additions & 0 deletions crates/infisical/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Client {
pub(crate) cache_ttl: u64, // No need for a mutex lock here, as we are only reading this value in the cache thread.

pub site_url: String,
pub user_agent: String,
}

impl Client {
Expand All @@ -33,6 +34,7 @@ impl Client {

cache: Arc::new(Mutex::new(Vec::new())),
cache_ttl: settings.cache_ttl.unwrap_or(300),
user_agent: settings.user_agent.unwrap(),
};

if c.cache_ttl != 0 {
Expand Down
3 changes: 2 additions & 1 deletion crates/infisical/src/client/client_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(default, rename_all = "camelCase")]
#[cfg_attr(feature = "mobile", derive(uniffi::Record))]
pub struct ClientSettings {
// These are optional because the access token can be set directly as well
pub client_secret: Option<String>,
Expand All @@ -14,6 +13,7 @@ pub struct ClientSettings {
pub site_url: Option<String>,

pub cache_ttl: Option<u64>, // This controls how often the cache should refresh, default is 300 seconds
pub user_agent: Option<String>, // We use this to identity which SDK/language was used to make a request.
}

impl Default for ClientSettings {
Expand All @@ -24,6 +24,7 @@ impl Default for ClientSettings {
access_token: None,
site_url: None,
cache_ttl: None,
user_agent: Some("infisical-unknown-sdk".to_string()),
}
}
}
3 changes: 2 additions & 1 deletion crates/infisical/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ pub fn build_base_request(
// Setting JSON as the content type is OK since we only work with JSON.
.header(reqwest::header::CONTENT_TYPE, "application/json")
.header(reqwest::header::ACCEPT, "application/json")
.header("Authorization", token);
.header("Authorization", token)
.header(reqwest::header::USER_AGENT, client.user_agent.clone());

// we need to be able to do .json() on this request
// .json(json)
Expand Down
1 change: 1 addition & 0 deletions crates/infisical/tests/cryptography.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn create_client() -> Client {
access_token: None,
site_url: Some(environment.site_url),
cache_ttl: None,
user_agent: Some("infisical-cryptography-test-sdk".to_string()),
};

let client = Client::new(Some(settings));
Expand Down
1 change: 1 addition & 0 deletions crates/infisical/tests/secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ fn create_client() -> Client {
access_token: None,
site_url: Some(environment.site_url),
cache_ttl: None,
user_agent: Some("infisical-secrets-test-sdk".to_string()),
};

let client = Client::new(Some(settings));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class InfisicalClient implements AutoCloseable {

public InfisicalClient(ClientSettings settings) {

settings.setUserAgent("infisical-java-sdk");

library = Native.load("infisical_c", InfisicalLibrary.class);

try {
Expand Down
3 changes: 3 additions & 0 deletions languages/node/src/infisical_client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export class InfisicalClient {
#client: rust.InfisicalClient;

constructor(settings: ClientSettings & { logLevel?: LogLevel }) {

settings.userAgent = "infisical-nodejs-sdk";

const settingsJson = settings == null ? null : Convert.clientSettingsToJson(settings);
this.#client = new rust.InfisicalClient(settingsJson, settings.logLevel ?? LogLevel.Error);
}
Expand Down

0 comments on commit 984641c

Please sign in to comment.