chore: Swapped over to using a macro with an offline mode

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2025-01-30 09:35:43 +11:00
parent 810fbdfe49
commit bb23e88ead
7 changed files with 17 additions and 76 deletions

View File

@ -113,10 +113,7 @@ impl Database {
prev_database,
base_url: "".to_owned(),
auth: None,
settings: Settings {
autostart: false,
max_download_threads: 4,
},
settings: Settings::default(),
cache_dir,
}
}

View File

@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
pub struct Settings {
pub autostart: bool,
pub max_download_threads: usize,
pub force_offline: bool
// ... other settings ...
}
impl Default for Settings {
@ -12,6 +13,7 @@ impl Default for Settings {
Self {
autostart: false,
max_download_threads: 4,
force_offline: false
}
}
}

View File

@ -1,9 +1,9 @@
use std::sync::Mutex;
use tauri::AppHandle;
use tauri::{AppHandle, Manager};
use crate::{
database::db::GameVersion, error::{library_error::LibraryError, remote_access_error::RemoteAccessError}, games::library::{get_current_meta, uninstall_game_logic}, AppState
database::db::GameVersion, error::{library_error::LibraryError, remote_access_error::RemoteAccessError}, games::library::{get_current_meta, uninstall_game_logic}, offline, AppState
};
use super::{
@ -16,7 +16,8 @@ use super::{
#[tauri::command]
pub fn fetch_library(app: AppHandle) -> Result<Vec<Game>, RemoteAccessError> {
fetch_library_logic(app)
let state = app.state::<Mutex<AppState>>();
offline!(state, fetch_library_logic, fetch_library_logic, app)
}
#[tauri::command]

View File

@ -69,7 +69,7 @@ pub struct StatsUpdateEvent {
pub fn fetch_library_logic(app: AppHandle) -> Result<Vec<Game>, RemoteAccessError> {
let header = generate_authorization_header();
let client = reqwest::blocking::Client::builder().;
let client = reqwest::blocking::Client::new();
let response = make_request(&client, &["/api/v1/client/user/library"], &[], |f| {
f.header("Authorization", header)
})?

View File

@ -1,4 +1,3 @@
mod cache;
mod database;
mod games;

View File

@ -1,70 +1,11 @@
use std::collections::HashMap;
use http::Version;
use reqwest::blocking::{Request, RequestBuilder, Response};
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Debug, Deserialize, Serialize)]
struct StoredResponse {
body: Vec<u8>,
headers: HashMap<String, String>,
status: u16,
url: Url,
}
// HTTP version enum in the http crate does not support serde, hence the modified copy.
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
enum HttpVersion {
#[serde(rename = "HTTP/0.9")]
Http09,
#[serde(rename = "HTTP/1.0")]
Http10,
#[serde(rename = "HTTP/1.1")]
Http11,
#[serde(rename = "HTTP/2.0")]
H2,
#[serde(rename = "HTTP/3.0")]
H3,
}
impl From<HttpVersion> for Version {
fn from(value: HttpVersion) -> Self {
match value {
HttpVersion::Http09 => Version::HTTP_09,
HttpVersion::Http10 => Version::HTTP_10,
HttpVersion::Http11 => Version::HTTP_11,
HttpVersion::H2 => Version::HTTP_2,
HttpVersion::H3 => Version::HTTP_3,
#[macro_export]
macro_rules! offline {
($var:expr, $func1:expr, $func2:expr, $( $arg:expr ),* ) => {
if borrow_db_checked().settings.offline || state.{
$func1( $( $arg ), *)
} else {
$func2( $( $arg ), *)
}
}
}
impl From<Version> for HttpVersion {
fn from(value: Version) -> Self {
match value {
Version::HTTP_09 => HttpVersion::Http09,
Version::HTTP_10 => HttpVersion::Http10,
Version::HTTP_11 => HttpVersion::Http11,
Version::HTTP_2 => HttpVersion::H2,
Version::HTTP_3 => HttpVersion::H3,
_ => unreachable!()
}
}
}
pub trait Cache {
fn send_cache(req: RequestBuilder) -> Result<Response, reqwest::Error>;
}
impl Cache for Request {
fn send_cache(req: RequestBuilder) -> Result<Response, reqwest::Error> {
let res = req.send()?;
let mut headers = HashMap::new();
for header in res.headers() {
headers.insert(header.0.as_str().to_owned(), header.1.to_str().unwrap().to_owned());
}
let status = res.status().as_u16();
let url = res.url().clone();
let version: HttpVersion = res.version().into();
let body: Vec<u8> = res.bytes()?.to_vec();
}
};
}

View File

@ -1,4 +1,5 @@
pub mod auth;
#[macro_use]
pub mod cache;
pub mod commands;
pub mod remote;