feat: update request payload type to be generic and adjust test bodies to use GeminiRequest

main
Xerxes-2 2025-08-03 22:13:11 +10:00
parent 218e8421c7
commit 2d0cbeaf18
No known key found for this signature in database
GPG Key ID: 98F63C0043C51BA3
3 changed files with 17 additions and 13 deletions

View File

@ -1,6 +1,6 @@
use backon::{ExponentialBuilder, Retryable}; use backon::{ExponentialBuilder, Retryable};
use reqwest::Client; use reqwest::Client;
use serde_json::Value; use serde::Serialize;
use tokio::time::Duration; use tokio::time::Duration;
use tracing::debug; use tracing::debug;
use url::Url; use url::Url;
@ -8,13 +8,16 @@ use url::Url;
use crate::error::ValidatorError; use crate::error::ValidatorError;
use crate::types::GeminiKey; use crate::types::GeminiKey;
pub async fn send_request( pub async fn send_request<T>(
client: Client, client: Client,
api_endpoint: &Url, api_endpoint: &Url,
key: GeminiKey, key: GeminiKey,
payload: &Value, payload: &T,
max_retries: usize, max_retries: usize,
) -> Result<(), ValidatorError> { ) -> Result<(), ValidatorError>
where
T: Serialize,
{
let retry_policy = ExponentialBuilder::default() let retry_policy = ExponentialBuilder::default()
.with_max_times(max_retries) .with_max_times(max_retries)
.with_min_delay(Duration::from_secs(1)) .with_min_delay(Duration::from_secs(1))

View File

@ -19,13 +19,16 @@ pub async fn test_generate_content_api(
client, client,
&api_endpoint, &api_endpoint,
api_key.clone(), api_key.clone(),
&GENERATE_CONTENT_TEST_BODY, &*GENERATE_CONTENT_TEST_BODY,
config.max_retries, config.max_retries,
) )
.await .await
{ {
Ok(_) => { Ok(_) => {
info!("BASIC API VALID - {}... - Passed generate content API test", &api_key.as_ref()[..10]); info!(
"BASIC API VALID - {}... - Passed generate content API test",
&api_key.as_ref()[..10]
);
Ok(ValidatedKey::new(api_key)) Ok(ValidatedKey::new(api_key))
} }
Err(e) => match &e { Err(e) => match &e {
@ -60,7 +63,7 @@ pub async fn test_cache_content_api(
client, client,
&api_endpoint, &api_endpoint,
validated_key.key.clone(), validated_key.key.clone(),
&CACHE_CONTENT_TEST_BODY, &*CACHE_CONTENT_TEST_BODY,
config.max_retries, config.max_retries,
) )
.await .await
@ -88,7 +91,6 @@ pub async fn test_cache_content_api(
); );
validated_key validated_key
} }
} },
} }
} }

View File

@ -63,10 +63,10 @@ pub static GENERATE_CONTENT_TEST_BODY: LazyLock<Value> = LazyLock::new(|| {
}); });
// LazyLock for the cached content test body used in cache API validation // LazyLock for the cached content test body used in cache API validation
pub static CACHE_CONTENT_TEST_BODY: LazyLock<Value> = LazyLock::new(|| { pub static CACHE_CONTENT_TEST_BODY: LazyLock<GeminiRequest> = LazyLock::new(|| {
// Generate random text content to meet the minimum 1024 tokens requirement for cache API // Generate random text content to meet the minimum 1024 tokens requirement for cache API
let long_text = "You are an expert at analyzing transcripts.".repeat(150); let long_text = "You are an expert at analyzing transcripts.".repeat(150);
let cache_request = GeminiRequest { GeminiRequest {
model: Some("models/gemini-2.5-flash".to_string()), model: Some("models/gemini-2.5-flash".to_string()),
contents: vec![ContentPart { contents: vec![ContentPart {
parts: vec![TextPart { text: long_text }], parts: vec![TextPart { text: long_text }],
@ -74,6 +74,5 @@ pub static CACHE_CONTENT_TEST_BODY: LazyLock<Value> = LazyLock::new(|| {
}], }],
generation_config: None, generation_config: None,
ttl: Some("300s".to_string()), ttl: Some("300s".to_string()),
}; }
serde_json::to_value(cache_request).unwrap()
}); });