use std::sync::Arc;
use http::StatusCode;
use super::core::*;
use super::error::parse_error;
use opendal_core::raw::oio::BatchDeleteResult;
use opendal_core::raw::*;
use opendal_core::*;
pub struct GcsDeleter {
core: Arc<GcsCore>,
}
impl GcsDeleter {
pub fn new(core: Arc<GcsCore>) -> Self {
Self { core }
}
}
impl oio::BatchDelete for GcsDeleter {
async fn delete_once(&self, path: String, _: OpDelete) -> Result<()> {
let resp = self.core.gcs_delete_object(&path).await?;
if resp.status().is_success() || resp.status() == StatusCode::NOT_FOUND {
Ok(())
} else {
Err(parse_error(resp))
}
}
async fn delete_batch(&self, batch: Vec<(String, OpDelete)>) -> Result<BatchDeleteResult> {
let paths: Vec<String> = batch.into_iter().map(|(p, _)| p).collect();
let resp = self.core.gcs_delete_objects(paths.clone()).await?;
let status = resp.status();
if status != StatusCode::OK {
return Err(parse_error(resp));
}
let boundary = parse_multipart_boundary(resp.headers())?.ok_or_else(|| {
Error::new(
ErrorKind::Unexpected,
"gcs batch delete response content type is empty",
)
})?;
let multipart: Multipart<MixedPart> = Multipart::new()
.with_boundary(boundary)
.parse(resp.into_body().to_bytes())?;
let parts = multipart.into_parts();
let mut batched_result = BatchDeleteResult::default();
for (i, part) in parts.into_iter().enumerate() {
let resp = part.into_response();
let path = paths[i].clone();
if resp.status().is_success() || resp.status() == StatusCode::NOT_FOUND {
batched_result.succeeded.push((path, OpDelete::default()));
} else {
batched_result
.failed
.push((path, OpDelete::default(), parse_error(resp)));
}
}
if batched_result.succeeded.is_empty() {
let err = batched_result.failed.remove(0).2;
return Err(err);
}
Ok(batched_result)
}
}