Beispiel #1
0
        public async Task AsyncPostDelegationCompletes()
        {
            using var client = CreateClient(Configuration.LocalBaseUrl);

            var baseRequest = await client.CreateBaseReq(Configuration.LocalDelegator1Address, null, null, null, null, null);

            var delegateRequest = new DelegateRequest(baseRequest, Configuration.LocalDelegator1Address, Configuration.LocalValidator1Address, new Coin("stake", 10));

            var postResult = await client
                             .Staking
                             .PostDelegationsAsync(delegateRequest);

            OutputHelper.WriteLine("Deserialized tx:");
            Dump(postResult);
            OutputHelper.WriteLine("");

            Assert.True(postResult.Msg.Count > 0);
            var msgDelegate = postResult
                              .Msg
                              .OfType <MsgDelegate>()
                              .First();

            Assert.Equal("stake", msgDelegate.Amount.Denom);
            Assert.Equal(10, msgDelegate.Amount.Amount);
        }
Beispiel #2
0
        /// <summary>
        /// Get a delegate from given parameters asynchronously
        /// </summary>
        /// <returns>DelegatesResponse with delegates</returns>
        public async Task <DelegateResponse> GetDelegateAsync(DelegateRequest req)
        {
            _url.Path  = Constants.ApiGetDelegate;
            _url.Query = req.ToQuery();
            var response = await _client.GetJsonAsync <DelegateResponse>(_url.ToString());

            ResetPath();
            return(response);
        }
        public Task <StdTx> PostDelegationsAsync(DelegateRequest request, CancellationToken cancellationToken = default)
        {
            var baseRequest = new BaseReqWithSimulate(request.BaseReq, false);

            request = new DelegateRequest(baseRequest, request.DelegatorAddress, request.ValidatorAddress, request.Amount);
            return(_clientGetter()
                   .Request("staking", "delegators", request.DelegatorAddress, "delegations")
                   .PostJsonAsync(request, cancellationToken)
                   .ReceiveJson <StdTx>()
                   .WrapExceptions());
        }
        private string LogError <T>(DelegateRequest <T> request, IResponse response, bool isWarning = false)
        {
            var message = string.Empty;

            if (response != null)
            {
                var requestMethod = request != null?request.GetAction() : null;

                message = $"{requestMethod.NaIfEmpty()} --> {response.GetErrorCode().NaIfEmpty()}. {response.GetErrorMessage().NaIfEmpty()}";

                Logger.Log(isWarning ? LogLevel.Warning : LogLevel.Error, new Exception(response.GetJson()), message, null);
            }

            return(message);
        }
        public ResponseResult AcceptProject(DelegateRequest requestFrom)
        {
            LogHelper.Error("调用接单API:" + requestFrom.ToJson(), null);
            var responseResult = new ResponseResult();

            try
            {
                if (string.IsNullOrEmpty(requestFrom.bussinessId))
                {
                    throw new InvalidDataException("必填项不能为空");
                }

                var project = JsonConvert.DeserializeObject <ProjectRequest>(requestFrom.bussinessForm);
                if (project == null)
                {
                    throw new InvalidDataException("bussinessForm参数不能为空");
                }
                project.BusinessId = requestFrom.bussinessId;


                var company = CompanyService.GetByCompanyId(project.CompanyId.HasValue?project.CompanyId.Value:0);
                if (company == null)
                {
                    throw new InvalidDataException("评估机构参数错误,找不到所属评估机构");
                }

                var resources = JsonConvert.DeserializeObject <IList <ProjectResourceRequest> >(requestFrom.files);

                var projectReturn = ProjectService.AcceptBusiness(project, resources);
                responseResult.Code    = 0;
                responseResult.Message = "success";
                responseResult.Data    = new { ProjectNo = projectReturn.ProjectNo };
            }
            catch (InvalidDataException ex)
            {
                responseResult.Code    = 1;
                responseResult.Message = ex.Message;
            }
            catch (Exception ex)
            {
                responseResult.Code    = 1;
                responseResult.Message = "bussinessForm,files参数为空或者格式不正确";
                LogHelper.Error("调用接单API错误日志:", ex);
            }
            return(responseResult);
        }
Beispiel #6
0
        public ResponseResult AcceptProject(DelegateRequest requestFrom)
        {
            LogHelper.Error("调用接单API:" + requestFrom.ToJson(), null);
            var responseResult = new ResponseResult();

            try
            {
                if (string.IsNullOrEmpty(requestFrom.bussinessId))
                {
                    throw new InvalidDataException("必填项不能为空");
                }

                var project = requestFrom.bussinessForm;
                if (project == null)
                {
                    throw new InvalidDataException("bussinessForm参数不能为空");
                }
                project.BusinessId = requestFrom.bussinessId;
                //if (!project.CompanyId.HasValue)
                //{
                //    throw new InvalidDataException("CompanyId参数不能为空");
                //}

                IList <ProjectResourceRequest> resources = requestFrom.files;
                if (requestFrom.files == null)
                {
                    resources = new List <ProjectResourceRequest>();
                }
                var projectReturn = ProjectService.ReceiveProject(project, resources, requestFrom.bussinessForm.CompanyName);
                responseResult.Code    = 0;
                responseResult.Message = "success";
                responseResult.Data    = new { ProjectNo = projectReturn.ProjectNo };
            }
            catch (InvalidDataException ex)
            {
                responseResult.Code    = 1;
                responseResult.Message = ex.Message;
            }
            catch (Exception ex)
            {
                responseResult.Code    = 1;
                responseResult.Message = "bussinessForm,files参数为空或者格式不正确";
                LogHelper.Error("调用接单API错误日志:", ex);
            }
            return(responseResult);
        }
