Exemple #1
0
        public void AddConverterFactory <T>(IRpcConverter <T> converter)
        {
            var factory = Converters.NewFactory(converter);

            _converterFactories.Add(factory);
            _converterMap.Add(typeof(T), factory.Create <T>());
        }
        /// <exception cref="HttpRequestException">Is thrown in a case when response from node is not OK</exception>
        public async Task <T> SendRequestAsync <T>(Request request, IRpcConverter <T> converter)
        {
            var serializedRequest = JsonConvert.SerializeObject(request, _rpcItemSerializer);
            var content           = new StringContent(serializedRequest, Encoding.UTF8, "application/json");
            var httpResponse      = await _httpClient.PostAsync(_url, content);

            var responseSerialized = await httpResponse.Content.ReadAsStringAsync();

            if (!httpResponse.IsSuccessStatusCode)
            {
                var errorCode = (int)httpResponse.StatusCode;
                if (errorCode >= (int)HttpStatusCode.InternalServerError)
                {
                    throw new HttpRequestException($"Response status code == {errorCode}, " +
                                                   $"Response body {responseSerialized}");
                }
            }

            var response = (Response)JsonConvert.DeserializeObject(responseSerialized, typeof(Response), _rpcItemSerializer);

            if (response.Error != null)
            {
                var exception = response.Error.ToException();

                throw exception;
            }

            var convertedResult = converter.ConvertTo(response.Result);

            return(convertedResult);
        }
 public static IRpcConverterFactory NewFactory <T>(IRpcConverter <T> converter)
 {
     return(new CustomRpcConverterFactory <T>(converter));
 }
 public CustomRpcConverterFactory(IRpcConverter <TT> converter)
 {
     _typeFor   = typeof(TT);
     _converter = converter;
 }