From 57efb47c116f880477f72f02a8e4239e88007d44 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Tue, 24 Dec 2024 17:00:12 +0200 Subject: [PATCH] feat(updater): add `Builder::header` and `Builder::headers` (#2235) --- .changes/updater-builder-header.md | 7 +++++++ plugins/updater/src/lib.rs | 29 ++++++++++++++++++++++++++++- plugins/updater/src/updater.rs | 10 ++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 .changes/updater-builder-header.md diff --git a/.changes/updater-builder-header.md b/.changes/updater-builder-header.md new file mode 100644 index 0000000000..71245aca46 --- /dev/null +++ b/.changes/updater-builder-header.md @@ -0,0 +1,7 @@ +--- +"updater": "patch" +"updater-js": "patch" +--- + +Add `Builder::header` and `Builder::headers` method to configure default headers for updater. + diff --git a/plugins/updater/src/lib.rs b/plugins/updater/src/lib.rs index f5045bebe4..b2b5c01438 100644 --- a/plugins/updater/src/lib.rs +++ b/plugins/updater/src/lib.rs @@ -13,6 +13,7 @@ use std::{ffi::OsString, sync::Arc}; +use http::{HeaderMap, HeaderName, HeaderValue}; use semver::Version; use tauri::{ plugin::{Builder as PluginBuilder, TauriPlugin}, @@ -74,13 +75,15 @@ impl> UpdaterExt for T { config, target, version_comparator, + headers, } = self.state::().inner(); let mut builder = UpdaterBuilder::new( package_info.name.clone(), package_info.version.clone(), config.clone(), - ); + ) + .headers(headers.clone()); if let Some(target) = target { builder = builder.target(target); @@ -124,6 +127,7 @@ struct UpdaterState { target: Option, config: Config, version_comparator: Option, + headers: HeaderMap, } #[derive(Default)] @@ -131,6 +135,7 @@ pub struct Builder { target: Option, pubkey: Option, installer_args: Vec, + headers: HeaderMap, default_version_comparator: Option, } @@ -172,6 +177,26 @@ impl Builder { self } + pub fn header(mut self, key: K, value: V) -> Result + where + HeaderName: TryFrom, + >::Error: Into, + HeaderValue: TryFrom, + >::Error: Into, + { + let key: std::result::Result = key.try_into().map_err(Into::into); + let value: std::result::Result = + value.try_into().map_err(Into::into); + self.headers.insert(key?, value?); + + Ok(self) + } + + pub fn headers(mut self, headers: HeaderMap) -> Self { + self.headers = headers; + self + } + pub fn default_version_comparator< F: Fn(Version, RemoteRelease) -> bool + Send + Sync + 'static, >( @@ -187,6 +212,7 @@ impl Builder { let target = self.target; let version_comparator = self.default_version_comparator; let installer_args = self.installer_args; + let headers = self.headers; PluginBuilder::::new("updater") .setup(move |app, api| { let mut config = api.config().clone(); @@ -200,6 +226,7 @@ impl Builder { target, config, version_comparator, + headers, }); Ok(()) }) diff --git a/plugins/updater/src/updater.rs b/plugins/updater/src/updater.rs index bb03e7ec52..244616ced5 100644 --- a/plugins/updater/src/updater.rs +++ b/plugins/updater/src/updater.rs @@ -179,6 +179,16 @@ impl UpdaterBuilder { Ok(self) } + pub fn headers(mut self, headers: HeaderMap) -> Self { + self.headers = headers; + self + } + + pub fn clear_headers(mut self) -> Self { + self.headers.clear(); + self + } + pub fn timeout(mut self, timeout: Duration) -> Self { self.timeout = Some(timeout); self