|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using System.Text.Json; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace Fission.DotNet.Common; |
| 9 | + |
| 10 | +public class FissionContext |
| 11 | +{ |
| 12 | + protected Stream _content; |
| 13 | + protected Dictionary<string, object> _arguments; |
| 14 | + protected Dictionary<string, string> _headers; |
| 15 | + protected Dictionary<string, string> _parameters; |
| 16 | + |
| 17 | + public FissionContext(Stream body, Dictionary<string, object> arguments, Dictionary<string, string> headers, Dictionary<string, string> parameters) |
| 18 | + { |
| 19 | + if (body == null) throw new ArgumentNullException(nameof(body)); |
| 20 | + if (arguments == null) throw new ArgumentNullException(nameof(arguments)); |
| 21 | + if (headers == null) throw new ArgumentNullException(nameof(headers)); |
| 22 | + if (parameters == null) throw new ArgumentNullException(nameof(parameters)); |
| 23 | + |
| 24 | + _content = body; |
| 25 | + _arguments = arguments; |
| 26 | + _headers = headers; |
| 27 | + _parameters = parameters; |
| 28 | + } |
| 29 | + |
| 30 | + protected string GetHeaderValue(string key, string defaultValue = null) |
| 31 | + { |
| 32 | + return _headers.ContainsKey(key) ? _headers[key] : defaultValue; |
| 33 | + } |
| 34 | + |
| 35 | + public Dictionary<string, object> Arguments => _arguments; |
| 36 | + public Dictionary<string, string> Parameters => _parameters; |
| 37 | + |
| 38 | + public string TraceID => GetHeaderValue("traceparent", Guid.NewGuid().ToString()); |
| 39 | + public string FunctionName => GetHeaderValue("X-Fission-Function-Name"); |
| 40 | + public string Namespace => GetHeaderValue("X-Fission-Function-Namespace"); |
| 41 | + public string ResourceVersion => GetHeaderValue("X-Fission-Function-Resourceversion"); |
| 42 | + public string UID => GetHeaderValue("X-Fission-Function-Uid"); |
| 43 | + public string Trigger => GetHeaderValue("Source-Name"); |
| 44 | + public string ContentType => GetHeaderValue("Content-Type"); |
| 45 | + public Int32 ContentLength => GetHeaderValue("Content-Length") != null ? Int32.Parse(GetHeaderValue("Content-Length")) : 0; |
| 46 | + public Stream Content => _content; |
| 47 | + |
| 48 | + public async Task<string?> ContentAsString() |
| 49 | + { |
| 50 | + if (_content == null) |
| 51 | + { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + _content.Position = 0; |
| 56 | + using (StreamReader reader = new StreamReader(_content, Encoding.UTF8, leaveOpen: true)) |
| 57 | + { |
| 58 | + return await reader.ReadToEndAsync(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public async Task<T> ContentAs<T>(JsonSerializerOptions? options = null) |
| 63 | + { |
| 64 | + if (_content == null) |
| 65 | + { |
| 66 | + return default; |
| 67 | + } |
| 68 | + |
| 69 | + _content.Position = 0; |
| 70 | + using (StreamReader reader = new StreamReader(_content, Encoding.UTF8, leaveOpen: true)) |
| 71 | + { |
| 72 | + string content = await reader.ReadToEndAsync(); |
| 73 | + return JsonSerializer.Deserialize<T>(content, options); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments