opendal/services/pcloud/
delete.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::sync::Arc;
19
20use bytes::Buf;
21use http::StatusCode;
22
23use super::core::*;
24use super::error::parse_error;
25use super::error::PcloudError;
26use crate::raw::*;
27use crate::*;
28
29pub struct PcloudDeleter {
30    core: Arc<PcloudCore>,
31}
32
33impl PcloudDeleter {
34    pub fn new(core: Arc<PcloudCore>) -> Self {
35        Self { core }
36    }
37}
38
39impl oio::OneShotDelete for PcloudDeleter {
40    async fn delete_once(&self, path: String, _: OpDelete) -> Result<()> {
41        let resp = if path.ends_with('/') {
42            self.core.delete_folder(&path).await?
43        } else {
44            self.core.delete_file(&path).await?
45        };
46
47        let status = resp.status();
48
49        match status {
50            StatusCode::OK => {
51                let bs = resp.into_body();
52                let resp: PcloudError =
53                    serde_json::from_reader(bs.reader()).map_err(new_json_deserialize_error)?;
54                let result = resp.result;
55
56                // pCloud returns 2005 or 2009 if the file or folder is not found
57                if result != 0 && result != 2005 && result != 2009 {
58                    return Err(Error::new(ErrorKind::Unexpected, format!("{resp:?}")));
59                }
60
61                Ok(())
62            }
63            _ => Err(parse_error(resp)),
64        }
65    }
66}