opendal/services/vercel_artifacts/
builder.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::fmt::Formatter;
20use std::sync::Arc;
21
22use super::backend::VercelArtifactsBackend;
23use super::core::VercelArtifactsCore;
24use crate::raw::HttpClient;
25use crate::raw::{Access, AccessorInfo};
26use crate::services::VercelArtifactsConfig;
27use crate::Scheme;
28use crate::*;
29
30impl Configurator for VercelArtifactsConfig {
31    type Builder = VercelArtifactsBuilder;
32
33    #[allow(deprecated)]
34    fn into_builder(self) -> Self::Builder {
35        VercelArtifactsBuilder {
36            config: self,
37            http_client: None,
38        }
39    }
40}
41
42/// [Vercel Cache](https://vercel.com/docs/concepts/monorepos/remote-caching) backend support.
43#[doc = include_str!("docs.md")]
44#[derive(Default)]
45pub struct VercelArtifactsBuilder {
46    config: VercelArtifactsConfig,
47
48    #[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
49    http_client: Option<HttpClient>,
50}
51
52impl Debug for VercelArtifactsBuilder {
53    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54        let mut d = f.debug_struct("VercelArtifactsBuilder");
55        d.field("config", &self.config);
56        d.finish_non_exhaustive()
57    }
58}
59
60impl VercelArtifactsBuilder {
61    /// set the bearer access token for Vercel
62    ///
63    /// default: no access token, which leads to failure
64    pub fn access_token(mut self, access_token: &str) -> Self {
65        self.config.access_token = Some(access_token.to_string());
66        self
67    }
68
69    /// Specify the http client that used by this service.
70    ///
71    /// # Notes
72    ///
73    /// This API is part of OpenDAL's Raw API. `HttpClient` could be changed
74    /// during minor updates.
75    #[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
76    #[allow(deprecated)]
77    pub fn http_client(mut self, http_client: HttpClient) -> Self {
78        self.http_client = Some(http_client);
79        self
80    }
81}
82
83impl Builder for VercelArtifactsBuilder {
84    const SCHEME: Scheme = Scheme::VercelArtifacts;
85    type Config = VercelArtifactsConfig;
86
87    fn build(self) -> Result<impl Access> {
88        let info = AccessorInfo::default();
89        info.set_scheme(Scheme::VercelArtifacts)
90            .set_native_capability(Capability {
91                stat: true,
92                stat_has_cache_control: true,
93                stat_has_content_length: true,
94                stat_has_content_type: true,
95                stat_has_content_encoding: true,
96                stat_has_content_range: true,
97                stat_has_etag: true,
98                stat_has_content_md5: true,
99                stat_has_last_modified: true,
100                stat_has_content_disposition: true,
101
102                read: true,
103
104                write: true,
105
106                shared: true,
107
108                ..Default::default()
109            });
110
111        // allow deprecated api here for compatibility
112        #[allow(deprecated)]
113        if let Some(client) = self.http_client {
114            info.update_http_client(|_| client);
115        }
116
117        match self.config.access_token.clone() {
118            Some(access_token) => Ok(VercelArtifactsBackend {
119                core: Arc::new(VercelArtifactsCore {
120                    info: Arc::new(info),
121                    access_token,
122                }),
123            }),
124            None => Err(Error::new(ErrorKind::ConfigInvalid, "access_token not set")),
125        }
126    }
127}