orign 0.2.3

A globally distributed container orchestrator
Documentation
// src/commands/send_cmd.rs

use orign::config::GlobalConfig;
use orign::resources::v1::buffers::models::V1ReplayBufferData;
use reqwest::Client;
use serde_json::Value;
use std::error::Error;
use std::fs;

pub async fn send_buffer(
    buffer_name: String,
    file_path: String,
    train: Option<bool>,
) -> Result<(), Box<dyn Error>> {
    use std::io::{BufRead, BufReader};

    // Open the file and read it line by line
    let file = fs::File::open(&file_path)?;
    let reader = BufReader::new(file);

    // This will collect each JSON object (one per line) into a Vec<Value>
    let mut examples: Vec<Value> = Vec::new();
    for line in reader.lines() {
        let line_str = line?;
        let object: Value = serde_json::from_str(&line_str)?;
        examples.push(object);
    }

    // Create the ReplayBufferData object
    // (adjust ReplayBufferData to expect Vec<Value> if needed)
    let buffer_data = V1ReplayBufferData {
        examples,
        train: train,
    };

    let client = Client::new();
    let config = GlobalConfig::read()?;
    let server = config
        .server
        .unwrap_or_else(|| "http://localhost:3000".to_string());
    let api_key = config.api_key.as_deref().ok_or("API key not set")?;
    let bearer_token = format!("Bearer {}", api_key);

    println!("Sending data to buffer: {}", buffer_name);

    let url = format!("{}/v1/buffers/{}/examples", server, buffer_name);
    let response = client
        .post(&url)
        .header("Authorization", bearer_token)
        .json(&buffer_data)
        .send()
        .await?;

    if response.status().is_success() {
        let resp_json: Value = response.json().await?;
        println!("Data sent successfully:");
        println!("{}", serde_json::to_string_pretty(&resp_json)?);
    } else {
        let error_text = response.text().await?;
        eprintln!("Error sending data: {}", error_text);
    }

    Ok(())
}