Skip to content

Commit

Permalink
refactor: remove duplicated sent_patch_check_request code (#182)
Browse files Browse the repository at this point in the history
* refactor: remove duplicated sent_patch_check_request code

* maintain debug log
  • Loading branch information
bryanoltman authored Jul 12, 2024
1 parent eac1158 commit 3cae153
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 33 deletions.
33 changes: 3 additions & 30 deletions library/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use std::io::Write;
use std::path::Path;
use std::string::ToString;

use crate::cache::UpdaterState;
use crate::config::{current_arch, current_platform, UpdateConfig};
use crate::config::UpdateConfig;
use crate::events::PatchEvent;

// https://stackoverflow.com/questions/67087597/is-it-possible-to-use-rusts-log-info-for-tests
Expand Down Expand Up @@ -64,9 +63,11 @@ pub fn patch_check_request_default(
url: &str,
request: PatchCheckRequest,
) -> anyhow::Result<PatchCheckResponse> {
info!("Sending patch check request: {:?}", request);
let client = reqwest::blocking::Client::new();
let result = client.post(url).json(&request).send();
let response = handle_network_result(result)?.json()?;
debug!("Patch check response: {:?}", response);
Ok(response)
}

Expand Down Expand Up @@ -198,34 +199,6 @@ pub struct PatchCheckResponse {
pub patch: Option<Patch>,
}

pub fn send_patch_check_request(
config: &UpdateConfig,
state: &UpdaterState,
) -> anyhow::Result<PatchCheckResponse> {
let latest_patch_number = state.latest_seen_patch_number();

// Send the request to the server.
let request = PatchCheckRequest {
app_id: config.app_id.clone(),
channel: config.channel.clone(),
release_version: config.release_version.clone(),
patch_number: latest_patch_number,
platform: current_platform().to_string(),
arch: current_arch().to_string(),
};
// Dumping the request should be info! since we direct users to look for it
// in the logs: https://docs.shorebird.dev/troubleshooting#how-to-fix-it-1
// Another option would be to make verbosity configurable via a key
// in shorebird.yaml.
info!("Sending patch check request: {:?}", request);
let url = &patches_check_url(&config.base_url);
let patch_check_request_fn = config.network_hooks.patch_check_request_fn;
let response = patch_check_request_fn(url, request)?;

debug!("Patch check response: {:?}", response);
Ok(response)
}

pub fn send_patch_event(event: PatchEvent, config: &UpdateConfig) -> anyhow::Result<()> {
let request = CreatePatchEventRequest { event };

Expand Down
7 changes: 4 additions & 3 deletions library/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use crate::config::{current_arch, current_platform, set_config, with_config, Upd
use crate::events::{EventType, PatchEvent};
use crate::logging::init_logging;
use crate::network::{
download_to_path, patches_check_url, send_patch_check_request, NetworkHooks, PatchCheckRequest,
PatchCheckResponse,
download_to_path, patches_check_url, NetworkHooks, PatchCheckRequest, PatchCheckResponse,
};
use crate::time;
use crate::updater_lock::{with_updater_thread_lock, UpdaterLockState};
Expand Down Expand Up @@ -295,7 +294,9 @@ fn update_internal(_: &UpdaterLockState) -> anyhow::Result<UpdateStatus> {
})?;

// Check for update.
let response = send_patch_check_request(&config, &read_only_state)?;
let request = patch_check_request(&config, &read_only_state);
let patch_check_request_fn = &(config.network_hooks.patch_check_request_fn);
let response = patch_check_request_fn(&patches_check_url(&config.base_url), request)?;
if !response.patch_available {
return Ok(UpdateStatus::NoUpdate);
}
Expand Down

0 comments on commit 3cae153

Please sign in to comment.