From 9ab417882cfff5ab89dd00417087e76b1312dc95 Mon Sep 17 00:00:00 2001 From: Bryan Oltman Date: Tue, 23 Jul 2024 13:48:43 -0400 Subject: [PATCH] refactor: make patch_check_request a constructor (#196) --- library/src/network.rs | 14 +++++++++++++- library/src/updater.rs | 17 +++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/library/src/network.rs b/library/src/network.rs index 0679693..d1cfa1f 100644 --- a/library/src/network.rs +++ b/library/src/network.rs @@ -8,7 +8,7 @@ use std::io::Write; use std::path::Path; use std::string::ToString; -use crate::config::UpdateConfig; +use crate::config::{current_arch, current_platform, UpdateConfig}; use crate::events::PatchEvent; // https://stackoverflow.com/questions/67087597/is-it-possible-to-use-rusts-log-info-for-tests @@ -181,6 +181,18 @@ pub struct PatchCheckRequest { // know what the latest available patch is. } +impl PatchCheckRequest { + pub fn new(config: &UpdateConfig) -> PatchCheckRequest { + PatchCheckRequest { + app_id: config.app_id.clone(), + channel: config.channel.clone(), + release_version: config.release_version.clone(), + platform: current_platform().to_string(), + arch: current_arch().to_string(), + } + } +} + /// The request body for the create patch install event endpoint. /// /// We may want to consider making this more generic if/when we add more events diff --git a/library/src/updater.rs b/library/src/updater.rs index 2f96b80..068e284 100644 --- a/library/src/updater.rs +++ b/library/src/updater.rs @@ -9,7 +9,7 @@ use anyhow::{bail, Context, Result}; use dyn_clone::DynClone; use crate::cache::{PatchInfo, UpdaterState}; -use crate::config::{current_arch, current_platform, set_config, with_config, UpdateConfig}; +use crate::config::{set_config, with_config, UpdateConfig}; use crate::events::{EventType, PatchEvent}; use crate::logging::init_logging; use crate::network::{ @@ -234,22 +234,11 @@ pub fn should_auto_update() -> anyhow::Result { with_config(|config| Ok(config.auto_update)) } -fn patch_check_request(config: &UpdateConfig) -> PatchCheckRequest { - // Send the request to the server. - PatchCheckRequest { - app_id: config.app_id.clone(), - channel: config.channel.clone(), - release_version: config.release_version.clone(), - platform: current_platform().to_string(), - arch: current_arch().to_string(), - } -} - fn check_for_update_internal() -> anyhow::Result { let (request, url, request_fn) = with_config(|config| { // Get the required info to make the request. Ok(( - patch_check_request(config), + PatchCheckRequest::new(config), patches_check_url(&config.base_url), config.network_hooks.patch_check_request_fn, )) @@ -354,7 +343,7 @@ fn update_internal(_: &UpdaterLockState) -> anyhow::Result { error!("Failed to clear events: {:?}", err); } // Update our outer state with the new state. - Ok(patch_check_request(&config)) + Ok(PatchCheckRequest::new(&config)) })?; // Check for update.