• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

divviup / divviup-api / 11294364682

11 Oct 2024 02:40PM UTC coverage: 55.946% (-0.04%) from 55.989%
11294364682

Pull #1340

github

web-flow
Merge c434d2a65 into 74e4ea7f8
Pull Request #1340: Upgrade eslint-plugin-react-hooks

3933 of 7030 relevant lines covered (55.95%)

103.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.58
/src/clients/aggregator_client.rs
1
use crate::{
2
    clients::{ClientConnExt, ClientError},
3
    entity::{task::ProvisionableTask, Aggregator},
4
    handler::Error,
5
};
6
use janus_messages::Time as JanusTime;
7
use serde::{de::DeserializeOwned, Serialize};
8
use trillium_client::{Client, KnownHeaderName};
9
use url::Url;
10
pub mod api_types;
11
pub use api_types::{
12
    AggregatorApiConfig, TaskCreate, TaskIds, TaskPatch, TaskResponse, TaskUploadMetrics,
13
};
14

15
const CONTENT_TYPE: &str = "application/vnd.janus.aggregator+json;version=0.1";
16

17
#[derive(Debug, Clone)]
18
pub struct AggregatorClient {
19
    client: Client,
20
    aggregator: Aggregator,
21
}
22

23
impl AsRef<Client> for AggregatorClient {
24
    fn as_ref(&self) -> &Client {
×
25
        &self.client
×
26
    }
×
27
}
28

29
impl AggregatorClient {
30
    pub fn new(client: Client, aggregator: Aggregator, bearer_token: &str) -> Self {
50✔
31
        let client = client
50✔
32
            .with_base(aggregator.api_url.clone())
50✔
33
            .with_default_header(
50✔
34
                KnownHeaderName::Authorization,
50✔
35
                format!("Bearer {bearer_token}"),
50✔
36
            )
50✔
37
            .with_default_header(KnownHeaderName::Accept, CONTENT_TYPE);
50✔
38

50✔
39
        Self { client, aggregator }
50✔
40
    }
50✔
41

42
    pub async fn get_config(
25✔
43
        client: Client,
25✔
44
        base_url: Url,
25✔
45
        token: &str,
25✔
46
    ) -> Result<AggregatorApiConfig, ClientError> {
25✔
47
        client
25✔
48
            .get(base_url)
25✔
49
            .with_request_header(KnownHeaderName::Authorization, format!("Bearer {token}"))
25✔
50
            .with_request_header(KnownHeaderName::Accept, CONTENT_TYPE)
25✔
51
            .success_or_client_error()
25✔
52
            .await?
18✔
53
            .response_json()
22✔
54
            .await
×
55
            .map_err(Into::into)
22✔
56
    }
25✔
57

58
    pub async fn get_task_ids(&self) -> Result<Vec<String>, ClientError> {
2✔
59
        let mut ids = vec![];
2✔
60
        let mut path = String::from("task_ids");
2✔
61
        loop {
62
            let TaskIds {
63
                task_ids,
6✔
64
                pagination_token,
6✔
65
            } = self.get(&path).await?;
6✔
66

67
            ids.extend(task_ids);
6✔
68

6✔
69
            match pagination_token {
6✔
70
                Some(pagination_token) => {
4✔
71
                    path = format!("task_ids?pagination_token={pagination_token}");
4✔
72
                }
4✔
73
                None => break Ok(ids),
2✔
74
            }
75
        }
76
    }
2✔
77

78
    pub async fn get_task(&self, task_id: &str) -> Result<TaskResponse, ClientError> {
5✔
79
        self.get(&format!("tasks/{task_id}")).await
5✔
80
    }
5✔
81

82
    pub async fn get_task_upload_metrics(
3✔
83
        &self,
3✔
84
        task_id: &str,
3✔
85
    ) -> Result<TaskUploadMetrics, ClientError> {
3✔
86
        self.get(&format!("tasks/{task_id}/metrics/uploads")).await
3✔
87
    }
3✔
88

89
    pub async fn create_task(&self, task: &ProvisionableTask) -> Result<TaskResponse, Error> {
14✔
90
        let task_create = TaskCreate::build(&self.aggregator, task)?;
14✔
91
        self.post("tasks", &task_create).await.map_err(Into::into)
20✔
92
    }
14✔
93

94
    pub async fn update_task_expiration(
26✔
95
        &self,
26✔
96
        task_id: &str,
26✔
97
        expiration: Option<JanusTime>,
26✔
98
    ) -> Result<TaskResponse, Error> {
26✔
99
        self.patch(
26✔
100
            &format!("tasks/{task_id}"),
26✔
101
            &TaskPatch {
26✔
102
                task_expiration: expiration,
26✔
103
            },
26✔
104
        )
26✔
105
        .await
60✔
106
        .map_err(Into::into)
26✔
107
    }
26✔
108

109
    // private below here
110

111
    async fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T, ClientError> {
14✔
112
        self.client
14✔
113
            .get(path)
14✔
114
            .success_or_client_error()
14✔
115
            .await?
10✔
116
            .response_json()
14✔
117
            .await
×
118
            .map_err(ClientError::from)
14✔
119
    }
14✔
120

121
    async fn post<T: DeserializeOwned>(
14✔
122
        &self,
14✔
123
        path: &str,
14✔
124
        body: &impl Serialize,
14✔
125
    ) -> Result<T, ClientError> {
14✔
126
        self.client
14✔
127
            .post(path)
14✔
128
            .with_json_body(body)?
14✔
129
            .with_request_header(KnownHeaderName::ContentType, CONTENT_TYPE)
14✔
130
            .success_or_client_error()
14✔
131
            .await?
20✔
132
            .response_json()
14✔
133
            .await
×
134
            .map_err(ClientError::from)
14✔
135
    }
14✔
136

137
    async fn patch<T: DeserializeOwned>(
26✔
138
        &self,
26✔
139
        path: &str,
26✔
140
        body: &impl Serialize,
26✔
141
    ) -> Result<T, ClientError> {
26✔
142
        self.client
26✔
143
            .patch(path)
26✔
144
            .with_json_body(body)?
26✔
145
            .with_request_header(KnownHeaderName::ContentType, CONTENT_TYPE)
26✔
146
            .success_or_client_error()
26✔
147
            .await?
60✔
148
            .response_json()
24✔
149
            .await
×
150
            .map_err(ClientError::from)
24✔
151
    }
26✔
152
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc