/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) { GetCostAndUsageWithResourcesResponse response = new GetCostAndUsageWithResourcesResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.TestExpression("GroupDefinitions", targetDepth)) { var unmarshaller = new ListUnmarshaller <GroupDefinition, GroupDefinitionUnmarshaller>(GroupDefinitionUnmarshaller.Instance); response.GroupDefinitions = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("NextPageToken", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.NextPageToken = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("ResultsByTime", targetDepth)) { var unmarshaller = new ListUnmarshaller <ResultByTime, ResultByTimeUnmarshaller>(ResultByTimeUnmarshaller.Instance); response.ResultsByTime = unmarshaller.Unmarshall(context); continue; } } return(response); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); try { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); log.LogInformation("requestBody : " + requestBody); GetCostAndUsageWithResourcesRequest costAndUsageWithResourcesRequest = JsonConvert.DeserializeObject <GetCostAndUsageWithResourcesRequest>(requestBody); log.LogInformation("Build JSON Completed and AWS call initiated.."); GetCostAndUsageWithResourcesResponse costAndUsageWithResourcesResponse = CallAWSCostAndUsageAPI(costAndUsageWithResourcesRequest); log.LogInformation("AWS call Completed.."); return(new OkObjectResult(costAndUsageWithResourcesResponse)); } catch (Exception ex) { log.LogInformation("Error occured in AWS Cost and Usage " + ex.Message.ToString()); return(new BadRequestObjectResult(ex.Message.ToString())); } }
private static GetCostAndUsageWithResourcesResponse CallAWSCostAndUsageAPI(GetCostAndUsageWithResourcesRequest costAndUsageWithResourcesRequest) { var client = new AmazonCostExplorerClient( awsAccessKeyId: Environment.GetEnvironmentVariable("awsAccessKeyId"), awsSecretAccessKey: Environment.GetEnvironmentVariable("awsSecretAccessKey"), Amazon.RegionEndpoint.USEast1); GetCostAndUsageWithResourcesResponse costAndUsageWithResourcesResponse = client.GetCostAndUsageWithResourcesAsync(costAndUsageWithResourcesRequest).Result; return(costAndUsageWithResourcesResponse); }