arete_sdk/
context.rs

1use super::{Client, Consumer, DEFAULT_TIMEOUT_SECS, Error, Node, Provider};
2use crate::client::Format;
3use std::sync::Arc;
4use std::time::Duration;
5
6#[derive(Clone)]
7pub struct Context {
8    client: Client,
9    pub(crate) node: Node,
10    pub id: String,
11}
12
13impl Context {
14    pub(crate) fn new(client: Client, node: Node, id: String) -> Context {
15        Self { client, node, id }
16    }
17
18    pub fn consumer(&self, profile: &str) -> Result<Arc<Consumer>, Error> {
19        let args = vec![
20            self.node.system.id.to_string(),
21            self.node.id.clone(),
22            self.id.to_string(),
23            profile.to_string(),
24        ];
25        let mut client = self.client.clone();
26        let transaction = client.send(Format::Json, "consumers", &args)?;
27        let _res = client.wait_for_response(transaction, Duration::from_secs(DEFAULT_TIMEOUT_SECS))?;
28        Ok(Arc::new(Consumer::new(client, self.clone(), profile.to_string())))
29    }
30
31    pub fn provider(&self, profile: &str) -> Result<Arc<Provider>, Error> {
32        let args = vec![
33            self.node.system.id.to_string(),
34            self.node.id.clone(),
35            self.id.to_string(),
36            profile.to_string(),
37        ];
38        let mut client = self.client.clone();
39        let transaction = client.send(Format::Json, "providers", &args)?;
40        let _res = client.wait_for_response(transaction, Duration::from_secs(DEFAULT_TIMEOUT_SECS))?;
41        Ok(Arc::new(Provider::new(client, self.clone(), profile.to_string())))
42    }
43}