reactive_graph_client/client/plugin/
api.rs

1use crate::client::plugin::mutations::restart::mutations::restart;
2use crate::client::plugin::mutations::start::mutations::start;
3use crate::client::plugin::mutations::stop::mutations::stop;
4use crate::client::plugin::mutations::uninstall::mutations::uninstall;
5use crate::client::plugin::queries::get_all::queries::get_all;
6use crate::client::plugin::queries::get_by_name::queries::get_by_name;
7use crate::client::plugin::queries::get_dependencies::queries::get_dependencies;
8use crate::client::plugin::queries::get_dependents::queries::get_dependents;
9use crate::client::plugin::queries::get_unsatisfied_dependencies::queries::get_unsatisfied_dependencies;
10use crate::client::plugin::queries::search::queries::search;
11use crate::client::plugin::variables::search::variables::SearchPluginVariables;
12use std::sync::Arc;
13
14use crate::ReactiveGraphClient;
15use crate::ReactiveGraphClientExecutionError;
16
17use reactive_graph_plugin_model::Plugin;
18
19pub struct Plugins {
20    client: Arc<ReactiveGraphClient>,
21}
22
23impl Plugins {
24    pub fn new(client: Arc<ReactiveGraphClient>) -> Self {
25        Self { client }
26    }
27
28    pub async fn get_all(&self) -> Result<Vec<Plugin>, ReactiveGraphClientExecutionError> {
29        self.client
30            .execute_plugins(get_all(), |data| data.plugins.iter().map(|plugin| plugin.into()).collect())
31            .await
32    }
33
34    pub async fn search(&self, vars: SearchPluginVariables) -> Result<Vec<Plugin>, ReactiveGraphClientExecutionError> {
35        self.client
36            .execute_plugins(search(vars), |data| data.plugins.iter().map(|plugin| plugin.into()).collect())
37            .await
38    }
39
40    /// Returns the plugin with the given name.
41    /// If no plugin was found an empty optional will be returned.
42    pub async fn get_by_name(&self, name: String) -> Result<Option<Plugin>, ReactiveGraphClientExecutionError> {
43        self.client
44            .execute_plugins(get_by_name(name), |data| data.plugins.iter().map(|plugin| plugin.into()).collect())
45            .await
46            .map(Plugins::get_first)
47    }
48
49    /// Returns the dependencies of the plugin with the given name.
50    /// If no plugin was found an empty optional will be returned.
51    pub async fn get_dependencies(&self, name: String) -> Result<Option<Vec<Plugin>>, ReactiveGraphClientExecutionError> {
52        self.client
53            .execute_plugins(get_dependencies(name), |data| data.plugins)
54            .await
55            .map(Plugins::get_first)
56            .map(|plugin| plugin.map(|plugin| plugin.dependencies))
57            .map(|plugins| plugins.map(|plugins| plugins.iter().map(|plugin| plugin.into()).collect()))
58    }
59
60    /// Returns the dependents of the plugin with the given name.
61    /// If no plugin was found an empty optional will be returned.
62    pub async fn get_dependents(&self, name: String) -> Result<Option<Vec<Plugin>>, ReactiveGraphClientExecutionError> {
63        self.client
64            .execute_plugins(get_dependents(name), |data| data.plugins)
65            .await
66            .map(Plugins::get_first)
67            .map(|plugin| plugin.map(|plugin| plugin.dependents))
68            .map(|plugins| plugins.map(|plugins| plugins.iter().map(|plugin| plugin.into()).collect()))
69    }
70
71    /// Returns the unsatisfied dependencies of the plugin with the given name.
72    /// If no plugin was found an empty optional will be returned.
73    pub async fn get_unsatisfied_dependencies(&self, name: String) -> Result<Option<Vec<Plugin>>, ReactiveGraphClientExecutionError> {
74        self.client
75            .execute_plugins(get_unsatisfied_dependencies(name), |data| data.plugins)
76            .await
77            .map(Plugins::get_first)
78            .map(|plugin| plugin.map(|plugin| plugin.unsatisfied_dependencies))
79            .map(|plugins| plugins.map(|plugins| plugins.iter().map(|plugin| plugin.into()).collect()))
80    }
81
82    pub async fn start(&self, name: String) -> Result<Plugin, ReactiveGraphClientExecutionError> {
83        self.client.execute_plugins(start(name), |data| (&data.start).into()).await
84    }
85
86    pub async fn stop(&self, name: String) -> Result<Plugin, ReactiveGraphClientExecutionError> {
87        self.client.execute_plugins(stop(name), |data| (&data.stop).into()).await
88    }
89
90    pub async fn restart(&self, name: String) -> Result<Plugin, ReactiveGraphClientExecutionError> {
91        self.client.execute_plugins(restart(name), |data| (&data.restart).into()).await
92    }
93
94    pub async fn uninstall(&self, name: String) -> Result<bool, ReactiveGraphClientExecutionError> {
95        self.client.execute_plugins(uninstall(name), |data| data.uninstall).await
96    }
97
98    fn get_first<P: Clone>(plugins: Vec<P>) -> Option<P> {
99        plugins.first().cloned()
100    }
101}