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