gemini_bridge 0.1.1

Types and functions to interact with Gemini AI API
Documentation
use anyhow::Error;
use async_trait::async_trait;
use serde_json::json;

use super::{request::RequestBody, response::GenerateContentResponse};

#[async_trait]
pub trait TextGenerator: Send + Sync {
    async fn generate_content(&self, req: RequestBody) -> Result<GenerateContentResponse, Error>;
}

pub(crate) struct TextGeneratorImpl {
    pub uri: String,
}

#[async_trait]
impl TextGenerator for TextGeneratorImpl {
    async fn generate_content(&self, req: RequestBody) -> Result<GenerateContentResponse, Error> {
        let uri = &self.uri;

        let request_body = json!(req);

        let res = reqwest::Client::new()
            .post(uri)
            .json(&request_body)
            .send()
            .await;

        print!("uri: {}", uri);

        let response_body = match res {
            Ok(response) => {
                if response.status().is_success() {
                    response.text().await?
                } else {
                    return Err(Error::msg(format!(
                        "Request failed with status: {}",
                        response.status()
                    )));
                }
            }
            Err(e) => return Err(Error::new(e)),
        };

        let result: GenerateContentResponse = match serde_json::from_str(&response_body) {
            Ok(parsed) => parsed,
            Err(e) => return Err(Error::new(e)),
        };

        Ok(result)
    }
}