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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use anyhow::Context;
use std::sync::Arc;
use tonic::transport::Channel;

use crate::client::iotics::api::describe_feed_request::Arguments as DescribeFeedRequestArguments;
use crate::client::iotics::api::describe_twin_request::Arguments as DescribeTwinRequestArguments;
use crate::client::iotics::api::feed_api_client::FeedApiClient;
use crate::client::iotics::api::twin_api_client::TwinApiClient;
use crate::client::iotics::api::{
    DescribeFeedRequest, DescribeTwinRequest, FeedId, Headers, TwinId,
};

use crate::auth_builder::IntoAuthBuilder;
use crate::channel::create_channel;
use crate::helpers::generate_client_app_id;
use crate::twin::{DescribeFeedResponse, DescribeTwinResponse};

pub async fn describe_twin(
    auth_builder: Arc<impl IntoAuthBuilder>,
    twin_id: &str,
    remote_host_id: Option<&str>,
) -> Result<DescribeTwinResponse, anyhow::Error> {
    let channel = create_channel(auth_builder.clone(), None, None, None).await?;
    describe_twin_with_channel(auth_builder, channel, twin_id, remote_host_id).await
}

pub async fn describe_twin_with_channel(
    auth_builder: Arc<impl IntoAuthBuilder>,
    channel: Channel,
    twin_id: &str,
    remote_host_id: Option<&str>,
) -> Result<DescribeTwinResponse, anyhow::Error> {
    let mut client = TwinApiClient::new(channel);
    let client_app_id = generate_client_app_id();
    let transaction_ref = vec![client_app_id.clone()];

    let headers = Headers {
        client_app_id,
        transaction_ref: transaction_ref.clone(),
        ..Default::default()
    };

    let twin_id_arg = TwinId {
        id: twin_id.to_string(),
        host_id: remote_host_id.unwrap_or_default().to_string(),
    };

    let args = DescribeTwinRequestArguments {
        twin_id: Some(twin_id_arg),
    };

    let mut request = tonic::Request::new(DescribeTwinRequest {
        headers: Some(headers),
        args: Some(args),
    });

    let token = auth_builder.get_token()?;

    request.metadata_mut().append(
        "authorization",
        token.parse().context("parse token failed")?,
    );

    let result = client.describe_twin(request).await.with_context(|| {
        format!(
            "Describing twin failed, transaction ref [{}]",
            transaction_ref.join(", ")
        )
    })?;
    let result = result.into_inner();

    Ok(result)
}

pub async fn describe_feed(
    auth_builder: Arc<impl IntoAuthBuilder>,
    twin_id: &str,
    feed_id: &str,
    remote_host_id: Option<&str>,
) -> Result<DescribeFeedResponse, anyhow::Error> {
    let channel = create_channel(auth_builder.clone(), None, None, None).await?;
    describe_feed_with_channel(auth_builder, channel, twin_id, feed_id, remote_host_id).await
}

pub async fn describe_feed_with_channel(
    auth_builder: Arc<impl IntoAuthBuilder>,
    channel: Channel,
    twin_id: &str,
    feed_id: &str,
    remote_host_id: Option<&str>,
) -> Result<DescribeFeedResponse, anyhow::Error> {
    let mut client = FeedApiClient::new(channel);
    let client_app_id = generate_client_app_id();
    let transaction_ref = vec![client_app_id.clone()];

    let headers = Headers {
        client_app_id,
        transaction_ref: transaction_ref.clone(),
        ..Default::default()
    };

    let args = DescribeFeedRequestArguments {
        feed_id: Some(FeedId {
            id: feed_id.to_string(),
            twin_id: twin_id.to_string(),
            host_id: remote_host_id.unwrap_or_default().to_string(),
        }),
    };

    let mut request = tonic::Request::new(DescribeFeedRequest {
        headers: Some(headers),
        args: Some(args),
    });

    let token = auth_builder.get_token()?;

    request.metadata_mut().append(
        "authorization",
        token.parse().context("parse token failed")?,
    );

    let result = client.describe_feed(request).await.with_context(|| {
        format!(
            "Describing feed failed, transaction ref [{}]",
            transaction_ref.join(", ")
        )
    })?;
    let result = result.into_inner();

    Ok(result)
}