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