Esempio n. 1
0
        HttpRequestMessage AsHttpRequest(RpcRequest request)
        {
            var requestJson = request.ToJson().ToString();

            return(new HttpRequestMessage(HttpMethod.Post, baseAddress)
            {
                Content = new StringContent(requestJson, Neo.Utility.StrictUTF8)
            });
        }
Esempio n. 2
0
        public async Task <RpcResponse> SendAsync(RpcRequest request)
        {
            var requestJson = request.ToJson().ToString();
            var result      = await httpClient.PostAsync(httpClient.BaseAddress, new StringContent(requestJson, Encoding.UTF8));

            var content = await result.Content.ReadAsStringAsync();

            var response = RpcResponse.FromJson(JObject.Parse(content));

            response.RawResponse = content;

            if (response.Error != null)
            {
                throw new RpcException(response.Error.Code, response.Error.Message);
            }

            return(response);
        }
Esempio n. 3
0
        public void TestFromJson()
        {
            var req = new RpcRequest()
            {
                Id      = 1,
                Jsonrpc = "myrpc",
                Method  = "get",
                Params  = new JObject[] {
                    new JBoolean(true)
                }
            };
            var json       = req.ToJson();
            var rpcRequest = RpcRequest.FromJson(json);

            rpcRequest.Jsonrpc.Should().Be("myrpc");
            rpcRequest.Method.Should().Be("get");
            rpcRequest.Id.Should().Be(1);
            rpcRequest.Params.Length.Should().Be(1);
        }
Esempio n. 4
0
        public async Task <RpcResponse> SendAsync(RpcRequest request)
        {
            if (disposedValue)
            {
                throw new ObjectDisposedException(nameof(RpcClient));
            }

            var requestJson = request.ToJson().ToString();

            using var result = await httpClient.PostAsync(baseAddress, new StringContent (requestJson, Encoding.UTF8)).ConfigureAwait(false);

            var content = await result.Content.ReadAsStringAsync();

            var response = RpcResponse.FromJson(JObject.Parse(content));

            response.RawResponse = content;

            if (response.Error != null)
            {
                throw new RpcException(response.Error.Code, response.Error.Message);
            }

            return(response);
        }
Esempio n. 5
0
 private void MockResponse(RpcRequest request, RpcResponse response)
 {
     handlerMock.Protected()
     // Setup the PROTECTED method to mock
     .Setup <Task <HttpResponseMessage> >(
         "SendAsync",
         ItExpr.Is <HttpRequestMessage>(p => p.Content.ReadAsStringAsync().Result == request.ToJson().ToString()),
         ItExpr.IsAny <CancellationToken>()
         )
     // prepare the expected response of the mocked http call
     .ReturnsAsync(new HttpResponseMessage()
     {
         StatusCode = HttpStatusCode.OK,
         Content    = new StringContent(response.ToJson().ToString()),
     })
     .Verifiable();
 }