mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
Replace all http code in epi/eframe/egui_glium/egui_web with ehttp (#697)
I've extracted all the http request code and turned it into its own crate at <https://github.com/emilk/ehttp>. There was never a reason for the HTTP request library to be part of `eframe`. Much better to have it as its own crate!
This commit is contained in:
@@ -4,6 +4,7 @@ All notable changes to the `egui_glium` integration will be noted in this file.
|
||||
|
||||
|
||||
## Unreleased
|
||||
* Remove "http" feature (use https://github.com/emilk/ehttp instead!).
|
||||
|
||||
|
||||
## 0.14.0 - 2021-08-24
|
||||
|
||||
@@ -28,9 +28,6 @@ epi = { version = "0.14.0", path = "../epi" }
|
||||
glium = "0.30"
|
||||
webbrowser = "0.5"
|
||||
|
||||
# feature "http":
|
||||
ureq = { version = "2.0", optional = true }
|
||||
|
||||
# feature "persistence":
|
||||
directories-next = { version = "2", optional = true }
|
||||
ron = { version = "0.6", optional = true }
|
||||
@@ -48,7 +45,6 @@ default = ["default_fonts"]
|
||||
# If set, egui will use `include_bytes!` to bundle some fonts.
|
||||
# If you plan on specifying your own fonts you may disable this feature.
|
||||
default_fonts = ["egui/default_fonts"]
|
||||
http = ["epi/http", "ureq"]
|
||||
persistence = [
|
||||
"directories-next",
|
||||
"egui/persistence",
|
||||
|
||||
@@ -177,9 +177,6 @@ pub fn run(mut app: Box<dyn epi::App>, native_options: epi::NativeOptions) {
|
||||
#[allow(unused_mut)]
|
||||
let mut storage = create_storage(app.name());
|
||||
|
||||
#[cfg(feature = "http")]
|
||||
let http = std::sync::Arc::new(crate::http::GliumHttp {});
|
||||
|
||||
let window_settings = deserialize_window_settings(&storage);
|
||||
let mut event_loop = glutin::event_loop::EventLoop::with_user_event();
|
||||
let icon = native_options.icon_data.clone().and_then(load_icon);
|
||||
@@ -198,8 +195,6 @@ pub fn run(mut app: Box<dyn epi::App>, native_options: epi::NativeOptions) {
|
||||
let mut frame = epi::backend::FrameBuilder {
|
||||
info: integration_info(&display, None),
|
||||
tex_allocator: painter,
|
||||
#[cfg(feature = "http")]
|
||||
http: http.clone(),
|
||||
output: &mut app_output,
|
||||
repaint_signal: repaint_signal.clone(),
|
||||
}
|
||||
@@ -222,8 +217,6 @@ pub fn run(mut app: Box<dyn epi::App>, native_options: epi::NativeOptions) {
|
||||
let mut frame = epi::backend::FrameBuilder {
|
||||
info: integration_info(&display, None),
|
||||
tex_allocator: painter,
|
||||
#[cfg(feature = "http")]
|
||||
http: http.clone(),
|
||||
output: &mut app_output,
|
||||
repaint_signal: repaint_signal.clone(),
|
||||
}
|
||||
@@ -319,8 +312,6 @@ pub fn run(mut app: Box<dyn epi::App>, native_options: epi::NativeOptions) {
|
||||
let mut frame = epi::backend::FrameBuilder {
|
||||
info: integration_info(&display, previous_frame_time),
|
||||
tex_allocator: painter,
|
||||
#[cfg(feature = "http")]
|
||||
http: http.clone(),
|
||||
output: &mut app_output,
|
||||
repaint_signal: repaint_signal.clone(),
|
||||
}
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
pub use epi::http::{Request, Response};
|
||||
|
||||
/// NOTE: Ok(..) is returned on network error.
|
||||
/// Err is only for failure to use the fetch api.
|
||||
pub fn fetch_blocking(request: &Request) -> Result<Response, String> {
|
||||
let mut req = ureq::request(&request.method, &request.url);
|
||||
|
||||
for header in &request.headers {
|
||||
req = req.set(header.0, header.1);
|
||||
}
|
||||
|
||||
let resp = if request.body.is_empty() {
|
||||
req.call()
|
||||
} else {
|
||||
req.send_bytes(&request.body)
|
||||
};
|
||||
|
||||
let (ok, resp) = match resp {
|
||||
Ok(resp) => (true, resp),
|
||||
Err(ureq::Error::Status(_, resp)) => (false, resp), // Still read the body on e.g. 404
|
||||
Err(ureq::Error::Transport(error)) => return Err(error.to_string()),
|
||||
};
|
||||
|
||||
let url = resp.get_url().to_owned();
|
||||
let status = resp.status();
|
||||
let status_text = resp.status_text().to_owned();
|
||||
let mut headers = BTreeMap::new();
|
||||
for key in &resp.headers_names() {
|
||||
if let Some(value) = resp.header(key) {
|
||||
// lowercase for easy lookup
|
||||
headers.insert(key.to_ascii_lowercase(), value.to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
let mut reader = resp.into_reader();
|
||||
let mut bytes = vec![];
|
||||
use std::io::Read;
|
||||
reader
|
||||
.read_to_end(&mut bytes)
|
||||
.map_err(|err| err.to_string())?;
|
||||
|
||||
let response = Response {
|
||||
url,
|
||||
ok,
|
||||
status,
|
||||
status_text,
|
||||
bytes,
|
||||
headers,
|
||||
};
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
pub(crate) struct GliumHttp {}
|
||||
|
||||
impl epi::backend::Http for GliumHttp {
|
||||
fn fetch_dyn(
|
||||
&self,
|
||||
request: Request,
|
||||
on_done: Box<dyn FnOnce(Result<Response, String>) + Send>,
|
||||
) {
|
||||
std::thread::spawn(move || {
|
||||
let result = crate::http::fetch_blocking(&request);
|
||||
on_done(result)
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -19,8 +19,6 @@
|
||||
#![allow(clippy::manual_range_contains, clippy::single_match)]
|
||||
|
||||
mod backend;
|
||||
#[cfg(feature = "http")]
|
||||
pub mod http;
|
||||
mod painter;
|
||||
#[cfg(feature = "persistence")]
|
||||
pub mod persistence;
|
||||
|
||||
Reference in New Issue
Block a user