rship-sdk 0.1.15

rship sdk in rust!
Documentation
use crate::{
    client::SdkClient,
    target::{TargetArgs, TargetProxy},
    EventTrackArgs, EventTrackProxy,
};
use rship_entities::{
    instance::{Instance, InstanceStatus},
    machine::Machine,
    target_status::Status,
};
use std::collections::HashSet;

#[derive(Clone)]
pub struct InstanceArgs {
    pub name: String,
    pub short_id: String,
    pub code: String,
    pub service_id: String,
    pub cluster_id: Option<String>,
    pub color: String,
    pub machine_id: String,
    pub message: Option<String>,
    pub status: InstanceStatus,
}

#[derive(Clone)]
pub struct InstanceProxy {
    pub(crate) args: InstanceArgs,
    pub(crate) client: SdkClient,
}

impl InstanceProxy {
    pub async fn add_target(&self, args: TargetArgs) -> TargetProxy {
        let p = TargetProxy {
            emitters: HashSet::new(),
            actions: HashSet::new(),
            instance: self.clone(),
            args,
            client: self.client.clone(),
        };

        p.save(Status::Online).await;

        p
    }

    pub async fn add_event_track(&self, args: EventTrackArgs) -> EventTrackProxy {
        let p = EventTrackProxy {
            instance: self.clone(),
            args,
            client: self.client.clone(),
        };

        p.save().await;

        p
    }

    pub async fn save(&self) {
        // let client_id = self.client.client.get_client_id().await;
        // if client_id.is_none() {
        //     println!("Could not get client id");
        //     return;
        // }
        let instance = Instance {
            name: self.args.name.clone(),
            id: self.args.short_id.clone(),
            service_type_code: self.args.code.clone(),
            service_id: self.args.service_id.clone(),
            // client_id: None,
            cluster_id: self.args.cluster_id.clone(),
            color: self.args.color.clone(),
            hash: uuid::Uuid::new_v4().to_string(),
            machine_id: self.args.machine_id.clone(),

            message: self.args.message.clone(),
            status: self.args.status.clone(),
        };

        let machine = Machine {
            hash: uuid::Uuid::new_v4().to_string(),
            name: None,
            addresses: vec![],
            dns_name: None,
            exec_name: Some(self.args.machine_id.clone()),
            id: self.args.machine_id.clone(),
        };

        if let Err(err) = self.client.save_item(&machine).await {
            println!("Could not save machine, {}", err);
        }
        if let Err(err) = self.client.save_item(&instance).await {
            println!("Could not save instance, {}", err);
        }
    }
}