nexgenomics 0.2.4

The official Rust crate for NexGenomics
Documentation

// src/threads.rs

use crate::utils;
use reqwest::{Client,Error};
use reqwest::header::{AUTHORIZATION, HeaderValue};
use chrono::{DateTime,Utc};
use serde::Deserialize;
use serde_json::{json,Value};

#[derive(Debug,Deserialize)]
pub struct Thread {
    id: String,
    title: String,
    creator: String,
    created_at: DateTime<Utc>,
}

#[derive(Debug,Deserialize)]
pub struct Message {
    creator: String,
    message: String,
    created_at: DateTime<Utc>,
}

#[derive(Debug,Deserialize)]
struct AssistantResponse {
    msg: String,
}

impl Thread {
    pub async fn new(title:&str) -> Result<Self,Box<dyn std::error::Error>> {
        let auth_token = utils::get_api_auth_token().unwrap();
        let url_stem = utils::get_api_url_stem().unwrap();
        let token = format!("Bearer {}", auth_token);
        let url = format!("{}/api/v0/thread",url_stem);


        let body = json!({
            "title": title.to_string(),
            "metadata": {},
        });

        let client = Client::new();
        let response = client
            .put(url)
            .header (AUTHORIZATION, HeaderValue::from_str(&token).unwrap())
            .json(&body)
            .send()
            .await?
            .error_for_status()?
            .json::<Value>()
            .await?;

        let tid = response.get("thread_id").ok_or("no thread id".to_string())?;
        let t2 = if let Value::String(s) = tid {
            Some(s)
        } else {
            None
        };

        Ok(Self {
            id: t2.unwrap().to_string(),
            creator: "".to_string(),
            title: title.to_string(),
            created_at: Utc::now(),
        })
    }



    pub async fn get_messages(&self) -> Result<Vec<Message>,Box<dyn std::error::Error>> {
        let auth_token = format! ("Bearer {}", utils::get_api_auth_token().unwrap());
        let url = format! ("{}/api/v0/thread/{}/messages", utils::get_api_url_stem().unwrap(), self.id);

        let client = Client::new();
        let response = client
            .get(url)
            .header (AUTHORIZATION, HeaderValue::from_str(&auth_token).unwrap())
            .send()
            .await?
            .error_for_status()?
            .json::<Vec<Message>>()
            .await?;

        Ok(response)
    }


    pub async fn post_message(&self, msg:&str) -> Result<(), Box<dyn std::error::Error>> {
        let auth_token = format! ("Bearer {}", utils::get_api_auth_token().unwrap());
        let url = format! ("{}/api/v0/thread/{}/message", utils::get_api_url_stem().unwrap(), self.id);

        let body = json!({
            "msg": msg.to_string(),
        });

        let client = Client::new();
        let _ = client
            .post(url)
            .header (AUTHORIZATION, HeaderValue::from_str(&auth_token).unwrap())
            .json(&body)
            .send()
            .await?
            .error_for_status()?;

        Ok(())
    }


    pub async fn ask_assistant(&self, optional_msg:&str) -> Result<String, Box<dyn std::error::Error>> {
        let auth_token = format! ("Bearer {}", utils::get_api_auth_token().unwrap());
        let url = format! ("{}/api/v0/thread/{}/assistant", utils::get_api_url_stem().unwrap(), self.id);

        let body = json!({
            "msg": optional_msg.to_string(),
        });

        let client = Client::new();
        let response = client
            .post(url)
            .header (AUTHORIZATION, HeaderValue::from_str(&auth_token).unwrap())
            .json(&body)
            .send()
            .await?
            .error_for_status()?
            .json::<AssistantResponse>()
            .await?;

        Ok(response.msg.to_string())
    }

}







async fn inner_get_threads() -> Result<Vec<Thread>, Error> {
    let auth_token = utils::get_api_auth_token().unwrap();
    let url_stem = utils::get_api_url_stem().unwrap();
    let token = format!("Bearer {}", auth_token);
    let url = format!("{}/api/v0/threads/list",url_stem);

    let client = Client::new();
    let response = client
        .post(url)
        .header (AUTHORIZATION, HeaderValue::from_str(&token).unwrap())
        .send()
        .await?
        .error_for_status()?;

    let p = response.json::<Vec<Thread>>().await?;
    Ok(p)
    
}

#[cfg(feature = "async")]
pub async fn get_threads() -> Result<Vec<Thread>,Error> {
    inner_get_threads().await
}


#[cfg(feature = "blocking")]
pub fn get_threads() -> Result<Vec<Thread>,Error> {
    let rt = tokio::runtime::Runtime::new()
        .expect("Failed to create Tokio runtime");
    rt.block_on(inner_get_threads())
}