mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-06 15:57:50 +02:00
feat(http): refactor and improvements (#428)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
+41
-21
@@ -6,39 +6,56 @@
|
||||
//!
|
||||
//! Access the HTTP client written in Rust.
|
||||
|
||||
#![doc(
|
||||
html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png",
|
||||
html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
|
||||
)]
|
||||
use std::sync::atomic::AtomicU32;
|
||||
use std::{collections::HashMap, future::Future, pin::Pin};
|
||||
|
||||
use config::{Config, HttpAllowlistScope};
|
||||
pub use reqwest as client;
|
||||
pub use reqwest;
|
||||
use reqwest::Response;
|
||||
use tauri::async_runtime::Mutex;
|
||||
use tauri::{
|
||||
plugin::{Builder, TauriPlugin},
|
||||
AppHandle, Manager, Runtime,
|
||||
};
|
||||
|
||||
use std::{collections::HashMap, sync::Mutex};
|
||||
use crate::config::{Config, HttpAllowlistScope};
|
||||
pub use error::{Error, Result};
|
||||
|
||||
mod commands;
|
||||
mod config;
|
||||
mod error;
|
||||
mod scope;
|
||||
|
||||
pub use error::Error;
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
type ClientId = u32;
|
||||
type RequestId = u32;
|
||||
type CancelableResponseResult = Result<Result<reqwest::Response>>;
|
||||
type CancelableResponseFuture =
|
||||
Pin<Box<dyn Future<Output = CancelableResponseResult> + Send + Sync>>;
|
||||
type RequestTable = HashMap<RequestId, FetchRequest>;
|
||||
type ResponseTable = HashMap<RequestId, Response>;
|
||||
|
||||
pub struct Http<R: Runtime> {
|
||||
#[allow(dead_code)]
|
||||
app: AppHandle<R>,
|
||||
pub(crate) clients: Mutex<HashMap<ClientId, commands::Client>>,
|
||||
pub(crate) scope: scope::Scope,
|
||||
struct FetchRequest(Mutex<CancelableResponseFuture>);
|
||||
impl FetchRequest {
|
||||
fn new(f: CancelableResponseFuture) -> Self {
|
||||
Self(Mutex::new(f))
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Runtime> Http<R> {}
|
||||
struct Http<R: Runtime> {
|
||||
#[allow(dead_code)]
|
||||
app: AppHandle<R>,
|
||||
scope: scope::Scope,
|
||||
current_id: AtomicU32,
|
||||
requests: Mutex<RequestTable>,
|
||||
responses: Mutex<ResponseTable>,
|
||||
}
|
||||
|
||||
pub trait HttpExt<R: Runtime> {
|
||||
impl<R: Runtime> Http<R> {
|
||||
fn next_id(&self) -> RequestId {
|
||||
self.current_id
|
||||
.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
trait HttpExt<R: Runtime> {
|
||||
fn http(&self) -> &Http<R>;
|
||||
}
|
||||
|
||||
@@ -52,15 +69,18 @@ pub fn init<R: Runtime>() -> TauriPlugin<R, Option<Config>> {
|
||||
Builder::<R, Option<Config>>::new("http")
|
||||
.js_init_script(include_str!("api-iife.js").to_string())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::create_client,
|
||||
commands::drop_client,
|
||||
commands::request
|
||||
commands::fetch,
|
||||
commands::fetch_cancel,
|
||||
commands::fetch_send,
|
||||
commands::fetch_read_body,
|
||||
])
|
||||
.setup(|app, api| {
|
||||
let default_scope = HttpAllowlistScope::default();
|
||||
app.manage(Http {
|
||||
app: app.clone(),
|
||||
clients: Default::default(),
|
||||
current_id: 0.into(),
|
||||
requests: Default::default(),
|
||||
responses: Default::default(),
|
||||
scope: scope::Scope::new(
|
||||
api.config()
|
||||
.as_ref()
|
||||
|
||||
Reference in New Issue
Block a user