opendal/services/vercel_artifacts/
core.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::Request;
22use http::Response;
23use http::header;
24
25use crate::raw::*;
26use crate::*;
27
28pub struct VercelArtifactsCore {
29    pub info: Arc<AccessorInfo>,
30    pub(crate) access_token: String,
31}
32
33impl Debug for VercelArtifactsCore {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        f.debug_struct("VercelArtifactsCore")
36            .finish_non_exhaustive()
37    }
38}
39
40impl VercelArtifactsCore {
41    pub(crate) async fn vercel_artifacts_get(
42        &self,
43        hash: &str,
44        range: BytesRange,
45        _: &OpRead,
46    ) -> Result<Response<HttpBody>> {
47        let url: String = format!(
48            "https://api.vercel.com/v8/artifacts/{}",
49            percent_encode_path(hash)
50        );
51
52        let mut req = Request::get(&url);
53
54        if !range.is_full() {
55            req = req.header(header::RANGE, range.to_header());
56        }
57
58        let auth_header_content = format!("Bearer {}", self.access_token);
59        req = req.header(header::AUTHORIZATION, auth_header_content);
60
61        req = req.extension(Operation::Read);
62
63        let req = req.body(Buffer::new()).map_err(new_request_build_error)?;
64
65        self.info.http_client().fetch(req).await
66    }
67
68    pub(crate) async fn vercel_artifacts_put(
69        &self,
70        hash: &str,
71        size: u64,
72        body: Buffer,
73    ) -> Result<Response<Buffer>> {
74        let url = format!(
75            "https://api.vercel.com/v8/artifacts/{}",
76            percent_encode_path(hash)
77        );
78
79        let mut req = Request::put(&url);
80
81        let auth_header_content = format!("Bearer {}", self.access_token);
82        req = req.header(header::CONTENT_TYPE, "application/octet-stream");
83        req = req.header(header::AUTHORIZATION, auth_header_content);
84        req = req.header(header::CONTENT_LENGTH, size);
85
86        req = req.extension(Operation::Write);
87
88        let req = req.body(body).map_err(new_request_build_error)?;
89
90        self.info.http_client().send(req).await
91    }
92
93    pub(crate) async fn vercel_artifacts_stat(&self, hash: &str) -> Result<Response<Buffer>> {
94        let url = format!(
95            "https://api.vercel.com/v8/artifacts/{}",
96            percent_encode_path(hash)
97        );
98
99        let mut req = Request::head(&url);
100
101        let auth_header_content = format!("Bearer {}", self.access_token);
102        req = req.header(header::AUTHORIZATION, auth_header_content);
103        req = req.header(header::CONTENT_LENGTH, 0);
104
105        req = req.extension(Operation::Stat);
106
107        let req = req.body(Buffer::new()).map_err(new_request_build_error)?;
108
109        self.info.http_client().send(req).await
110    }
111}