inexor_rgf_client/client/system/command/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::sync::Arc;

use cynic::http::ReqwestExt;
use serde_json::Value;

use crate::client::system::command::queries::execute;
use crate::client::InexorRgfClient;
use crate::client::InexorRgfClientExecutionError;

#[cynic::schema_for_derives(file = r#"schema.graphql"#, module = "crate::schema::schema")]
pub mod queries {
    use crate::schema::property_instance::PropertyInstance;
    use crate::schema::scalar::Json;
    use serde_json::Value;

    #[derive(cynic::QueryVariables, Debug)]
    pub struct ExecuteCommandVariables {
        pub name: String,
        pub args: Option<Json>,
    }

    #[derive(cynic::QueryFragment, Debug)]
    #[cynic(graphql_type = "Mutation", variables = "ExecuteCommandVariables")]
    pub struct ExecuteCommand {
        pub system: MutationSystem,
    }

    #[derive(cynic::QueryFragment, Debug)]
    #[cynic(variables = "ExecuteCommandVariables")]
    pub struct MutationSystem {
        pub commands: MutationCommands,
    }

    #[derive(cynic::QueryFragment, Debug)]
    #[cynic(variables = "ExecuteCommandVariables")]
    pub struct MutationCommands {
        #[arguments(name: $name, args: $args)]
        pub execute: Option<PropertyInstance>,
    }

    pub fn execute(name: String, args: Option<Value>) -> cynic::Operation<ExecuteCommand, ExecuteCommandVariables> {
        use cynic::MutationBuilder;
        let vars = ExecuteCommandVariables {
            name,
            args: args.map(|value| value.into()),
        };
        ExecuteCommand::build(vars)
    }
}

pub struct Command {
    client: Arc<InexorRgfClient>,
}

impl Command {
    pub fn new(client: Arc<InexorRgfClient>) -> Self {
        Self { client }
    }

    pub async fn execute(&self, name: String, args: Option<Value>) -> Result<Option<Value>, InexorRgfClientExecutionError> {
        let value = self
            .client
            .client
            .post(self.client.url())
            .run_graphql(execute(name, args))
            .await
            .map_err(InexorRgfClientExecutionError::FailedToSendRequest)?
            .data
            .and_then(|data| data.system.commands.execute)
            .map(|property_instance| property_instance.value.0);
        Ok(value)
    }
}