Skip to content

Commit

Permalink
extend tauri builder with updater_header method
Browse files Browse the repository at this point in the history
  • Loading branch information
Atrox committed Aug 16, 2023
1 parent 964d81f commit 9ea755b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/tauri-builder-updater-header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": minor:feat
---

Added `tauri::Builder::updater_header` to add headers to the tauri updater.
31 changes: 31 additions & 0 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ impl<R: Runtime> GlobalWindowEvent<R> {
#[derive(Debug, Clone, Default)]
pub(crate) struct UpdaterSettings {
pub(crate) target: Option<String>,
pub(crate) headers: Option<HashMap<http::HeaderName, http::HeaderValue>>,
}

/// The path resolver is a helper for the application-specific [`crate::api::path`] APIs.
Expand Down Expand Up @@ -1528,6 +1529,36 @@ impl<R: Runtime> Builder<R> {
self
}

/// Adds a header to the updater request.
///
/// See [`UpdateBuilder::header`](crate::updater::UpdateBuilder#method.header) for more information.
///
/// # Examples
///
/// ```
/// let mut builder = tauri::Builder::default();
/// builder = builder.updater_header("X-My-Header", "my-value").unwrap();
/// builder = builder.updater_header("X-My-Other-Header", "my-other-value").unwrap();
/// ```
#[cfg(updater)]
pub fn updater_header<K, V>(mut self, key: K, value: V) -> Result<Self, http::Error>
where
http::HeaderName: TryFrom<K>,
<http::HeaderName as TryFrom<K>>::Error: Into<http::Error>,
http::HeaderValue: TryFrom<V>,
<http::HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
{
let key = key.try_into().map_err(Into::into)?;
let value = value.try_into().map_err(Into::into)?;

self
.updater_settings
.headers
.get_or_insert(Default::default())
.insert(key, value);
Ok(self)
}

/// Change the device event filter mode.
///
/// Since the DeviceEvent capture can lead to high CPU usage for unfocused windows, [`tao`]
Expand Down
14 changes: 14 additions & 0 deletions core/tauri/src/updater/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,13 @@ pub(crate) async fn check_update_with_dialog<R: Runtime>(handle: AppHandle<R>) {
if let Some(target) = &handle.updater_settings.target {
builder = builder.target(target);
}
if let Some(headers) = &handle.updater_settings.headers {
for (key, value) in headers {
builder = builder
.header(key, value)
.expect("unwrapping because key/value are valid headers");
}
}

// check updates
match builder.build().await {
Expand Down Expand Up @@ -543,6 +550,13 @@ pub fn builder<R: Runtime>(handle: AppHandle<R>) -> UpdateBuilder<R> {
if let Some(target) = &handle.updater_settings.target {
builder = builder.target(target);
}
if let Some(headers) = &handle.updater_settings.headers {
for (key, value) in headers {
builder = builder
.header(key, value)
.expect("unwrapping because key/value are valid headers");
}
}
UpdateBuilder {
inner: builder,
events: true,
Expand Down

0 comments on commit 9ea755b

Please sign in to comment.