Beispiel #7
0
        public async Task AsyncPostSimulatedDelegationCompletes()
        {
            using var client = CreateClient(Configuration.LocalBaseUrl);

            var baseRequest = await client.CreateBaseReq(Configuration.LocalDelegator1Address, null, null, null, null, null);

            var delegateRequest = new DelegateRequest(baseRequest, Configuration.LocalDelegator1Address, Configuration.LocalValidator1Address, new Coin("stake", 10));

            var postResult = await client
                             .Staking
                             .PostDelegationsSimulationAsync(delegateRequest);

            OutputHelper.WriteLine("Deserialized gas estimate:");
            Dump(postResult);
            OutputHelper.WriteLine("");

            Assert.NotNull(postResult);
        }
Beispiel #8
0
        public void ConstructorTest()
        {
            var methods = new HttpMethod[] {
                HttpMethod.Get,
                HttpMethod.Post,
                HttpMethod.Put,
                HttpMethod.Delete,
            };

            foreach (var method in methods)
            {
                var path = Guid.NewGuid().ToString();
                var acceptContentTypes = new[] {
                    PreDefinedAcceptContentTypes.Json
                };
                var request = new DelegateRequest <object, object, object>(method, path, acceptContentTypes);

                Assert.AreEqual(method, request.Method);
                Assert.AreEqual(path, request.Path);
                Assert.IsTrue(Enumerable.SequenceEqual(acceptContentTypes, request.AcceptContentTypes));
            }
        }
Beispiel #9
0
        /// <summary>
        /// Get a delegate from given parameters synchronously
        /// </summary>
        /// <returns>DelegatesResponse with delegates</returns>
        public DelegateResponse GetDelegate(DelegateRequest req)
        {
            var response = GetDelegateAsync(req).GetAwaiter().GetResult();

            return(response);
        }
Beispiel #10
0
        public async Task HandlersTest()
        {
            var configureHeaderCalled    = false;
            var configureParameterCalled = false;
            var parseFailureResultCalled = false;
            var parseSuccessResultCalled = false;
            var expectedParameter        = new object();
            var expectedParameterBag     = new StubParameterBag();
            var expectedHeaderBag        = new StubHeaderBag();
            var expectedStatusCode       = HttpStatusCode.OK;
            var expectedRequestResult    = new StubRequestResult();
            var expectedSuccessResult    = new object();
            var expectedFailureResult    = new object();

            var request = new DelegateRequest <object, object, object>(HttpMethod.Post, string.Empty)
            {
                ConfigureHeaderAsyncHandler = (headerBag, parameter) => {
                    Assert.AreEqual(expectedHeaderBag, headerBag);
                    Assert.AreEqual(expectedParameter, parameter);
                    configureHeaderCalled = true;

                    return(Task.FromResult(0));
                },
                ConfigureParameterAsyncHandler = (parameterBag, parameter) => {
                    Assert.AreEqual(expectedParameterBag, parameterBag);
                    Assert.AreEqual(expectedParameter, parameter);
                    configureParameterCalled = true;

                    return(Task.FromResult(0));
                },
                ParseSuccessResultAsyncHandler = (statusCode, requestResult) => {
                    Assert.AreEqual(expectedStatusCode, statusCode);
                    Assert.AreEqual(expectedRequestResult, requestResult);
                    parseSuccessResultCalled = true;

                    return(Task.FromResult <object>(expectedSuccessResult));
                },
                ParseFailureResultAsyncHandler = (statusCode, requestResult) => {
                    Assert.AreEqual(expectedStatusCode, statusCode);
                    Assert.AreEqual(expectedRequestResult, requestResult);
                    parseFailureResultCalled = true;

                    return(Task.FromResult <object>(expectedFailureResult));
                },
            };

            await request.ConfigureHeaderAsync(expectedHeaderBag, expectedParameter);

            await request.ConfigureParameterAsync(expectedParameterBag, expectedParameter);

            var successResult = await request.ParseSuccessResultAsync(expectedStatusCode, expectedRequestResult);

            var failureResult = await request.ParseFailureResultAsync(expectedStatusCode, expectedRequestResult);

            Assert.IsTrue(configureHeaderCalled);
            Assert.IsTrue(configureParameterCalled);
            Assert.IsTrue(parseSuccessResultCalled);
            Assert.IsTrue(parseFailureResultCalled);

            Assert.AreEqual(expectedSuccessResult, successResult);
            Assert.AreEqual(expectedFailureResult, failureResult);
        }
Beispiel #11
0
 // sends an request to the remote server (async)
 private void SendRequestAsync(string name, Uri remotePath, FileTransferCallback callback, DelegateRequest caller)
 {
     try
     {
         caller.BeginInvoke(name, remotePath,
                            delegate(IAsyncResult iar)
         {
             var c = (DelegateRequest)iar.AsyncState;
             c.EndInvoke(iar);
             callback.Invoke(FileTransferResult.Success);
         }, caller);
     }
     catch (Exception e)
     {
         ErrorLog.Add(this, e.Message);
         callback.Invoke(FileTransferResult.Failure);
     }
 }
 public GasEstimateResponse PostDelegationsSimulation(DelegateRequest request)
 {
     return(PostDelegationsSimulationAsync(request)
            .Sync());
 }
 public StdTx PostDelegations(DelegateRequest request)
 {
     return(PostDelegationsAsync(request)
            .Sync());
 }
Beispiel #14
0
 public void sendRequest(DelegateRequest a)
 {
     this.a = a;
 }