Skip to content

Commit c3f00a8

Browse files
authored
refactor: Migrate services to context based http client (#5882)
* refactor: Migrate ghac service to context based http client * refactor: Migrate koofr service to context based http client * refactor: Migrate vercel_blob service to context based http client * refactor: Migrate huggingface service to context based http client * refactor: Migrate lakefs service to context based http client * refactor: Migrate seafile service to context based http client * refactor: Migrate swift service to context based http client * refactor: Migrate webdav service to context based http client * refactor: Migrate azdls service to context based http client
1 parent 2282db5 commit c3f00a8

File tree

19 files changed

+127
-141
lines changed

19 files changed

+127
-141
lines changed

core/src/services/azdls/backend.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const KNOWN_AZDLS_ENDPOINT_SUFFIX: &[&str] = &[
4848

4949
impl Configurator for AzdlsConfig {
5050
type Builder = AzdlsBuilder;
51+
52+
#[allow(deprecated)]
5153
fn into_builder(self) -> Self::Builder {
5254
AzdlsBuilder {
5355
config: self,
@@ -61,6 +63,8 @@ impl Configurator for AzdlsConfig {
6163
#[derive(Default, Clone)]
6264
pub struct AzdlsBuilder {
6365
config: AzdlsConfig,
66+
67+
#[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
6468
http_client: Option<HttpClient>,
6569
}
6670

@@ -140,6 +144,8 @@ impl AzdlsBuilder {
140144
///
141145
/// This API is part of OpenDAL's Raw API. `HttpClient` could be changed
142146
/// during minor updates.
147+
#[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
148+
#[allow(deprecated)]
143149
pub fn http_client(mut self, client: HttpClient) -> Self {
144150
self.http_client = Some(client);
145151
self
@@ -173,15 +179,6 @@ impl Builder for AzdlsBuilder {
173179
}?;
174180
debug!("backend use endpoint {}", &endpoint);
175181

176-
let client = if let Some(client) = self.http_client {
177-
client
178-
} else {
179-
HttpClient::new().map_err(|err| {
180-
err.with_operation("Builder::build")
181-
.with_context("service", Scheme::Azdls)
182-
})?
183-
};
184-
185182
let config_loader = AzureStorageConfig {
186183
account_name: self
187184
.config
@@ -235,12 +232,17 @@ impl Builder for AzdlsBuilder {
235232
..Default::default()
236233
});
237234

235+
// allow deprecated api here for compatibility
236+
#[allow(deprecated)]
237+
if let Some(client) = self.http_client {
238+
am.update_http_client(|_| client);
239+
}
240+
238241
am.into()
239242
},
240243
filesystem: self.config.filesystem.clone(),
241244
root,
242245
endpoint,
243-
client,
244246
loader: cred_loader,
245247
signer,
246248
}),

core/src/services/azdls/core.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ pub struct AzdlsCore {
4343
pub root: String,
4444
pub endpoint: String,
4545

46-
pub client: HttpClient,
4746
pub loader: AzureStorageLoader,
4847
pub signer: AzureStorageSigner,
4948
}
@@ -93,7 +92,7 @@ impl AzdlsCore {
9392

9493
#[inline]
9594
pub async fn send(&self, req: Request<Buffer>) -> Result<Response<Buffer>> {
96-
self.client.send(req).await
95+
self.info.http_client().send(req).await
9796
}
9897
}
9998

@@ -117,7 +116,7 @@ impl AzdlsCore {
117116
let mut req = req.body(Buffer::new()).map_err(new_request_build_error)?;
118117

119118
self.sign(&mut req).await?;
120-
self.client.fetch(req).await
119+
self.info.http_client().fetch(req).await
121120
}
122121

123122
/// resource should be one of `file` or `directory`
@@ -241,7 +240,7 @@ impl AzdlsCore {
241240
let mut req = req.body(Buffer::new()).map_err(new_request_build_error)?;
242241

243242
self.sign(&mut req).await?;
244-
self.client.send(req).await
243+
self.info.http_client().send(req).await
245244
}
246245

247246
pub async fn azdls_delete(&self, path: &str) -> Result<Response<Buffer>> {

core/src/services/ghac/backend.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ fn value_or_env(
5454

5555
impl Configurator for GhacConfig {
5656
type Builder = GhacBuilder;
57+
58+
#[allow(deprecated)]
5759
fn into_builder(self) -> Self::Builder {
5860
GhacBuilder {
5961
config: self,
@@ -67,6 +69,8 @@ impl Configurator for GhacConfig {
6769
#[derive(Debug, Default)]
6870
pub struct GhacBuilder {
6971
config: GhacConfig,
72+
73+
#[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
7074
http_client: Option<HttpClient>,
7175
}
7276

@@ -127,6 +131,8 @@ impl GhacBuilder {
127131
///
128132
/// This API is part of OpenDAL's Raw API. `HttpClient` could be changed
129133
/// during minor updates.
134+
#[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
135+
#[allow(deprecated)]
130136
pub fn http_client(mut self, client: HttpClient) -> Self {
131137
self.http_client = Some(client);
132138
self
@@ -169,15 +175,6 @@ impl Builder for GhacBuilder {
169175
));
170176
}
171177

172-
let http_client = if let Some(client) = self.http_client {
173-
client
174-
} else {
175-
HttpClient::new().map_err(|err| {
176-
err.with_operation("Builder::build")
177-
.with_context("service", Scheme::Ghac)
178-
})?
179-
};
180-
181178
let core = GhacCore {
182179
info: {
183180
let am = AccessorInfo::default();
@@ -205,6 +202,13 @@ impl Builder for GhacBuilder {
205202

206203
..Default::default()
207204
});
205+
206+
// allow deprecated api here for compatibility
207+
#[allow(deprecated)]
208+
if let Some(client) = self.http_client {
209+
am.update_http_client(|_| client);
210+
}
211+
208212
am.into()
209213
},
210214
root,
@@ -218,7 +222,6 @@ impl Builder for GhacBuilder {
218222
version,
219223

220224
service_version,
221-
http_client,
222225
};
223226

224227
Ok(GhacBackend {
@@ -259,7 +262,7 @@ impl Access for GhacBackend {
259262
.header(header::RANGE, "bytes=0-0")
260263
.body(Buffer::new())
261264
.map_err(new_request_build_error)?;
262-
let resp = self.core.http_client.send(req).await?;
265+
let resp = self.core.info.http_client().send(req).await?;
263266

264267
let status = resp.status();
265268
match status {
@@ -289,7 +292,7 @@ impl Access for GhacBackend {
289292
}
290293
let req = req.body(Buffer::new()).map_err(new_request_build_error)?;
291294

292-
let resp = self.core.http_client.fetch(req).await?;
295+
let resp = self.core.info.http_client().fetch(req).await?;
293296

294297
let status = resp.status();
295298
match status {

core/src/services/ghac/core.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,21 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use super::error::parse_error;
19-
use crate::raw::{
20-
build_abs_path, new_json_deserialize_error, new_json_serialize_error, new_request_build_error,
21-
percent_encode_path, AccessorInfo, HttpClient,
22-
};
23-
use crate::*;
18+
use std::env;
19+
use std::fmt::{Debug, Formatter};
20+
use std::str::FromStr;
21+
use std::sync::Arc;
22+
2423
use ::ghac::v1 as ghac_types;
2524
use bytes::{Buf, Bytes};
2625
use http::header::{ACCEPT, AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE};
2726
use http::{Request, StatusCode, Uri};
2827
use prost::Message;
2928
use serde::{Deserialize, Serialize};
30-
use std::env;
31-
use std::fmt::{Debug, Formatter};
32-
use std::str::FromStr;
33-
use std::sync::Arc;
29+
30+
use super::error::parse_error;
31+
use crate::raw::*;
32+
use crate::*;
3433

3534
/// The base url for cache url.
3635
pub const CACHE_URL_BASE: &str = "_apis/artifactcache";
@@ -76,7 +75,6 @@ pub struct GhacCore {
7675
pub version: String,
7776

7877
pub service_version: GhacVersion,
79-
pub http_client: HttpClient,
8078
}
8179

8280
impl Debug for GhacCore {
@@ -108,7 +106,7 @@ impl GhacCore {
108106
req = req.header(ACCEPT, CACHE_HEADER_ACCEPT);
109107

110108
let req = req.body(Buffer::new()).map_err(new_request_build_error)?;
111-
let resp = self.http_client.send(req).await?;
109+
let resp = self.info.http_client().send(req).await?;
112110
let location = if resp.status() == StatusCode::OK {
113111
let slc = resp.into_body();
114112
let query_resp: GhacQueryResponse = serde_json::from_reader(slc.reader())
@@ -141,7 +139,7 @@ impl GhacCore {
141139
.header(CONTENT_LENGTH, body.len())
142140
.body(body)
143141
.map_err(new_request_build_error)?;
144-
let resp = self.http_client.send(req).await?;
142+
let resp = self.info.http_client().send(req).await?;
145143
let location = if resp.status() == StatusCode::OK {
146144
let slc = resp.into_body();
147145
let query_resp = ghac_types::GetCacheEntryDownloadUrlResponse::decode(slc)
@@ -195,7 +193,7 @@ impl GhacCore {
195193
let req = req
196194
.body(Buffer::from(Bytes::from(bs)))
197195
.map_err(new_request_build_error)?;
198-
let resp = self.http_client.send(req).await?;
196+
let resp = self.info.http_client().send(req).await?;
199197
let cache_id = if resp.status().is_success() {
200198
let slc = resp.into_body();
201199
let reserve_resp: GhacReserveResponse = serde_json::from_reader(slc.reader())
@@ -227,7 +225,7 @@ impl GhacCore {
227225
.header(CONTENT_LENGTH, body.len())
228226
.body(body)
229227
.map_err(new_request_build_error)?;
230-
let resp = self.http_client.send(req).await?;
228+
let resp = self.info.http_client().send(req).await?;
231229
let location = if resp.status() == StatusCode::OK {
232230
let (parts, slc) = resp.into_parts();
233231
let query_resp = ghac_types::CreateCacheEntryResponse::decode(slc)
@@ -263,7 +261,7 @@ impl GhacCore {
263261
.header(CONTENT_LENGTH, bs.len())
264262
.body(Buffer::from(bs))
265263
.map_err(new_request_build_error)?;
266-
let resp = self.http_client.send(req).await?;
264+
let resp = self.info.http_client().send(req).await?;
267265
if resp.status().is_success() {
268266
Ok(())
269267
} else {
@@ -291,7 +289,7 @@ impl GhacCore {
291289
.header(CONTENT_LENGTH, body.len())
292290
.body(body)
293291
.map_err(new_request_build_error)?;
294-
let resp = self.http_client.send(req).await?;
292+
let resp = self.info.http_client().send(req).await?;
295293
if resp.status() != StatusCode::OK {
296294
return Err(parse_error(resp));
297295
};

core/src/services/ghac/writer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl oio::Write for GhacWriterV1 {
176176
);
177177
let req = req.body(bs).map_err(new_request_build_error)?;
178178

179-
let resp = self.core.http_client.send(req).await?;
179+
let resp = self.core.info.http_client().send(req).await?;
180180
if !resp.status().is_success() {
181181
return Err(parse_error(resp).map(|err| err.with_operation("Backend::ghac_upload")));
182182
}

core/src/services/huggingface/backend.rs

-3
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,6 @@ impl Builder for HuggingfaceBuilder {
169169

170170
let token = self.config.token.as_ref().cloned();
171171

172-
let client = HttpClient::new()?;
173-
174172
Ok(HuggingfaceBackend {
175173
core: Arc::new(HuggingfaceCore {
176174
info: {
@@ -199,7 +197,6 @@ impl Builder for HuggingfaceBuilder {
199197
revision,
200198
root,
201199
token,
202-
client,
203200
}),
204201
})
205202
}

core/src/services/huggingface/core.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ pub struct HuggingfaceCore {
3535
pub revision: String,
3636
pub root: String,
3737
pub token: Option<String>,
38-
39-
pub client: HttpClient,
4038
}
4139

4240
impl Debug for HuggingfaceCore {
@@ -83,7 +81,7 @@ impl HuggingfaceCore {
8381
.body(Buffer::from(Bytes::from(req_body)))
8482
.map_err(new_request_build_error)?;
8583

86-
self.client.send(req).await
84+
self.info.http_client().send(req).await
8785
}
8886

8987
pub async fn hf_list(&self, path: &str, recursive: bool) -> Result<Response<Buffer>> {
@@ -120,7 +118,7 @@ impl HuggingfaceCore {
120118

121119
let req = req.body(Buffer::new()).map_err(new_request_build_error)?;
122120

123-
self.client.send(req).await
121+
self.info.http_client().send(req).await
124122
}
125123

126124
pub async fn hf_resolve(
@@ -162,7 +160,7 @@ impl HuggingfaceCore {
162160
let req = req.extension(Operation::ReaderStart);
163161
let req = req.body(Buffer::new()).map_err(new_request_build_error)?;
164162

165-
self.client.fetch(req).await
163+
self.info.http_client().fetch(req).await
166164
}
167165
}
168166

core/src/services/koofr/backend.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ use crate::*;
4040

4141
impl Configurator for KoofrConfig {
4242
type Builder = KoofrBuilder;
43+
44+
#[allow(deprecated)]
4345
fn into_builder(self) -> Self::Builder {
4446
KoofrBuilder {
4547
config: self,
@@ -54,6 +56,7 @@ impl Configurator for KoofrConfig {
5456
pub struct KoofrBuilder {
5557
config: KoofrConfig,
5658

59+
#[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
5760
http_client: Option<HttpClient>,
5861
}
5962

@@ -124,6 +127,8 @@ impl KoofrBuilder {
124127
///
125128
/// This API is part of OpenDAL's Raw API. `HttpClient` could be changed
126129
/// during minor updates.
130+
#[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
131+
#[allow(deprecated)]
127132
pub fn http_client(mut self, client: HttpClient) -> Self {
128133
self.http_client = Some(client);
129134
self
@@ -164,15 +169,6 @@ impl Builder for KoofrBuilder {
164169
.with_context("service", Scheme::Koofr)),
165170
}?;
166171

167-
let client = if let Some(client) = self.http_client {
168-
client
169-
} else {
170-
HttpClient::new().map_err(|err| {
171-
err.with_operation("Builder::build")
172-
.with_context("service", Scheme::Koofr)
173-
})?
174-
};
175-
176172
let signer = Arc::new(Mutex::new(KoofrSigner::default()));
177173

178174
Ok(KoofrBackend {
@@ -210,6 +206,12 @@ impl Builder for KoofrBuilder {
210206
..Default::default()
211207
});
212208

209+
// allow deprecated api here for compatibility
210+
#[allow(deprecated)]
211+
if let Some(client) = self.http_client {
212+
am.update_http_client(|_| client);
213+
}
214+
213215
am.into()
214216
},
215217
root,
@@ -218,7 +220,6 @@ impl Builder for KoofrBuilder {
218220
password,
219221
mount_id: OnceCell::new(),
220222
signer,
221-
client,
222223
}),
223224
})
224225
}

0 commit comments

Comments
 (0)