pytauri_core/plugins/http/
mod.rs

1use std::{
2    error::Error,
3    fmt::{Debug, Display, Formatter},
4};
5
6use pyo3::{exceptions::PyRuntimeError, prelude::*};
7use tauri_plugin_http::{self as plugin};
8
9use crate::{ext_mod::plugin::Plugin, pytauri_plugins, tauri_runtime::Runtime, utils::TauriError};
10
11#[derive(Debug)]
12struct PluginError(plugin::Error);
13
14impl Display for PluginError {
15    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16        Display::fmt(&self.0, f)
17    }
18}
19
20impl Error for PluginError {}
21
22impl From<PluginError> for PyErr {
23    fn from(value: PluginError) -> Self {
24        match value.0 {
25            plugin::Error::Io(e) => e.into(),
26            plugin::Error::Tauri(e) => TauriError::from(e).into(),
27            plugin::Error::Utf8(e) => e.into(),
28            plugin::Error::FsError(e) => pytauri_plugins::fs::PluginError::from(e).into(),
29            e @ (plugin::Error::Json(_)
30            | plugin::Error::Network(_)
31            | plugin::Error::Http(_)
32            | plugin::Error::HttpInvalidHeaderName(_)
33            | plugin::Error::HttpInvalidHeaderValue(_)
34            | plugin::Error::UrlNotAllowed(_)
35            | plugin::Error::UrlParseError(_)
36            | plugin::Error::HttpMethod(_)
37            | plugin::Error::SchemeNotSupport(_)
38            | plugin::Error::RequestCanceled
39            | plugin::Error::DataUrlError
40            | plugin::Error::DataUrlDecodeError
41            | plugin::Error::DangerousSettings) => PyRuntimeError::new_err(e.to_string()),
42        }
43    }
44}
45
46impl From<plugin::Error> for PluginError {
47    fn from(value: plugin::Error) -> Self {
48        Self(value)
49    }
50}
51
52/// See also: [tauri_plugin_http::init]
53#[pyfunction]
54pub fn init() -> Plugin {
55    Plugin::new(Box::new(|| Box::new(plugin::init::<Runtime>())))
56}
57
58/// See also: [tauri_plugin_http]
59#[pymodule(submodule, gil_used = false)]
60pub mod http {
61    #[pymodule_export]
62    pub use super::init;
63}