public async Task TestBatchApiRequestsOneRequestAsync()
        {
            Trace.WriteLine("POST /batch");

            // Start the batch request
            BatchApiRequest.StartNewBatchRequest();

            try
            {
                // Make the API calls to be batched
                await Template.GetTemplatesAsync();

                // Make the batch API Reqeust
                var batchResponses = await BatchApiRequest.SendBatchApiRequest();

                // Validate the response to the batch API request
                ValidateBatchApiCallResponses(batchResponses, 1);

                // Validate the response to the individual API call
                ValidateIndividualBatchedApiCallResponse <List <Template> >(batchResponses[0]);
            }
            catch (AggregateException exception)
            {
                Assert.Fail(exception.ToString());
            }
        }
        public async Task TestBatchApiRequestsAbortRequestAsync()
        {
            Trace.WriteLine("POST /batch");

            // Start the batch request
            BatchApiRequest.StartNewBatchRequest();

            try
            {
                // Make the API call to be batched
                await Template.GetTemplatesAsync();

                // Abort the batch request
                BatchApiRequest.AbortBatchRequest();

                // Make another API call and make sure it goes through
                var snippets = await Snippet.GetSnippetsAsync();

                SendwithusClientTest.ValidateResponse(snippets);

                // Make the aborted batch API Reqeust anyways
                var batchResponses = await BatchApiRequest.SendBatchApiRequest();

                // Make sure no API calls were included in the batch request (empty request)
                ValidateBatchApiCallResponses(batchResponses, 0);
            }
            catch (AggregateException exception)
            {
                Assert.Fail(exception.ToString());
            }
        }
        public async Task TestBatchApiRequestsTenRequestsAsync()
        {
            Trace.WriteLine("POST /batch");

            // Start the batch request
            BatchApiRequest.StartNewBatchRequest();

            // Make the API calls to be batched (at least one of each type)
            try
            {
                await TemplateTest.BuildAndSendCreateTemplateRequestWithAllParametersAsync(); // POST

                await Snippet.GetSnippetsAsync();                                             // GET

                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET

                await RenderTest.BuildAndSendRenderTemplateRequestWithAllParametersId();      // POST

                await Log.GetLogEventsAsync(BatchApiRequestTest.DEFAULT_LOG_ID);              // GET

                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET

                await EmailTest.BuildAndSendEmailWithAllParametersAsync();                    // POST

                await DripCampaign.GetDripCampaignsAsync();                                   // GET

                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET

                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET

                // Make the batch Api Reqeust
                var batchResponses = await BatchApiRequest.SendBatchApiRequest();

                // Validate the response to the batch API request
                ValidateBatchApiCallResponses(batchResponses, 10);

                // Validate the response to the individual API calls
                ValidateIndividualBatchedApiCallResponse <Template>(batchResponses[0]);
                ValidateIndividualBatchedApiCallResponse <List <Snippet> >(batchResponses[1]);
                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[2]);
                ValidateIndividualBatchedApiCallResponse <RenderTemplateResponse>(batchResponses[3]);
                ValidateIndividualBatchedApiCallResponse <List <Log> >(batchResponses[4]);
                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[5]);
                ValidateIndividualBatchedApiCallResponse <EmailResponse>(batchResponses[6]);
                ValidateIndividualBatchedApiCallResponse <List <DripCampaignDetails> >(batchResponses[7]);
                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[8]);
                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[9]);
            }
            catch (AggregateException exception)
            {
                Assert.Fail(exception.ToString());
            }
        }
        private async Task <BatchRequestResponse> SendBatchRequestAsync(Func <Task> executeRequests)
        {
            BatchApiRequest.StartNewBatchRequest();

            try
            {
                bool isBatchSuccess = true;
                var  data           = new List <object>();

                await executeRequests();

                List <BatchApiResponse> batchResponses = await BatchApiRequest.SendBatchApiRequest();

                for (int i = 0; i < batchResponses.Count; i++)
                {
                    BatchApiResponse batchResponse = batchResponses[i];
                    if (batchResponse.status_code != 200)
                    {
                        isBatchSuccess = false;
                        BatchApiRequest.AbortBatchRequest();
                        break;
                    }
                    var body = batchResponses[i].GetBody <object>();
                    data.Add(body);
                }

                var response = new BatchRequestResponse(isBatchSuccess, "200", data.ToJson());
                return(response);
            }
            catch (AggregateException ex)
            {
                BatchApiRequest.AbortBatchRequest();
                return(new BatchRequestResponse(false, "500", null, ex.ToString()));
            }
            catch (InvalidOperationException ex)
            {
                BatchApiRequest.AbortBatchRequest();
                return(new BatchRequestResponse(false, "500", null, ex.ToString()));
            }
        }
        public async Task TestBatchApiRequestsPauseAndResumeRequestAsync()
        {
            Trace.WriteLine("POST /batch");

            // Start the batch request
            BatchApiRequest.StartNewBatchRequest();

            try
            {
                // Make the API call to be batched
                await Template.GetTemplatesAsync();

                // Pause the batch request
                BatchApiRequest.PauseBatchRequest();

                // Make another API call and make sure it goes through
                var snippets = await Snippet.GetSnippetsAsync();

                SendwithusClientTest.ValidateResponse(snippets);

                // Resume the batch request and add another API call to it
                BatchApiRequest.ResumeBatchRequest();
                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS);

                // Make the final batch request
                var batchResponses = await BatchApiRequest.SendBatchApiRequest();

                // Make sure both API calls were included in the batch request
                ValidateBatchApiCallResponses(batchResponses, 2);

                // Valideate each of those requests
                ValidateIndividualBatchedApiCallResponse <List <Template> >(batchResponses[0]);
                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[1]);
            }
            catch (AggregateException exception)
            {
                Assert.Fail(exception.ToString());
            }
        }
        public async Task TestBatchApiRequestsTwelveRequestsWithoutOverrideAsync()
        {
            Trace.WriteLine("POST /batch");

            // Start the batch request
            BatchApiRequest.StartNewBatchRequest();

            // Override the maximum number of API calls that can be included in this batch
            BatchApiRequest.OverrideMaximumBatchRequests(12);

            // Make the API calls to be batched (at least one of each type)
            try
            {
                await TemplateTest.BuildAndSendCreateTemplateRequestWithAllParametersAsync(); // POST

                await Snippet.GetSnippetsAsync();                                             // GET

                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET

                await RenderTest.BuildAndSendRenderTemplateRequestWithAllParametersId();      // POST

                await Log.GetLogEventsAsync(BatchApiRequestTest.DEFAULT_LOG_ID);              // GET

                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET

                await EmailTest.BuildAndSendEmailWithAllParametersAsync();                    // POST

                await DripCampaign.GetDripCampaignsAsync();                                   // GET

                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET

                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET

                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET

                // Make the batch Api Request
                var batchResponses = await BatchApiRequest.SendBatchApiRequest();

                // Validate the response to the batch API request
                ValidateBatchApiCallResponses(batchResponses, 11);

                // Validate the response to the individual API calls
                ValidateIndividualBatchedApiCallResponse <Template>(batchResponses[0]);
                ValidateIndividualBatchedApiCallResponse <List <Snippet> >(batchResponses[1]);
                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[2]);
                ValidateIndividualBatchedApiCallResponse <RenderTemplateResponse>(batchResponses[3]);
                ValidateIndividualBatchedApiCallResponse <List <Log> >(batchResponses[4]);
                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[5]);
                ValidateIndividualBatchedApiCallResponse <EmailResponse>(batchResponses[6]);
                ValidateIndividualBatchedApiCallResponse <List <DripCampaignDetails> >(batchResponses[7]);
                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[8]);
                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[9]);
                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[10]);
            }
            catch (InvalidOperationException exception)
            {
                Trace.WriteLine(String.Format("Successfully caught exception triggered by adding too many API calls to the batch API request. Error message: {0}", exception.Message));
                Assert.IsTrue(true);
            }
            finally
            {
                // Return the max batch request limit to its default value
                BatchApiRequest.SetMaximumBatchRequestsToDefault();
            }
        }
        public async Task TestBatchApiRequestsElevenRequestsWithoutOverrideAsync()
        {
            Trace.WriteLine("POST /batch");

            // Start the batch request
            BatchApiRequest.StartNewBatchRequest();

            try
            {
                // Make the API calls to be batched (at least one of each type)
                await TemplateTest.BuildAndSendCreateTemplateRequestWithAllParametersAsync(); // POST

                await Snippet.GetSnippetsAsync();                                             // GET

                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET

                await RenderTest.BuildAndSendRenderTemplateRequestWithAllParametersId();      // POST

                await Log.GetLogEventsAsync(BatchApiRequestTest.DEFAULT_LOG_ID);              // GET

                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET

                await EmailTest.BuildAndSendEmailWithAllParametersAsync();                    // POST

                await DripCampaign.GetDripCampaignsAsync();                                   // GET

                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET

                await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET

                // Add the 11th API Request
                try
                {
                    await Customer.GetCustomerAsync(CustomerTest.DEFAULT_CUSTOMER_EMAIL_ADDRESS); // GET
                }
                catch (InvalidOperationException exception)
                {
                    Trace.WriteLine(String.Format("Successfully caught exception triggered by adding too many API calls to the batch API request. Error message: {0}", exception.Message));
                    Assert.IsTrue(true);
                }

                // Send the batch AP Request and make sure it still goes through (with the previous 10 requests included)
                var batchResponses = await BatchApiRequest.SendBatchApiRequest();

                // Validate the response to the batch API request
                ValidateBatchApiCallResponses(batchResponses, 10);

                // Validate the response to the individual API calls
                ValidateIndividualBatchedApiCallResponse <Template>(batchResponses[0]);
                ValidateIndividualBatchedApiCallResponse <List <Snippet> >(batchResponses[1]);
                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[2]);
                ValidateIndividualBatchedApiCallResponse <RenderTemplateResponse>(batchResponses[3]);
                await Log.GetLogEventsAsync(BatchApiRequestTest.DEFAULT_LOG_ID); // GET

                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[5]);
                ValidateIndividualBatchedApiCallResponse <EmailResponse>(batchResponses[6]);
                ValidateIndividualBatchedApiCallResponse <List <DripCampaignDetails> >(batchResponses[7]);
                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[8]);
                ValidateIndividualBatchedApiCallResponse <Customer>(batchResponses[9]);
            }
            catch (AggregateException exception)
            {
                Assert.Fail(exception.ToString());
            }
        }