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