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