mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2025-08-22 22:14:07 +00:00
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:
parent
561b671f8d
commit
6f46f21e21
6 changed files with 118 additions and 13 deletions
|
@ -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()
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
pub mod any;
|
||||
pub mod http;
|
||||
pub mod text;
|
||||
#[cfg(feature = "wasm")]
|
||||
pub mod wasm_application_io;
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue