opendal/services/ipmfs/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 log::debug;
19
20use super::backend::IpmfsBackend;
21use crate::raw::*;
22use crate::services::IpmfsConfig;
23use crate::*;
24
25impl Configurator for IpmfsConfig {
26 type Builder = IpmfsBuilder;
27 fn into_builder(self) -> Self::Builder {
28 IpmfsBuilder {
29 config: self,
30 http_client: None,
31 }
32 }
33}
34
35/// IPFS file system support based on [IPFS MFS](https://docs.ipfs.tech/concepts/file-systems/) API.
36///
37/// # Capabilities
38///
39/// This service can be used to:
40///
41/// - [x] read
42/// - [x] write
43/// - [x] list
44/// - [ ] presign
45/// - [ ] blocking
46///
47/// # Configuration
48///
49/// - `root`: Set the work directory for backend
50/// - `endpoint`: Customizable endpoint setting
51///
52/// You can refer to [`IpmfsBuilder`]'s docs for more information
53///
54/// # Example
55///
56/// ## Via Builder
57///
58/// ```no_run
59/// use anyhow::Result;
60/// use opendal::services::Ipmfs;
61/// use opendal::Operator;
62///
63/// #[tokio::main]
64/// async fn main() -> Result<()> {
65/// // create backend builder
66/// let mut builder = Ipmfs::default()
67/// // set the storage bucket for OpenDAL
68/// .endpoint("http://127.0.0.1:5001");
69///
70/// let op: Operator = Operator::new(builder)?.finish();
71///
72/// Ok(())
73/// }
74/// ```
75#[derive(Default, Debug)]
76pub struct IpmfsBuilder {
77 config: IpmfsConfig,
78 http_client: Option<HttpClient>,
79}
80
81impl IpmfsBuilder {
82 /// Set root for ipfs.
83 pub fn root(mut self, root: &str) -> Self {
84 self.config.root = if root.is_empty() {
85 None
86 } else {
87 Some(root.to_string())
88 };
89
90 self
91 }
92
93 /// Set endpoint for ipfs.
94 ///
95 /// Default: http://localhost:5001
96 pub fn endpoint(mut self, endpoint: &str) -> Self {
97 self.config.endpoint = if endpoint.is_empty() {
98 None
99 } else {
100 Some(endpoint.to_string())
101 };
102 self
103 }
104
105 /// Specify the http client that used by this service.
106 ///
107 /// # Notes
108 ///
109 /// This API is part of OpenDAL's Raw API. `HttpClient` could be changed
110 /// during minor updates.
111 pub fn http_client(mut self, client: HttpClient) -> Self {
112 self.http_client = Some(client);
113 self
114 }
115}
116
117impl Builder for IpmfsBuilder {
118 const SCHEME: Scheme = Scheme::Ipmfs;
119 type Config = IpmfsConfig;
120
121 fn build(self) -> Result<impl Access> {
122 let root = normalize_root(&self.config.root.unwrap_or_default());
123 debug!("backend use root {}", root);
124
125 let endpoint = self
126 .config
127 .endpoint
128 .clone()
129 .unwrap_or_else(|| "http://localhost:5001".to_string());
130
131 let client = if let Some(client) = self.http_client {
132 client
133 } else {
134 HttpClient::new().map_err(|err| {
135 err.with_operation("Builder::build")
136 .with_context("service", Scheme::Ipmfs)
137 })?
138 };
139
140 Ok(IpmfsBackend::new(root, client, endpoint))
141 }
142}