photonic_interface_grpc_client/
lib.rs

1use std::collections::HashSet;
2use std::sync::Arc;
3
4use anyhow::Result;
5use parking_lot::Mutex;
6use tonic::transport::{Channel, Endpoint, Uri};
7
8use photonic_interface_grpc_proto::interface_client::InterfaceClient;
9use photonic_interface_grpc_proto::{AttrInfoRequest, AttrName, InputInfoRequest, NodeInfoRequest};
10
11pub use crate::attr::{Attr, AttrId};
12pub use crate::input::{Input, InputId};
13pub use crate::node::{Node, NodeId};
14
15pub mod attr;
16pub mod input;
17pub mod node;
18pub mod values;
19
20#[cfg(feature = "python")]
21mod python;
22
23pub struct Client {
24    client: Arc<Mutex<InterfaceClient<Channel>>>,
25}
26
27impl Client {
28    pub async fn connect(uri: Uri) -> Result<Self> {
29        let channel = Endpoint::from(uri).connect_lazy();
30
31        let client = InterfaceClient::new(channel);
32        let client = Arc::new(Mutex::new(client));
33
34        return Ok(Self {
35            client,
36        });
37    }
38
39    pub async fn nodes(&self) -> Result<HashSet<NodeId>> {
40        let mut client = self.client.lock_arc();
41
42        let nodes = client.nodes(()).await?.into_inner().nodes.into_iter().map(NodeId).collect();
43
44        return Ok(nodes);
45    }
46
47    pub async fn inputs(&self) -> Result<HashSet<InputId>> {
48        let mut client = self.client.lock_arc();
49
50        let inputs = client.inputs(()).await?.into_inner().inputs.into_iter().map(InputId).collect();
51
52        return Ok(inputs);
53    }
54
55    pub async fn root(&self) -> Result<Node> {
56        let mut client = self.client.lock_arc();
57
58        let info = client.root(()).await?.into_inner();
59
60        return Ok(Node::from_node_info(self.client.clone(), info));
61    }
62
63    pub async fn node(&self, name: &NodeId) -> Result<Node> {
64        let mut client = self.client.lock_arc();
65
66        let info = client
67            .node(NodeInfoRequest {
68                name: name.0.clone(),
69            })
70            .await?
71            .into_inner();
72
73        return Ok(Node::from_node_info(self.client.clone(), info));
74    }
75
76    pub async fn attr(&self, name: &AttrId) -> Result<Attr> {
77        let mut client = self.client.lock_arc();
78
79        let info = client
80            .attr(AttrInfoRequest {
81                name: Some(AttrName {
82                    node: name.node.0.clone(),
83                    path: name.path.clone(),
84                }),
85            })
86            .await?
87            .into_inner();
88
89        return Ok(Attr::from_attr_info(self.client.clone(), info));
90    }
91
92    pub async fn input(&self, name: &InputId) -> Result<Input> {
93        let mut client = self.client.lock_arc();
94
95        let info = client
96            .input(InputInfoRequest {
97                name: name.0.clone(),
98            })
99            .await?
100            .into_inner();
101
102        return Ok(Input::from_input_info(self.client.clone(), info));
103    }
104}