// Copyright 2019-2023 Tauri Programme within The Commons Conservancy // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT //! [![](https://github.com/tauri-apps/plugins-workspace/raw/v2/plugins/http/banner.png)](https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/http) //! //! 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 config::{Config, HttpAllowlistScope}; pub use reqwest as client; use tauri::{ plugin::{Builder, TauriPlugin}, AppHandle, Manager, Runtime, }; use std::{collections::HashMap, sync::Mutex}; mod commands; mod config; mod error; mod scope; pub use error::Error; type Result = std::result::Result; type ClientId = u32; pub struct Http { #[allow(dead_code)] app: AppHandle, pub(crate) clients: Mutex>, pub(crate) scope: scope::Scope, } impl Http {} pub trait HttpExt { fn http(&self) -> &Http; } impl> HttpExt for T { fn http(&self) -> &Http { self.state::>().inner() } } pub fn init() -> TauriPlugin> { Builder::>::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 ]) .setup(|app, api| { let default_scope = HttpAllowlistScope::default(); app.manage(Http { app: app.clone(), clients: Default::default(), scope: scope::Scope::new( api.config() .as_ref() .map(|c| &c.scope) .unwrap_or(&default_scope), ), }); Ok(()) }) .build() }