public async Task <RpcResponse <T> > PostAsync <T>(string url, RpcRequestBody body) { FixRequestBody(body); string bodyStr = JsonSerializer.Serialize(body, Config.JsonSerializerOptions); var httpContent = new StringContent(bodyStr, Encoding.UTF8, "application/json"); HttpResponseMessage response = null; try { response = await _httpClient.PostAsync(url, httpContent); } catch (HttpRequestException rex) { response?.Dispose(); return(new RpcResponse <T>(false) { ErrorMsg = (rex.InnerException ?? rex).Message }); } catch (Exception ex) { response?.Dispose(); return(new RpcResponse <T>(false) { ErrorMsg = ex.Message }); } if (!response.IsSuccessStatusCode) { return(new RpcResponse <T>(false) { ErrorMsg = response.StatusCode.ToString() }); } using var rspStream = await response.Content.ReadAsStreamAsync(); RpcResponseBody <T> rspBody = null; try { rspBody = await JsonSerializer.DeserializeAsync <RpcResponseBody <T> >(rspStream, Config.JsonSerializerOptions); } catch (JsonException jex) { #if DEBUG string rspBodyText = await response.Content.ReadAsStringAsync(); Debug.WriteLine("JsonParseError: at[{0}]->{1}", nameof(RpcHttpClient), rspBodyText); #endif return(new RpcResponse <T>(false) { ErrorMsg = jex.Message }); } return(new RpcResponse <T>(true) { Body = rspBody }); }
private void FixRequestBody(RpcRequestBody body) { if (string.IsNullOrEmpty(body.JsonRpc)) { body.JsonRpc = Config.ApiVersion.ToString(2); } if (body.Params is null) { body.Params = Array.Empty <object>(); } }
public async Task <NodeCache> ExecuteAsync(Node node) { var allRpcMethods = _rpcMethodSettings.Items; var indexes = _rpcMethodSettings.Indexes ?? new HashSet <int>(); var client = _clientFactory.CreateClient(); client.BaseAddress = new Uri(node.Url); client.Timeout = TimeSpan.FromMilliseconds(_commonOption.Timeout); var result = new NodeCache(node); var rpcMethods = indexes.Select(i => allRpcMethods[i]).ToArray(); var tasks = rpcMethods.AsParallel().Select(async m => { IValidatePipeline pipeline; if (m.ResultType != ResultTypeEnum.None) { var resultValidator = ValidatorUtility.GetCachedValidator(m.ResultType); pipeline = _pipelineBuilder.Use(resultValidator).Build(); } else { pipeline = _pipelineBuilder.Build(); } var r = await pipeline.ValidateAsync(async() => { var body = new RpcRequestBody(m.Name.ToLower()) { JsonRpc = _commonOption.Jsonrpc, Params = m.Params ?? Enumerable.Empty <object>(), Id = _commonOption.Id }; string bodyStr = JsonSerializer.Serialize(body, jsonSerializerOptions); var httpContent = new StringContent(bodyStr, Encoding.UTF8, "application/json"); return(await client.PostAsync(string.Empty, httpContent)); }, m.Result ?? string.Empty); var typeResult = r.Result ? new ValidateResult <ValidationResultType>() { Result = ValidationResultType.Available } : new ValidateResult <ValidationResultType>() { Result = ValidationResultType.Unavailable, Exception = r.Exception, ExtraErrorMsg = r.ExtraErrorMsg }; result.MethodsResult.TryAdd(m.Name, typeResult); }); await Task.WhenAll(tasks); var uncheckedTypeResult = new ValidateResult <ValidationResultType>() { Result = ValidationResultType.Unchecked }; if (allRpcMethods.Length > indexes.Count) { for (int i = 0; i < allRpcMethods.Length; i++) { if (!indexes.Contains(i)) { result.MethodsResult.TryAdd(allRpcMethods[i].Name, uncheckedTypeResult); } } } return(result); }