public async Task GivenICreateAContractWithTheFollowingValues(Table table) { foreach (TableRow tableRow in table.Rows) { EstateDetails estateDetails = this.TestingContext.GetEstateDetails(tableRow); String token = this.TestingContext.AccessToken; if (String.IsNullOrEmpty(estateDetails.AccessToken) == false) { token = estateDetails.AccessToken; } String operatorName = SpecflowTableHelper.GetStringRowValue(tableRow, "OperatorName"); Guid operatorId = estateDetails.GetOperatorId(operatorName); CreateContractRequest createContractRequest = new CreateContractRequest { OperatorId = operatorId, Description = SpecflowTableHelper.GetStringRowValue(tableRow, "ContractDescription") }; CreateContractResponse contractResponse = await this.TestingContext.DockerHelper.EstateClient.CreateContract(token, estateDetails.EstateId, createContractRequest, CancellationToken.None); estateDetails.AddContract(contractResponse.ContractId, createContractRequest.Description, operatorId); } }
/// <summary> /// Creates the contract. /// </summary> /// <param name="accessToken">The access token.</param> /// <param name="estateId">The estate identifier.</param> /// <param name="createContractRequest">The create contract request.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public async Task <CreateContractResponse> CreateContract(String accessToken, Guid estateId, CreateContractRequest createContractRequest, CancellationToken cancellationToken) { CreateContractResponse response = null; String requestUri = this.BuildRequestUrl($"/api/estates/{estateId}/contracts/"); try { String requestSerialised = JsonConvert.SerializeObject(createContractRequest); StringContent httpContent = new StringContent(requestSerialised, Encoding.UTF8, "application/json"); // Add the access token to the client headers this.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); // Make the Http Call here HttpResponseMessage httpResponse = await this.HttpClient.PostAsync(requestUri, httpContent, cancellationToken); // Process the response String content = await this.HandleResponse(httpResponse, cancellationToken); // call was successful so now deserialise the body to the response object response = JsonConvert.DeserializeObject <CreateContractResponse>(content); } catch (Exception ex) { // An exception has occurred, add some additional information to the message Exception exception = new Exception($"Error creating contract [{createContractRequest.Description}] for estate {estateId}.", ex); throw exception; } return(response); }