/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) { GetCostForecastResponse response = new GetCostForecastResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.TestExpression("ForecastResultsByTime", targetDepth)) { var unmarshaller = new ListUnmarshaller <ForecastResult, ForecastResultUnmarshaller>(ForecastResultUnmarshaller.Instance); response.ForecastResultsByTime = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Total", targetDepth)) { var unmarshaller = MetricValueUnmarshaller.Instance; response.Total = unmarshaller.Unmarshall(context); continue; } } return(response); }
public async Task <List <CostUsageResponse> > GetForecastForCurrentMonth(CostUsageRequest costUsageRequest) { var mapRequest = mapper.Map <GetCostForecastRequest>(costUsageRequest); mapRequest.Granularity = Granularity.MONTHLY; mapRequest.Metric = Amazon.CostExplorer.Metric.UNBLENDED_COST; string startDate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"); //DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"); string endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).ToString("yyyy-MM-dd"); //new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd"); if (endDate.Equals(startDate)) { } mapRequest.TimePeriod = new DateInterval() { Start = startDate, End = endDate }; GetCostForecastResponse response = new GetCostForecastResponse(); try { response = await costExplorerRepository.GetCostForecast(mapRequest); } catch (DataUnavailableException ex) { return(new List <CostUsageResponse>() { new CostUsageResponse() { Name = "Current Month Cost:", Amount = 0.00M, Custom = "0", Date = DateTime.Now } }); } var currentMonthCost = response.Total.Amount; var lastMonthCost = await GetLastMonthCost(costUsageRequest); var mapResponse = response.ForecastResultsByTime.Select(x => new CostUsageResponse() { Name = "Current Month Cost:", Amount = Convert.ToDecimal(currentMonthCost), Custom = Convert.ToString(Math.Round((Convert.ToDecimal(currentMonthCost) - Convert.ToDecimal(lastMonthCost)) / Convert.ToDecimal(currentMonthCost) * 100, 2)) }); return(mapResponse.ToList()); }