Add consistent error handling to text_embeddings and count_tokens

- Check HTTP status before parsing response body, matching the
  pattern used by generate_content and predict_image
- Unwrap TextEmbeddingResponse enum, returning TextEmbeddingResponseOk
- Extract CountTokensResponseResult struct and add into_result(),
  returning the unwrapped result instead of the raw enum
- All endpoints now consistently return the success type directly
  and surface API errors as GeminiError or GenericApiError
This commit is contained in:
2026-01-30 20:32:40 +00:00
parent eb38c65ac5
commit a8fbe658bb
2 changed files with 65 additions and 12 deletions

View File

@@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::error::{Error, Result};
use super::Content;
#[derive(Debug, Serialize, Deserialize)]
@@ -38,12 +40,22 @@ impl CountTokensRequestBuilder {
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CountTokensResponse {
#[serde(rename_all = "camelCase")]
Ok {
total_tokens: i32,
total_billable_characters: u32,
},
Error {
error: super::VertexApiError,
},
Ok(CountTokensResponseResult),
Error { error: super::VertexApiError },
}
impl CountTokensResponse {
pub fn into_result(self) -> Result<CountTokensResponseResult> {
match self {
CountTokensResponse::Ok(result) => Ok(result),
CountTokensResponse::Error { error } => Err(Error::VertexError(error)),
}
}
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CountTokensResponseResult {
pub total_tokens: i32,
pub total_billable_characters: u32,
}