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 super::DEFAULT_SCHEME;
25use crate::raw::Access;
26use crate::raw::AccessorInfo;
27use crate::raw::HttpClient;
28use crate::services::VercelArtifactsConfig;
29use crate::*;
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    type Config = VercelArtifactsConfig;
85
86    fn build(self) -> Result<impl Access> {
87        let info = AccessorInfo::default();
88        info.set_scheme(DEFAULT_SCHEME)
89            .set_native_capability(Capability {
90                stat: true,
91
92                read: true,
93
94                write: true,
95
96                shared: true,
97
98                ..Default::default()
99            });
100
101        // allow deprecated api here for compatibility
102        #[allow(deprecated)]
103        if let Some(client) = self.http_client {
104            info.update_http_client(|_| client);
105        }
106
107        match self.config.access_token.clone() {
108            Some(access_token) => Ok(VercelArtifactsBackend {
109                core: Arc::new(VercelArtifactsCore {
110                    info: Arc::new(info),
111                    access_token,
112                }),
113            }),
114            None => Err(Error::new(ErrorKind::ConfigInvalid, "access_token not set")),
115        }
116    }
117}