opendal/services/vercel_artifacts/
backend.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 http::Response;
21use http::StatusCode;
22
23use super::core::VercelArtifactsCore;
24use super::error::parse_error;
25use super::writer::VercelArtifactsWriter;
26use crate::raw::*;
27use crate::*;
28
29#[doc = include_str!("docs.md")]
30#[derive(Clone, Debug)]
31pub struct VercelArtifactsBackend {
32    pub core: Arc<VercelArtifactsCore>,
33}
34
35impl Access for VercelArtifactsBackend {
36    type Reader = HttpBody;
37    type Writer = oio::OneShotWriter<VercelArtifactsWriter>;
38    type Lister = ();
39    type Deleter = ();
40
41    fn info(&self) -> Arc<AccessorInfo> {
42        self.core.info.clone()
43    }
44
45    async fn stat(&self, path: &str, _args: OpStat) -> Result<RpStat> {
46        let response = self.core.vercel_artifacts_stat(path).await?;
47
48        let status = response.status();
49
50        match status {
51            StatusCode::OK => {
52                let meta = parse_into_metadata(path, response.headers())?;
53                Ok(RpStat::new(meta))
54            }
55
56            _ => Err(parse_error(response)),
57        }
58    }
59
60    async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
61        let response = self
62            .core
63            .vercel_artifacts_get(path, args.range(), &args)
64            .await?;
65
66        let status = response.status();
67
68        match status {
69            StatusCode::OK | StatusCode::PARTIAL_CONTENT => {
70                Ok((RpRead::new(), response.into_body()))
71            }
72            _ => {
73                let (part, mut body) = response.into_parts();
74                let buf = body.to_buffer().await?;
75                Err(parse_error(Response::from_parts(part, buf)))
76            }
77        }
78    }
79
80    async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
81        Ok((
82            RpWrite::default(),
83            oio::OneShotWriter::new(VercelArtifactsWriter::new(
84                self.core.clone(),
85                args,
86                path.to_string(),
87            )),
88        ))
89    }
90}