genius-core-client 0.4.0

Genius Core Client Library. Written in Rust and using PyO3 for Python bindings.
Documentation
use genius_core_client::{
    client::{Client, Protocol},
    types::entity::HSMLEntity,
    CollisionStrategy,
};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Environment variables to configure client
    let genius_db_url = env::var("GENIUS_DB_URL").unwrap();
    let genius_db_port = env::var("GENIUS_DB_PORT").unwrap();
    let token = env::var("GENIUS_JWT").unwrap();

    // Initialize client
    let mut client =
        Client::new_with_oauth2_token(Protocol::HTTPS, genius_db_url, genius_db_port, token, None)
            .await?;

    // Create hardcoded swids to avoid multiple entities with the same name
    // which would cause a collision error
    // You can otherwise make these swids with genius_core_client::utils::make_swid("entity")
    let hello_entity_swid = "swid:entity:XdK5KRN6OtUd6-HQEp2-t".to_string();
    let mut hello_entity = HSMLEntity::new(hello_entity_swid.clone());
    let world_entity_swid = "swid:entity:QhmTxLrXfiqVRgySNUH4T".to_string();
    let mut world_entity = HSMLEntity::new(world_entity_swid.clone());

    // Give entities names and descriptions
    hello_entity.name = Some("hello_example".to_string());
    hello_entity.description = Some("Hello".to_string());
    world_entity.name = Some("world_example".to_string());
    world_entity.description = Some("World!".to_string());

    // Create link
    let link_swid = "swid:link:iU66SCBZ4JzzfHNbi0WfR".to_string();
    let link = HSMLEntity::new_link(
        link_swid.clone(),
        // Destination swids
        &[world_entity_swid.clone()],
        // Source swids
        &[hello_entity_swid.clone()],
        None,
    );

    // Upsert entities (including links)
    client
        .upsert(
            vec![hello_entity, world_entity, link],
            CollisionStrategy::Overwrite,
        )
        .await?;

    // Query for entities by name
    let hello_entity = client.query_for_entity("hello_example").await?;
    let world_entity = client.query_for_entity("world_example").await?;

    // Print entities
    println!("Hello Entity:\n{:#?}", hello_entity);
    println!("World Entity:\n{:#?}", world_entity);

    // Query for entities that are linked to the hello_example entity
    let links_to_hello = client.query_for_entity_array("<-- hello_example").await?;

    // Iterate through entities that are linked to the hello_example entity
    for entity in &links_to_hello.entities {
        // Print links to the hello_example entity
        println!("Links to Hello Entity:\n{:#?}", entity);
        // Get the destination swid of the link (world_example entity swid)
        let destination_swid = entity
            .as_link()
            .expect("Entity is not a link")
            .destination_swid
            .as_array()
            .expect("Destination swid is not an array")
            .first()
            .expect("Destination swid array is empty")
            .as_str()
            .expect("Destination swid is not a string");
        // Query for the destination entity
        let world = client
            .query_for_entity(destination_swid.to_string())
            .await?;
        // Print entities that are linked to the hello_example entity
        println!("Destination Entity linked to Hello:\n{:#?}", world);
    }

    Ok(())
}