opendal_core/services/http/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::HttpBuilder;
24
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct HttpConfig {
30 pub endpoint: Option<String>,
32 pub username: Option<String>,
34 pub password: Option<String>,
36 pub token: Option<String>,
38 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}