-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonRpcEngine.cs
58 lines (52 loc) · 2.46 KB
/
JsonRpcEngine.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
using Newtonsoft.Json;
namespace BlockchainInteraction
{
public class JsonRpcEngine
{
public string rpcUrl;
private HttpClient client;
private RequestRpcMiddleware<object>[] requestMiddleware;
private ResponseRpcMiddleware<object, object>[] responseMiddleware;
public JsonRpcEngine(string rpcUrl)
{
this.rpcUrl = rpcUrl;
this.client = new HttpClient();
this.requestMiddleware = new RequestRpcMiddleware<object>[] { };
this.responseMiddleware = new ResponseRpcMiddleware<object, object>[] { };
}
public void AddRequestMiddleware<Params>(RequestRpcMiddleware<Params> middleware)
{
RequestRpcMiddleware<object>? _temp = middleware as RequestRpcMiddleware<object>;
if (_temp != null) this.requestMiddleware.Append(_temp);
}
public void AddResponseMiddleware<Params, Result>(ResponseRpcMiddleware<Params, Result> middleware)
{
ResponseRpcMiddleware<object, object>? _temp = middleware as ResponseRpcMiddleware<object, object>;
if (_temp != null) this.responseMiddleware.Append(_temp);
}
private async Task<JsonRpcResponse<object>> fetch<Params>(JsonRpcRequest<Params> request)
{
string json = JsonConvert.SerializeObject(request);
StringContent httpContent = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
var res = await this.client.PostAsync(this.rpcUrl, httpContent);
string? contents = await res.Content.ReadAsStringAsync();
var data = new JsonRpcResponse<object>(request.id);
data.SetResult(contents);
return data;
}
public async Task<object?> Handle<Params, Result>(JsonRpcRequest<Params> request, Action<JsonRpcResponse<object>>? callback)
{
JsonRpcRequest<object>? objectRequest = (request as JsonRpcRequest<object>);
if (objectRequest != null)
{
RequestRpcMiddlewareReturn<object> requestResult = JsonRpcMiddleware.RunRequestMiddlewareList<object>(objectRequest, this.requestMiddleware);
if (requestResult.error != null) throw new Exception("RPC engine fail");
JsonRpcResponse<object> _result = await this.fetch<object>(requestResult.request);
ResponseRpcMiddlewareReturn<object, object> responseResult = JsonRpcMiddleware.RunResponseMiddlewareList<object, object>(objectRequest, _result, this.responseMiddleware);
if (callback != null) callback(responseResult.response);
return responseResult.response.result;
}
return null;
}
}
}