use crate::{
Error,
error::normal_error,
request::{Oss, OssRequest},
};
use bytes::Bytes;
use http::Method;
use http_body_util::Full;
use super::BucketEncryption;
pub struct PutBucketEncryption {
req: OssRequest,
encryption: BucketEncryption,
}
impl PutBucketEncryption {
pub(super) fn new(oss: Oss) -> Self {
let mut req = OssRequest::new(oss, Method::PUT);
req.insert_query("encryption", "");
PutBucketEncryption {
req,
encryption: BucketEncryption::default(),
}
}
pub fn set_algorithm(mut self, algorithm: impl ToString) -> Self {
self.encryption.rule.default_sse.sse_algorithm = algorithm.to_string();
self
}
pub fn set_kms_master_key_id(mut self, key_id: impl ToString) -> Self {
self.encryption.rule.default_sse.kms_master_key_id = Some(key_id.to_string());
self
}
pub fn set_encryption(mut self, encryption: BucketEncryption) -> Self {
self.encryption = encryption;
self
}
pub async fn send(mut self) -> Result<(), Error> {
let body =
serde_xml_rs::to_string(&self.encryption).map_err(|_| Error::InvalidCharacter)?;
self.req.set_body(Full::new(Bytes::from(body)));
let response = self.req.send_to_oss()?.await?;
match response.status() {
code if code.is_success() => Ok(()),
_ => Err(normal_error(response).await),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encryption_serialization() {
let xml = serde_xml_rs::to_string(&BucketEncryption::default()).unwrap();
assert!(xml.contains("<ServerSideEncryptionConfiguration>"));
assert!(xml.contains("<SSEAlgorithm>AES256</SSEAlgorithm>"));
}
}