ossify/ops/bucket/replication/
get_bucket_replication_progress.rs1use std::future::Future;
6
7use http::Method;
8use serde::{Deserialize, Serialize};
9
10use crate::body::NoneBody;
11use crate::error::Result;
12use crate::ops::common::ReplicationRule;
13use crate::response::BodyResponseProcessor;
14use crate::ser::OnlyKeyField;
15use crate::{Client, Ops, Prepared, Request};
16
17#[derive(Debug, Clone, Serialize)]
18#[serde(rename_all = "kebab-case")]
19pub struct GetBucketReplicationProgressParams {
20 #[serde(rename = "replicationProgress")]
21 replication_progress: OnlyKeyField,
22 pub rule_id: String,
23}
24
25impl GetBucketReplicationProgressParams {
26 pub fn new(rule_id: impl Into<String>) -> Self {
27 Self {
28 replication_progress: OnlyKeyField,
29 rule_id: rule_id.into(),
30 }
31 }
32}
33
34#[derive(Debug, Clone, Default, Deserialize)]
36#[serde(rename = "ReplicationProgress", rename_all = "PascalCase")]
37pub struct ReplicationProgress {
38 #[serde(rename = "Rule", default)]
39 pub rules: Vec<ReplicationRule>,
40}
41
42pub struct GetBucketReplicationProgress {
43 pub params: GetBucketReplicationProgressParams,
44}
45
46impl Ops for GetBucketReplicationProgress {
47 type Response = BodyResponseProcessor<ReplicationProgress>;
48 type Body = NoneBody;
49 type Query = GetBucketReplicationProgressParams;
50
51 fn prepare(self) -> Result<Prepared<GetBucketReplicationProgressParams>> {
52 Ok(Prepared {
53 method: Method::GET,
54 query: Some(self.params),
55 ..Default::default()
56 })
57 }
58}
59
60pub trait GetBucketReplicationProgressOps {
61 fn get_bucket_replication_progress(
65 &self,
66 rule_id: impl Into<String>,
67 ) -> impl Future<Output = Result<ReplicationProgress>>;
68}
69
70impl GetBucketReplicationProgressOps for Client {
71 async fn get_bucket_replication_progress(
72 &self,
73 rule_id: impl Into<String>,
74 ) -> Result<ReplicationProgress> {
75 self.request(GetBucketReplicationProgress {
76 params: GetBucketReplicationProgressParams::new(rule_id),
77 })
78 .await
79 }
80}
81
82#[cfg(test)]
83mod tests {
84 use super::*;
85
86 #[test]
87 fn params_serialize() {
88 let q = crate::ser::to_string(&GetBucketReplicationProgressParams::new("r-1")).unwrap();
89 assert_eq!(q, "replicationProgress&rule-id=r-1");
90 }
91
92 #[test]
93 fn parse_progress_response() {
94 let xml = r#"<ReplicationProgress>
95 <Rule>
96 <ID>test_replication_1</ID>
97 <Action>PUT</Action>
98 <Destination>
99 <Bucket>target</Bucket>
100 <Location>oss-cn-beijing</Location>
101 </Destination>
102 <Status>doing</Status>
103 <Progress>
104 <HistoricalObject>0.85</HistoricalObject>
105 <NewObject>2015-09-24T15:28:14.000Z</NewObject>
106 </Progress>
107 </Rule>
108</ReplicationProgress>"#;
109 let parsed: ReplicationProgress = quick_xml::de::from_str(xml).unwrap();
110 assert_eq!(parsed.rules.len(), 1);
111 assert_eq!(parsed.rules[0].id.as_deref(), Some("test_replication_1"));
112 let p = parsed.rules[0].progress.as_ref().unwrap();
113 assert_eq!(p.historical_object.as_deref(), Some("0.85"));
114 }
115}