@@ -59,6 +59,81 @@ async fn create_surface<'a: 'n>(_: impl Ctx, editor: &'a WasmEditorApi) -> Arc<W
5959// }
6060// }
6161
62+ #[ node_macro:: node( category( "Web Request" ) ) ]
63+ async fn get_request ( _: impl Ctx , _primary : ( ) , #[ name( "URL" ) ] url : String , discard_result : bool ) -> String {
64+ #[ cfg( target_arch = "wasm32" ) ]
65+ {
66+ if discard_result {
67+ wasm_bindgen_futures:: spawn_local ( async move {
68+ let _ = reqwest:: get ( url) . await ;
69+ } ) ;
70+ return String :: new ( ) ;
71+ }
72+ }
73+ #[ cfg( not( target_arch = "wasm32" ) ) ]
74+ {
75+ #[ cfg( feature = "tokio" ) ]
76+ if discard_result {
77+ tokio:: spawn ( async move {
78+ let _ = reqwest:: get ( url) . await ;
79+ } ) ;
80+ return String :: new ( ) ;
81+ }
82+ #[ cfg( not( feature = "tokio" ) ) ]
83+ if discard_result {
84+ return String :: new ( ) ;
85+ }
86+ }
87+
88+ let Ok ( response) = reqwest:: get ( url) . await else { return String :: new ( ) } ;
89+ response. text ( ) . await . ok ( ) . unwrap_or_default ( )
90+ }
91+
92+ #[ node_macro:: node( category( "Web Request" ) ) ]
93+ async fn post_request ( _: impl Ctx , _primary : ( ) , #[ name( "URL" ) ] url : String , body : Vec < u8 > , discard_result : bool ) -> String {
94+ #[ cfg( target_arch = "wasm32" ) ]
95+ {
96+ if discard_result {
97+ wasm_bindgen_futures:: spawn_local ( async move {
98+ let _ = reqwest:: Client :: new ( ) . post ( url) . body ( body) . header ( "Content-Type" , "application/octet-stream" ) . send ( ) . await ;
99+ } ) ;
100+ return String :: new ( ) ;
101+ }
102+ }
103+ #[ cfg( not( target_arch = "wasm32" ) ) ]
104+ {
105+ #[ cfg( feature = "tokio" ) ]
106+ if discard_result {
107+ let url = url. clone ( ) ;
108+ let body = body. clone ( ) ;
109+ tokio:: spawn ( async move {
110+ let _ = reqwest:: Client :: new ( ) . post ( url) . body ( body) . header ( "Content-Type" , "application/octet-stream" ) . send ( ) . await ;
111+ } ) ;
112+ return String :: new ( ) ;
113+ }
114+ #[ cfg( not( feature = "tokio" ) ) ]
115+ if discard_result {
116+ return String :: new ( ) ;
117+ }
118+ }
119+
120+ let Ok ( response) = reqwest:: Client :: new ( ) . post ( url) . body ( body) . header ( "Content-Type" , "application/octet-stream" ) . send ( ) . await else {
121+ return String :: new ( ) ;
122+ } ;
123+ response. text ( ) . await . ok ( ) . unwrap_or_default ( )
124+ }
125+
126+ #[ node_macro:: node( category( "Web Request" ) , name( "String to Bytes" ) ) ]
127+ fn string_to_bytes ( _: impl Ctx , string : String ) -> Vec < u8 > {
128+ string. into_bytes ( )
129+ }
130+
131+ #[ node_macro:: node( category( "Web Request" ) , name( "Image to Bytes" ) ) ]
132+ fn image_to_bytes ( _: impl Ctx , image : RasterDataTable < CPU > ) -> Vec < u8 > {
133+ let Some ( image) = image. instance_ref_iter ( ) . next ( ) else { return vec ! [ ] } ;
134+ image. instance . data . iter ( ) . flat_map ( |color| color. to_rgb8_srgb ( ) . into_iter ( ) ) . collect :: < Vec < u8 > > ( )
135+ }
136+
62137#[ node_macro:: node( category( "Web Request" ) ) ]
63138async fn load_resource < ' a : ' n > ( _: impl Ctx , _primary : ( ) , #[ scope( "editor-api" ) ] editor : & ' a WasmEditorApi , #[ name( "URL" ) ] url : String ) -> Arc < [ u8 ] > {
64139 let Some ( api) = editor. application_io . as_ref ( ) else {
0 commit comments