Improve the HTTP request nodes and add new related nodes (#2896)

* Improve the network request nodes and add new ones to process data

* Use Content-Type: application/octet-stream

* Add 'Gamma Correction' node
This commit is contained in:
Keavon Chambers 2025-07-18 05:03:50 -07:00 committed by GitHub
parent 561b671f8d
commit 6f46f21e21
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 118 additions and 13 deletions

View file

@ -1,11 +0,0 @@
use graphene_core::Ctx;
#[node_macro::node(category("Web Request"))]
async fn get_request(_: impl Ctx, url: String) -> reqwest::Response {
reqwest::get(url).await.unwrap()
}
#[node_macro::node(category("Web Request"))]
async fn post_request(_: impl Ctx, url: String, body: String) -> reqwest::Response {
reqwest::Client::new().post(url).body(body).send().await.unwrap()
}

View file

@ -1,5 +1,4 @@
pub mod any;
pub mod http;
pub mod text;
#[cfg(feature = "wasm")]
pub mod wasm_application_io;

View file

@ -59,6 +59,81 @@ async fn create_surface<'a: 'n>(_: impl Ctx, editor: &'a WasmEditorApi) -> Arc<W
// }
// }
#[node_macro::node(category("Web Request"))]
async fn get_request(_: impl Ctx, _primary: (), #[name("URL")] url: String, discard_result: bool) -> String {
#[cfg(target_arch = "wasm32")]
{
if discard_result {
wasm_bindgen_futures::spawn_local(async move {
let _ = reqwest::get(url).await;
});
return String::new();
}
}
#[cfg(not(target_arch = "wasm32"))]
{
#[cfg(feature = "tokio")]
if discard_result {
tokio::spawn(async move {
let _ = reqwest::get(url).await;
});
return String::new();
}
#[cfg(not(feature = "tokio"))]
if discard_result {
return String::new();
}
}
let Ok(response) = reqwest::get(url).await else { return String::new() };
response.text().await.ok().unwrap_or_default()
}
#[node_macro::node(category("Web Request"))]
async fn post_request(_: impl Ctx, _primary: (), #[name("URL")] url: String, body: Vec<u8>, discard_result: bool) -> String {
#[cfg(target_arch = "wasm32")]
{
if discard_result {
wasm_bindgen_futures::spawn_local(async move {
let _ = reqwest::Client::new().post(url).body(body).header("Content-Type", "application/octet-stream").send().await;
});
return String::new();
}
}
#[cfg(not(target_arch = "wasm32"))]
{
#[cfg(feature = "tokio")]
if discard_result {
let url = url.clone();
let body = body.clone();
tokio::spawn(async move {
let _ = reqwest::Client::new().post(url).body(body).header("Content-Type", "application/octet-stream").send().await;
});
return String::new();
}
#[cfg(not(feature = "tokio"))]
if discard_result {
return String::new();
}
}
let Ok(response) = reqwest::Client::new().post(url).body(body).header("Content-Type", "application/octet-stream").send().await else {
return String::new();
};
response.text().await.ok().unwrap_or_default()
}
#[node_macro::node(category("Web Request"), name("String to Bytes"))]
fn string_to_bytes(_: impl Ctx, string: String) -> Vec<u8> {
string.into_bytes()
}
#[node_macro::node(category("Web Request"), name("Image to Bytes"))]
fn image_to_bytes(_: impl Ctx, image: RasterDataTable<CPU>) -> Vec<u8> {
let Some(image) = image.instance_ref_iter().next() else { return vec![] };
image.instance.data.iter().flat_map(|color| color.to_rgb8_srgb().into_iter()).collect::<Vec<u8>>()
}
#[node_macro::node(category("Web Request"))]
async fn load_resource<'a: 'n>(_: impl Ctx, _primary: (), #[scope("editor-api")] editor: &'a WasmEditorApi, #[name("URL")] url: String) -> Arc<[u8]> {
let Some(api) = editor.application_io.as_ref() else {