Skip to content

Commit 5c8b8be

Browse files
goenninggezihuzi
authored andcommitted
feat(updater): add configure_client to UpdaterBuilder (tauri-apps#2430)
1 parent b8aab43 commit 5c8b8be

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"updater": minor
3+
"updater-js": minor
4+
---
5+
6+
Add `UpdaterBuilder::configure_client` method on Rust side, to configure the `reqwest` client used to check and download the update.

plugins/updater/src/updater.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ impl RemoteRelease {
9696
}
9797

9898
pub type OnBeforeExit = Arc<dyn Fn() + Send + Sync + 'static>;
99+
pub type OnBeforeRequest = Arc<dyn Fn(ClientBuilder) -> ClientBuilder + Send + Sync + 'static>;
99100
pub type VersionComparator = Arc<dyn Fn(Version, RemoteRelease) -> bool + Send + Sync>;
100101
type MainThreadClosure = Box<dyn FnOnce() + Send + Sync + 'static>;
101102
type RunOnMainThread =
@@ -117,6 +118,7 @@ pub struct UpdaterBuilder {
117118
installer_args: Vec<OsString>,
118119
current_exe_args: Vec<OsString>,
119120
on_before_exit: Option<OnBeforeExit>,
121+
configure_client: Option<OnBeforeRequest>,
120122
}
121123

122124
impl UpdaterBuilder {
@@ -143,6 +145,7 @@ impl UpdaterBuilder {
143145
timeout: None,
144146
proxy: None,
145147
on_before_exit: None,
148+
configure_client: None,
146149
}
147150
}
148151

@@ -242,6 +245,19 @@ impl UpdaterBuilder {
242245
self
243246
}
244247

248+
/// Allows you to modify the `reqwest` client builder before the HTTP request is sent.
249+
///
250+
/// Note that `reqwest` crate may be updated in minor releases of tauri-plugin-updater.
251+
/// Therefore it's recommended to pin the plugin to at least a minor version when you're using `configure_client`.
252+
///
253+
pub fn configure_client<F: Fn(ClientBuilder) -> ClientBuilder + Send + Sync + 'static>(
254+
mut self,
255+
f: F,
256+
) -> Self {
257+
self.configure_client.replace(Arc::new(f));
258+
self
259+
}
260+
245261
pub fn build(self) -> Result<Updater> {
246262
let endpoints = self
247263
.endpoints
@@ -285,6 +301,7 @@ impl UpdaterBuilder {
285301
headers: self.headers,
286302
extract_path,
287303
on_before_exit: self.on_before_exit,
304+
configure_client: self.configure_client,
288305
})
289306
}
290307
}
@@ -319,6 +336,7 @@ pub struct Updater {
319336
headers: HeaderMap,
320337
extract_path: PathBuf,
321338
on_before_exit: Option<OnBeforeExit>,
339+
configure_client: Option<OnBeforeRequest>,
322340
#[allow(unused)]
323341
installer_args: Vec<OsString>,
324342
#[allow(unused)]
@@ -382,6 +400,11 @@ impl Updater {
382400
let proxy = reqwest::Proxy::all(proxy.as_str())?;
383401
request = request.proxy(proxy);
384402
}
403+
404+
if let Some(ref configure_client) = self.configure_client {
405+
request = configure_client(request);
406+
}
407+
385408
let response = request
386409
.build()?
387410
.get(url)
@@ -463,6 +486,7 @@ impl Updater {
463486
headers: self.headers.clone(),
464487
installer_args: self.installer_args.clone(),
465488
current_exe_args: self.current_exe_args.clone(),
489+
configure_client: self.configure_client.clone(),
466490
})
467491
} else {
468492
None
@@ -511,6 +535,7 @@ pub struct Update {
511535
installer_args: Vec<OsString>,
512536
#[allow(unused)]
513537
current_exe_args: Vec<OsString>,
538+
configure_client: Option<OnBeforeRequest>,
514539
}
515540

516541
impl Resource for Update {}
@@ -539,6 +564,9 @@ impl Update {
539564
let proxy = reqwest::Proxy::all(proxy.as_str())?;
540565
request = request.proxy(proxy);
541566
}
567+
if let Some(ref configure_client) = self.configure_client {
568+
request = configure_client(request);
569+
}
542570
let response = request
543571
.build()?
544572
.get(self.download_url.clone())

0 commit comments

Comments
 (0)