Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amend Rust client api I can create store object pointing to local or remote server #1019

Open
AlexMikhalev opened this issue Nov 23, 2024 · 0 comments
Assignees
Labels
blocker Drastically limits user - Need help urgently

Comments

@AlexMikhalev
Copy link
Collaborator

I have tried to build a new example:

use atomic_lib::{agents::Agent, errors::AtomicResult, Resource, Store, Storelike};
use std::env;

/// Creates a new Class resource in an Atomic Data store.
/// Requires two environment variables:
/// - ATOMIC_SERVER_URL: The base URL of the Atomic Server (e.g., "https://atomicdata.dev")
/// - ATOMIC_AGENT_SECRET: The private key of the Agent that will create the Class
///
/// # Returns
/// - `AtomicResult<String>`: The subject URL of the newly created Class
///
/// # Example usage:
/// ```bash
/// export ATOMIC_SERVER_URL="https://localhost:9883"
/// export ATOMIC_AGENT_SECRET="secret_key_here"
/// cargo run
/// ```
fn main() -> AtomicResult<()> {
    // Initialize store with server URL from environment
    let server_url = env::var("ATOMIC_SERVER_URL")
        .map_err(|_| "ATOMIC_SERVER_URL environment variable not set")?;
    let mut store = Store::init()?;
    println!("Server URL: {}", store.get_server_url());
    // Initialize agent from secret
    let agent_secret = env::var("ATOMIC_AGENT_SECRET")
        .map_err(|_| "ATOMIC_AGENT_SECRET environment variable not set")?;
    let agent = Agent::from_secret(&agent_secret)?;
    store.set_default_agent(agent);

    // Create a new Class instance
    let mut class_resource =
        Resource::new_instance("https://atomicdata.dev/classes/Class", &store)?;

    // Set required properties for the Class
    class_resource
        // shortname is required for all Classes
        .set_shortname("shortname", "person", &store)?
        // description is required for all Classes
        .set_shortname(
            "description",
            "Represents a human being with various properties",
            &store,
        )?;

    // Save the resource to the server
    // This will create a Commit and send it to the server
    class_resource.save(&store)?;

    let subject = class_resource.get_subject();
    println!("Successfully created new Class!");
    println!("Subject URL: {}", subject);

    Ok(())
}

I can create a store object in Javascript and pass the Atomic Server URL and Agent (secret).
I can't figure out how to do it in Rust.

@AlexMikhalev AlexMikhalev added the blocker Drastically limits user - Need help urgently label Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocker Drastically limits user - Need help urgently
Projects
None yet
Development

No branches or pull requests

2 participants