opendal_core/services/http/
config.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;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::HttpBuilder;
24
25/// Config for Http service support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct HttpConfig {
30    /// endpoint of this backend
31    pub endpoint: Option<String>,
32    /// username of this backend
33    pub username: Option<String>,
34    /// password of this backend
35    pub password: Option<String>,
36    /// token of this backend
37    pub token: Option<String>,
38    /// root of this backend
39    pub root: Option<String>,
40}
41
42impl Debug for HttpConfig {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        f.debug_struct("HttpConfig")
45            .field("endpoint", &self.endpoint)
46            .field("root", &self.root)
47            .finish_non_exhaustive()
48    }
49}
50
51impl crate::Configurator for HttpConfig {
52    type Builder = HttpBuilder;
53
54    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
55        let mut map = uri.options().clone();
56        if let Some(authority) = uri.authority() {
57            map.insert(
58                "endpoint".to_string(),
59                format!("{}://{}", uri.scheme(), authority),
60            );
61        }
62
63        if let Some(root) = uri.root() {
64            map.insert("root".to_string(), root.to_string());
65        }
66
67        Self::from_iter(map)
68    }
69
70    #[allow(deprecated)]
71    fn into_builder(self) -> Self::Builder {
72        HttpBuilder {
73            config: self,
74            http_client: None,
75        }
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82    use crate::Configurator;
83    use crate::types::OperatorUri;
84
85    #[test]
86    fn from_uri_sets_endpoint_and_root() {
87        let uri = OperatorUri::new(
88            "http://example.com/static/assets",
89            Vec::<(String, String)>::new(),
90        )
91        .unwrap();
92
93        let cfg = HttpConfig::from_uri(&uri).unwrap();
94        assert_eq!(cfg.endpoint.as_deref(), Some("http://example.com"));
95        assert_eq!(cfg.root.as_deref(), Some("static/assets"));
96    }
97
98    #[test]
99    fn from_uri_allows_missing_authority() {
100        let uri = OperatorUri::new("http", Vec::<(String, String)>::new()).unwrap();
101
102        let cfg = HttpConfig::from_uri(&uri).unwrap();
103        assert!(cfg.endpoint.is_none());
104    }
105
106    #[test]
107    fn from_uri_preserves_query_options() {
108        let uri = OperatorUri::new(
109            "http://cdn.example.com/data?token=abc123",
110            Vec::<(String, String)>::new(),
111        )
112        .unwrap();
113        let cfg = HttpConfig::from_uri(&uri).unwrap();
114
115        assert_eq!(cfg.endpoint.as_deref(), Some("http://cdn.example.com"));
116        assert_eq!(cfg.token.as_deref(), Some("abc123"));
117    }
118
119    #[test]
120    fn from_uri_ignores_endpoint_override() {
121        let uri = OperatorUri::new(
122            "http://example.com/data",
123            vec![(
124                "endpoint".to_string(),
125                "https://cdn.example.com".to_string(),
126            )],
127        )
128        .unwrap();
129        let cfg = HttpConfig::from_uri(&uri).unwrap();
130
131        assert_eq!(cfg.endpoint.as_deref(), Some("http://example.com"));
132    }
133}