Skip to content

Commit

Permalink
feat(updater): add Builder::header and Builder::headers (#2235)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Dec 24, 2024
1 parent c4d50aa commit 57efb47
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changes/updater-builder-header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"updater": "patch"
"updater-js": "patch"
---

Add `Builder::header` and `Builder::headers` method to configure default headers for updater.

29 changes: 28 additions & 1 deletion plugins/updater/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -74,13 +75,15 @@ impl<R: Runtime, T: Manager<R>> UpdaterExt<R> for T {
config,
target,
version_comparator,
headers,
} = self.state::<UpdaterState>().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);
Expand Down Expand Up @@ -124,13 +127,15 @@ struct UpdaterState {
target: Option<String>,
config: Config,
version_comparator: Option<VersionComparator>,
headers: HeaderMap,
}

#[derive(Default)]
pub struct Builder {
target: Option<String>,
pubkey: Option<String>,
installer_args: Vec<OsString>,
headers: HeaderMap,
default_version_comparator: Option<VersionComparator>,
}

Expand Down Expand Up @@ -172,6 +177,26 @@ impl Builder {
self
}

pub fn header<K, V>(mut self, key: K, value: V) -> Result<Self>
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<http::Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
{
let key: std::result::Result<HeaderName, http::Error> = key.try_into().map_err(Into::into);
let value: std::result::Result<HeaderValue, http::Error> =
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,
>(
Expand All @@ -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::<R, Config>::new("updater")
.setup(move |app, api| {
let mut config = api.config().clone();
Expand All @@ -200,6 +226,7 @@ impl Builder {
target,
config,
version_comparator,
headers,
});
Ok(())
})
Expand Down
10 changes: 10 additions & 0 deletions plugins/updater/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 57efb47

Please sign in to comment.