From 7faec60fba9c9aefbe0c1360175f7614d623bdb6 Mon Sep 17 00:00:00 2001 From: quexeky <116044207+quexeky@users.noreply.github.com> Date: Sun, 19 Jan 2025 15:09:35 +1100 Subject: [PATCH] refactor(remote): Created separate function to generate requests --- desktop/src-tauri/src/error/user_error.rs | 2 +- .../src/games/downloads/download_agent.rs | 37 +++++++------------ desktop/src-tauri/src/games/library.rs | 34 ++++++----------- desktop/src-tauri/src/remote/mod.rs | 1 + desktop/src-tauri/src/remote/requests.rs | 26 +++++++++++++ 5 files changed, 53 insertions(+), 47 deletions(-) create mode 100644 desktop/src-tauri/src/remote/requests.rs diff --git a/desktop/src-tauri/src/error/user_error.rs b/desktop/src-tauri/src/error/user_error.rs index bb019fcb..2ef151ce 100644 --- a/desktop/src-tauri/src/error/user_error.rs +++ b/desktop/src-tauri/src/error/user_error.rs @@ -5,6 +5,7 @@ use std::{ use serde::Serialize; +#[derive(Debug)] pub enum UserValue where T: Serialize, @@ -33,7 +34,6 @@ impl From> for UserValue { } } } - impl Try for UserValue { type Output = T; diff --git a/desktop/src-tauri/src/games/downloads/download_agent.rs b/desktop/src-tauri/src/games/downloads/download_agent.rs index a9da4c3a..dc6824bc 100644 --- a/desktop/src-tauri/src/games/downloads/download_agent.rs +++ b/desktop/src-tauri/src/games/downloads/download_agent.rs @@ -13,6 +13,7 @@ use crate::error::application_download_error::ApplicationDownloadError; use crate::error::remote_access_error::RemoteAccessError; use crate::games::downloads::manifest::{DropDownloadContext, DropManifest}; use crate::games::library::{on_game_complete, push_game_update, GameUpdateEvent}; +use crate::remote::requests::make_request; use crate::DB; use log::{debug, error, info}; use rayon::ThreadPoolBuilder; @@ -264,8 +265,6 @@ impl GameDownloadAgent { let completed_indexes = Arc::new(boxcar::Vec::new()); let completed_indexes_loop_arc = completed_indexes.clone(); - let base_url = DB.fetch_base_url(); - let contexts = self.contexts.lock().unwrap(); pool.scope(|scope| { let client = &reqwest::blocking::Client::new(); @@ -283,7 +282,18 @@ impl GameDownloadAgent { let sender = self.sender.clone(); - let request = generate_request(&base_url, client, context); + // TODO: Error handling + let request = make_request(&client, &[ + "/api/v1/client/chunk" + ], &[ + ("id", &context.game_id), + ("version", &context.version), + ("name", &context.file_name), + ("chunk", &context.index.to_string()), + ], + |r| { + r.header("Authorization", generate_authorization_header()) + }).unwrap(); scope.spawn(move |_| { match download_game_chunk(context, &self.control_flag, progress_handle, request) @@ -338,27 +348,6 @@ impl GameDownloadAgent { } } -fn generate_request( - base_url: &url::Url, - client: reqwest::blocking::Client, - context: &DropDownloadContext, -) -> reqwest::blocking::RequestBuilder { - let chunk_url = base_url - .join(&format!( - "/api/v1/client/chunk?id={}&version={}&name={}&chunk={}", - // Encode the parts we don't trust - context.game_id, - encode(&context.version), - encode(&context.file_name), - context.index - )) - .unwrap(); - - let header = generate_authorization_header(); - - client.get(chunk_url).header("Authorization", header) -} - impl Downloadable for GameDownloadAgent { fn download(&self, app_handle: &AppHandle) -> Result { *self.status.lock().unwrap() = DownloadStatus::Downloading; diff --git a/desktop/src-tauri/src/games/library.rs b/desktop/src-tauri/src/games/library.rs index ab4cc9ea..94d5cded 100644 --- a/desktop/src-tauri/src/games/library.rs +++ b/desktop/src-tauri/src/games/library.rs @@ -16,6 +16,7 @@ use crate::error::remote_access_error::RemoteAccessError; use crate::games::state::{GameStatusManager, GameStatusWithTransient}; use crate::process::process_manager::Platform; use crate::remote::auth::generate_authorization_header; +use crate::remote::requests::make_request; use crate::{AppState, DB}; #[derive(serde::Serialize)] @@ -66,7 +67,7 @@ pub struct StatsUpdateEvent { } // Game version with some fields missing and size information -#[derive(serde::Deserialize, serde::Serialize)] +#[derive(serde::Deserialize, serde::Serialize, Debug)] #[serde(rename_all = "camelCase")] pub struct GameVersionOption { version_index: usize, @@ -137,17 +138,10 @@ pub fn fetch_game_logic( return Ok(data); } - - let base_url = DB.fetch_base_url(); - - let endpoint = base_url.join(&format!("/api/v1/game/{}", id))?; - let header = generate_authorization_header(); - let client = reqwest::blocking::Client::new(); - let response = client - .get(endpoint.to_string()) - .header("Authorization", header) - .send()?; + let response = make_request(&client, &["/api/v1/game/", &id],&[], |r| { + r.header("Authorization", generate_authorization_header()) + })?.send()?; if response.status() == 404 { return Err(RemoteAccessError::GameNotFound); @@ -184,25 +178,21 @@ pub fn fetch_game_verion_options_logic( game_id: String, state: tauri::State<'_, Mutex>, ) -> Result, RemoteAccessError> { - let base_url = DB.fetch_base_url(); - - let endpoint = - base_url.join(format!("/api/v1/client/game/versions?id={}", game_id).as_str())?; - let header = generate_authorization_header(); - let client = reqwest::blocking::Client::new(); - let response = client - .get(endpoint.to_string()) - .header("Authorization", header) - .send()?; + + let response = make_request(&client, &["/api/v1/client/game/versions"], &[("id", &game_id)], |r| { + r.header("Authorization", generate_authorization_header()) + })?.send()?; if response.status() != 200 { let err = response.json().unwrap(); warn!("{:?}", err); return Err(RemoteAccessError::InvalidResponse(err)); } + let text = response.text().unwrap(); + println!("JSON Text: {}", text); - let data: Vec = response.json()?; + let data: Vec = serde_json::from_str(&text).unwrap(); let state_lock = state.lock().unwrap(); let process_manager_lock = state_lock.process_manager.lock().unwrap(); diff --git a/desktop/src-tauri/src/remote/mod.rs b/desktop/src-tauri/src/remote/mod.rs index 29d8c020..03f9ed10 100644 --- a/desktop/src-tauri/src/remote/mod.rs +++ b/desktop/src-tauri/src/remote/mod.rs @@ -1,3 +1,4 @@ pub mod auth; pub mod commands; pub mod remote; +pub mod requests; \ No newline at end of file diff --git a/desktop/src-tauri/src/remote/requests.rs b/desktop/src-tauri/src/remote/requests.rs new file mode 100644 index 00000000..a3b3fd4e --- /dev/null +++ b/desktop/src-tauri/src/remote/requests.rs @@ -0,0 +1,26 @@ +use std::ops::Deref; + +use reqwest::blocking::{Client, RequestBuilder, Response}; +use url::{ParseError, Url}; + +use crate::{database::db::DatabaseImpls, error::remote_access_error::RemoteAccessError, DB}; + +pub fn make_request<'a, T: AsRef, F: FnOnce(RequestBuilder) -> RequestBuilder>( + client: &Client, + endpoints: &[T], + params: &[(T, T)], + f: F, +) -> Result { + let mut base_url = DB.fetch_base_url(); + for endpoint in endpoints { + base_url = base_url.join(endpoint.as_ref())?; + } + { + let mut queries = base_url.query_pairs_mut(); + for (param, val) in params { + queries.append_pair(param.as_ref(), val.as_ref()); + } + } + let response = client.get(base_url); + Ok(f(response)) +}