File tree Expand file tree Collapse file tree 2 files changed +80
-0
lines changed Expand file tree Collapse file tree 2 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 1
1
namespace EdgeFunctionWorld . wit . exports . wasi . http . v0_2_0 ;
2
2
using System . Text ;
3
+ using System . Text . Json ;
4
+ using System . Text . Json . Serialization ;
3
5
4
6
using EdgeFunctionWorld ;
5
7
using EdgeFunctionWorld . wit . exports . wasi . http . v0_2_0 ;
6
8
using EdgeFunctionWorld . wit . imports . wasi . http . v0_2_0 ;
7
9
using EdgeFunctionWorld . wit . imports . wasi . io . v0_2_0 ;
8
10
11
+ public class Settings
12
+ {
13
+ [ JsonPropertyName ( "example" ) ]
14
+ public string Example { get ; set ; }
15
+ }
16
+
17
+ [ JsonSerializable ( typeof ( Settings ) ) ]
18
+ internal partial class SettingsJsonContext : JsonSerializerContext
19
+ {
20
+ }
21
+
22
+
23
+
9
24
public class IncomingHandlerImpl : IIncomingHandler {
10
25
public static void Handle ( ITypes . IncomingRequest request , ITypes . ResponseOutparam responseOut ) {
26
+ var incomingHeaders = RequestUtility . ParseHeaders ( request ) ;
27
+ var incomingBody = RequestUtility . ParseBody ( request ) ;
28
+ var settings = RequestUtility . ParseSetting ( incomingHeaders ) ;
29
+
30
+ Console . WriteLine ( $ "Received request with headers: { string . Join ( ", " , incomingHeaders . Select ( kv => $ "{ kv . Key } : { Encoding . UTF8 . GetString ( kv . Value ) } ") ) } ") ;
31
+ if ( incomingBody != null )
32
+ {
33
+ Console . WriteLine ( $ "Request body: { Encoding . UTF8 . GetString ( incomingBody ) } ") ;
34
+ }
35
+ else
36
+ {
37
+ Console . WriteLine ( "Request body: null" ) ;
38
+ }
39
+ Console . WriteLine ( $ "Settings: { settings ? . Example ?? "No settings provided" } ") ;
40
+
41
+ // write output
11
42
byte [ ] content = "Hello, World!"u8 . ToArray ( ) ;
12
43
List < ( string , byte [ ] ) > headers =
13
44
[
Original file line number Diff line number Diff line change
1
+ using System . Text ;
2
+ using System . Text . Json ;
3
+ using System . Text . Json . Serialization ;
4
+
5
+ using EdgeFunctionWorld ;
6
+ using EdgeFunctionWorld . wit . exports . wasi . http . v0_2_0 ;
7
+ using EdgeFunctionWorld . wit . imports . wasi . http . v0_2_0 ;
8
+ using EdgeFunctionWorld . wit . imports . wasi . io . v0_2_0 ;
9
+
10
+ public static class RequestUtility {
11
+ public static Dictionary < string , byte [ ] > ParseHeaders ( ITypes . IncomingRequest request ) {
12
+ var requestHeaders = new Dictionary < string , byte [ ] > ( ) ;
13
+ foreach ( ( var key , var value ) in request . Headers ( ) . Entries ( ) )
14
+ {
15
+ requestHeaders . Add ( key , value ) ;
16
+ }
17
+ return requestHeaders ;
18
+ }
19
+
20
+ public static byte [ ] ? ParseBody ( ITypes . IncomingRequest request ) {
21
+ var body_handle = request . Consume ( ) ;
22
+ var stream = body_handle . Stream ( ) ;
23
+ var bodyChunks = new List < byte > ( ) ;
24
+
25
+ try {
26
+ while ( true ) {
27
+ var chunk = stream . Read ( 1024 ) ;
28
+ if ( chunk . Length == 0 ) {
29
+ break ;
30
+ }
31
+ bodyChunks . AddRange ( chunk ) ;
32
+ }
33
+ }
34
+ catch ( EdgeFunctionWorld . WitException e ) {
35
+ switch ( e . Value ) {
36
+ case global ::EdgeFunctionWorld . wit . imports . wasi . io . v0_2_0 . IStreams . StreamError streamError :
37
+ break ;
38
+ default :
39
+ throw ;
40
+ }
41
+ }
42
+ finally { }
43
+ return bodyChunks . ToArray ( ) ;
44
+ }
45
+
46
+ public static Settings ParseSetting ( Dictionary < string , byte [ ] > headers ) {
47
+ return JsonSerializer . Deserialize < Settings > ( headers [ "x-edgee-component-settings" ] , SettingsJsonContext . Default . Settings ) ;
48
+ }
49
+ }
You can’t perform that action at this time.
0 commit comments