This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathGrpcExplorer.cs
216 lines (190 loc) · 8.04 KB
/
GrpcExplorer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System.Collections;
using System.Diagnostics;
using System.Text.Json.Nodes;
using Grpc.Net.Client;
using Microsoft.Extensions.Logging;
using Spacetime.Core.Formatters;
using Spacetime.Core.gRPC.Dynamic;
using Spacetime.Core.gRPC.Interfaces;
namespace Spacetime.Core.gRPC
{
public class GrpcExplorer : IGrpcExplorer
{
private readonly JsonFormatter _formatter;
private readonly ILogger<GrpcExplorer> _log;
private readonly IDynamicGrpcFactory _factory;
public GrpcExplorer(ILogger<GrpcExplorer> log, IDynamicGrpcFactory factory, JsonFormatter formatter)
{
_log = log;
_factory = factory;
_formatter = formatter;
}
public async Task<GrpcResponse> Invoke(string host, string service, string method, string json)
{
_log.LogInformation("Invoking service {host}, {service}, {method}", host, service, method);
var response = new GrpcResponse() { Status = GrpcStatus.Active };
Stopwatch stopwatch = null;
try
{
using var channel = GrpcChannel.ForAddress(host);
var client = await _factory.FromServerReflection(channel);
var input = CreateDictionaryFromJson(json);
stopwatch = Stopwatch.StartNew();
var jsonResponse = string.Empty;
await foreach (var result in client.AsyncDynamicCall(service, method, ToAsync(input)))
{
jsonResponse = ToJson(result)!.ToJsonString();
}
stopwatch.Stop();
if (!string.IsNullOrWhiteSpace(jsonResponse))
{
jsonResponse = _formatter.Format(jsonResponse);
}
response.Status = GrpcStatus.Ok;
response.ResponseBody = jsonResponse;
response.ElapsedMs = stopwatch.ElapsedMilliseconds;
}
catch (Exception ex)
{
_log.LogError(ex, "Failed to invoke service {host}, {service}, {method}", host, service, method);
response.ResponseBody = ex.Message;
response.Status = GrpcStatus.Error;
response.ElapsedMs = stopwatch?.ElapsedMilliseconds ?? -1;
}
return response;
}
private static List<IDictionary<string, object>> CreateDictionaryFromJson(string json)
{
var data = ParseJson(json);
var input = ParseInput(data);
return input;
}
// thanks & credit to: https://github.com/xoofx/grpc-curl
private static List<IDictionary<string, object>> ParseInput(object data)
{
// Parse Input
var input = new List<IDictionary<string, object>>();
switch (data)
{
case IEnumerable<object> it:
{
var index = 0;
foreach (var item in it)
{
if (item is IDictionary<string, object> dict)
{
input.Add(dict);
}
else
{
throw new ApplicationException($"Invalid type `{item?.GetType()?.FullName}` from the input array at index [{index}]. Expecting an object.");
}
index++;
}
break;
}
case IDictionary<string, object> dict:
input.Add(dict);
break;
default:
throw new ApplicationException($"Invalid type `{data?.GetType()?.FullName}` from the input. Expecting an object.");
}
return input;
}
// thanks & credit to: https://github.com/xoofx/grpc-curl
private static JsonNode? ToJson(object? requestObject)
{
switch (requestObject)
{
case int i32: return JsonValue.Create(i32);
case uint u32: return JsonValue.Create(u32);
case long i64: return JsonValue.Create(i64);
case ulong u64: return JsonValue.Create(u64);
case float f32: return JsonValue.Create(f32);
case double f64: return JsonValue.Create(f64);
case short i16: return JsonValue.Create(i16);
case ushort u16: return JsonValue.Create(u16);
case sbyte i8: return JsonValue.Create(i8);
case byte u8: return JsonValue.Create(u8);
case bool b: return JsonValue.Create(b);
case string str:
return JsonValue.Create(str);
case IDictionary<string, object> obj: // objects become Dictionary<string,object>
var jsonObject = new JsonObject();
foreach (var kp in obj)
{
jsonObject.Add(kp.Key, ToJson(kp.Value));
}
return jsonObject;
case IEnumerable array: // arrays become List<object>
var jsonArray = new JsonArray();
foreach (var o in array)
{
jsonArray.Add(ToJson(o));
}
return jsonArray;
case KeyValuePair<object, object> pair:
return JsonValue.Create(pair);
default: // don't know what to do here
return null;
}
}
// thanks & credit to: https://github.com/xoofx/grpc-curl
private static async IAsyncEnumerable<IDictionary<string, object>> ToAsync(IEnumerable<IDictionary<string, object>> input)
{
foreach (var item in input)
{
yield return await ValueTask.FromResult(item);
}
}
// thanks & credit to: https://github.com/xoofx/grpc-curl
private static object? ParseJson(string data)
{
try
{
var json = JsonNode.Parse(data);
return ToApiRequest(json);
}
catch (Exception ex)
{
throw new ApplicationException($"Failing to deserialize JSON data. Reason: {ex.Message}.");
}
}
// thanks & credit to: https://github.com/xoofx/grpc-curl
private static object? ToApiRequest(JsonNode? requestObject)
{
switch (requestObject)
{
case JsonObject jObject: // objects become Dictionary<string,object>
return jObject.ToDictionary(j => j.Key, j => ToApiRequest(j.Value));
case JsonArray jArray: // arrays become List<object>
return jArray.Select(ToApiRequest).ToList();
case JsonValue jValue: // values just become the value
return jValue.GetValue<object>();
case null:
return null;
default: // don't know what to do here
throw new Exception($"Unsupported type: {requestObject.GetType()}");
}
}
public async Task<GrpcExploreResult> Explore(string host)
{
using var channel = GrpcChannel.ForAddress(host);
var client = await _factory.FromServerReflection(channel);
var result = await client.Explore();
// the .NET server reflection for gRPC shows up as a service
// named ServerReflection - remove this from the list to avoid clutter
result.Services = result.Services
.Where(p => !p.Name.Equals("ServerReflection", StringComparison.OrdinalIgnoreCase)).ToList();
result.Services.ForEach(svc =>
{
svc.Id = Guid.NewGuid();
svc.Methods.ForEach(method =>
{
method.Id = Guid.NewGuid();
});
});
return result;
}
}
}