orchestra-toolkit 0.6.1

Client to interract with Orchestra system, uses HGTP protocol
Documentation
/* Copyright 2024-2025 LEDR Technologies Inc.
* This file is part of the Orchestra library, which helps developer use our Orchestra technology which is based on AvesTerra, owned and developped by Georgetown University, under license agreement with LEDR Technologies Inc.
*
* The Orchestra library is a free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
*
* The Orchestra library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with the Orchestra library. If not, see <https://www.gnu.org/licenses/>.
*
* If you have any questions, feedback or issues about the Orchestra library, you can contact us at support@ledr.io.
*/

use std::{collections::HashMap, mem::MaybeUninit, str::FromStr, sync::Once};

use ascii::AsciiString;
use orchestra_toolkit::*;
use time::OffsetDateTime;

/// Magic to setup the session once, and get an auth token to create entities
fn setup() -> (Session, Token) {
    static mut SINGLETON: MaybeUninit<Session> = MaybeUninit::uninit();
    static ONCE: Once = Once::new();

    dotenv::dotenv().ok();

    let auth_str = std::env::var("AVESTERRA_AUTH").expect("Getting `AVESTERRA_AUTH` from env");
    let auth = Token::from_str(&auth_str).expect("Parsing `AVESTERRA_AUTH` found in env");

    unsafe {
        ONCE.call_once(|| {
            let config = SessionConfig::default();
            SINGLETON.write(Session::initialize(config).unwrap());
        });

        return (SINGLETON.assume_init_ref().clone(), auth);
    }
}

#[test]
fn retrieve_entity() {
    let (session, auth) = setup();

    let e = session
        .create_general(String255::NULL, auth)
        .call()
        .unwrap();
    session
        .set_property(
            e,
            String255::unchecked("My property key"),
            String255::unchecked("My property name"),
            Value::Text("My property value".to_owned()),
            auth,
        )
        .with_deferred(true)
        .call()
        .unwrap();

    session
        .set_fact(e, Attribute::Id, Value::Integer(42), auth)
        .with_deferred(true)
        .call()
        .unwrap();

    session
        .set_facet(
            e,
            Attribute::Date,
            String255::unchecked("The facet name"),
            Value::Time(OffsetDateTime::from_unix_timestamp(1_546_300_800).unwrap()),
            auth,
        )
        .with_deferred(true)
        .call()
        .unwrap();

    session
        .set_factor(
            e,
            Attribute::City,
            String255::unchecked("New York City"),
            String255::unchecked("Population"),
            Value::Integer(8_623_000),
            auth,
        )
        .with_deferred(true)
        .call()
        .unwrap();

    session
        .set_feature(
            e,
            Attribute::Sku,
            String255::unchecked("The feature key"),
            String255::unchecked("The feature name"),
            Value::String(AsciiString::from_str("The feature value").unwrap()),
            auth,
        )
        .with_deferred(true)
        .call()
        .unwrap();

    session
        .set_field(
            e,
            Attribute::Table,
            String255::unchecked("The only field name"),
            Value::Text("The only field default value".to_owned()),
            auth,
        )
        .with_deferred(true)
        .call()
        .unwrap();

    session
        .set_frame(
            e,
            Attribute::Table,
            String255::unchecked("The frame key"),
            String255::unchecked("The only field name"),
            Value::Integer(98),
            auth,
        )
        .with_deferred(true)
        .call()
        .unwrap();

    let model = session.retrieve_entity(e, auth).call().unwrap();

    assert_eq!(
        model,
        AvialModel {
            name: String255::NULL,
            key: String255::NULL,
            data: 0,
            attributes: vec![],
            properties: vec![ModelProperty {
                key: String255::unchecked("My property key"),
                name: String255::unchecked("My property name"),
                value: Value::Text("My property value".to_owned()),
                annotations: HashMap::new(),
            },],
            facts: vec![
                ModelFact {
                    attribute: Attribute::Id,
                    value: Value::Integer(42),
                    ..Default::default()
                },
                ModelFact {
                    attribute: Attribute::Date,
                    facets: vec![Facet {
                        name: String255::unchecked("The facet name"),
                        value: Value::Time(
                            OffsetDateTime::from_unix_timestamp(1_546_300_800).unwrap()
                        ),
                        factors: vec![],
                    }],
                    ..Default::default()
                },
                ModelFact {
                    attribute: Attribute::City,
                    facets: vec![Facet {
                        name: String255::unchecked("New York City"),
                        factors: vec![ModelFactor {
                            key: String255::unchecked("Population"),
                            value: Value::Integer(8_623_000),
                        },],
                        ..Default::default()
                    }],
                    ..Default::default()
                },
                ModelFact {
                    attribute: Attribute::Sku,
                    features: vec![ModelFeature {
                        key: String255::unchecked("The feature key"),
                        name: String255::unchecked("The feature name"),
                        value: Value::String(AsciiString::from_str("The feature value").unwrap()),
                    }],
                    ..Default::default()
                },
                ModelFact {
                    attribute: Attribute::Table,
                    fields: vec![ModelField {
                        name: String255::unchecked("The only field name"),
                        default_value: Value::Text("The only field default value".to_owned()),
                    }],
                    frames: vec![ModelFrame {
                        key: String255::unchecked("The frame key"),
                        values: vec![Value::Integer(98)],
                    }],
                    ..Default::default()
                }
            ],
        }
    )
}