/// <summary>
        /// Unsubscribe communications in MS-Individual Using Tool Service
        /// </summary>
        /// <param name="email">email address</param>
        /// <param name="communicationId">CommunicationId to Unsubscribe</param>
        /// <param name="deliveryFormatId">0 = Html , 1 = Text</param>
        /// <returns>Returns the EmailInterchangeResult alogn with the EI ID</returns>
        public AzureTBNService.EmailInterchangeResponseToken UnsubscribeV2(string email, int communicationId, int deliveryFormatId)
        {
            AzureTBNService.EmailInterchangeResponseToken emailInterchangeResponseToken = new AzureTBNService.EmailInterchangeResponseToken();
            try
            {
                AzureTBNService.TriggerRequestNotificationClient client = new AzureTBNService.TriggerRequestNotificationClient(PrimaryEndpoint);

                // Input Validations
                if (string.IsNullOrWhiteSpace(email))
                {
                    throw new ArgumentNullException("Email");
                }
                else if (!Regex.IsMatch(email, EmailRegExPattern))
                {
                    throw new ArgumentException("Invalid Email");
                }

                if (communicationId < 0)
                {
                    throw new ArgumentException("Invalid CommunicationId");
                }

                emailInterchangeResponseToken = client.UnsubscribeV2(email, communicationId, deliveryFormatId);
            }
            catch (Exception ex)
            {
                throw new Exception(MSIExceptionMessagePredicate + ex.Message.ToString());
            }

            return(emailInterchangeResponseToken);
        }
        /// <summary>
        /// Method for Calling the Azure Endpoint.
        /// </summary>
        /// <param name="request">Request that is passed by Tenant.</param>
        /// <returns>Returns the EmailDataList to Calling method.</returns>
        internal AzureTBNService.EmailDataList CallGetEmailDetails(AzureTBNService.RequestForReturnEmailList request, out Guid requestID)
        {
            int exceptionCount = 0;

            AzureTBNService.EmailDataList azure_EmailDataList       = null;
            AzureTBNService.TriggerRequestNotificationClient client = null;
            for (int retryCount = 0; retryCount < retryAttempts; retryCount++)
            {
                try
                {
                    using (client = new AzureTBNService.TriggerRequestNotificationClient(PrimaryEndpoint))
                    {
                        azure_EmailDataList = client.GetEmailDetailsV2(out requestID, request as AzureTBNService.RequestForReturnEmailList);
                    }
                    return(azure_EmailDataList);
                }
                catch (Exception ex)
                {
                    if (client == null)
                    {
                        throw;
                    }

                    exceptionCount++;

                    // Throw exception if maxm retry attempts are exhausted
                    if (exceptionCount == retryAttempts)
                    {
                        throw new Exception(GenericExceptionMessagePredicate + ex.Message.ToString());
                    }
                }
            }
            requestID = Guid.Empty;
            return(azure_EmailDataList);
        }
        /// <summary>
        /// Function retrives Email Tracking Summary Details.
        /// </summary>
        /// <typeparam name="TInput">EmailTrackingInputOfBulkSendSummaryInput/EmailTrackingInputOfBulkSendSummaryInput</typeparam>
        /// <typeparam name="TOutput">EmailTrackingOutputOfBulkSendSummaryDetails/EmailTrackingOutputOfTriggerSendSummaryDetails</typeparam>
        /// <param name="emailTracking"></param>
        /// <returns></returns>
        public TOutput GetEmailTrackingSummaryData <TInput, TOutput>(TInput emailTracking, out System.Guid requestID)
        {
            AzureTBNService.TriggerRequestNotificationClient client = null;
            AzureTBNService.EmailTrackingOutputOfTriggerSendSummaryDetails triggerSummary = null;
            AzureTBNService.EmailTrackingOutputOfBulkSendSummaryDetails    bulkSummary    = null;
            bool   IsBulkSend;
            int    exceptionCount = 0;
            object retrunType     = null;

            if (emailTracking == null)
            {
                throw new Exception("Null reference exception.");
            }

            using (client = new AzureTBNService.TriggerRequestNotificationClient(PrimaryEndpoint))
            {
                //Check tenant requesting for Trigger send or Batcg send.
                IsBulkSend = (emailTracking.GetType().IsEquivalentTo(typeof(AzureTBNService.EmailTrackingInputOfBulkSendSummaryInput)) ? true : false);
                //3 retry attempts.
                for (int retryCount = 0; retryCount < retryAttempts; retryCount++)
                {
                    try
                    {
                        if (IsBulkSend)
                        {
                            //Invoke bulk send.
                            bulkSummary = client.GetEmailTrackingBulkSendSummaryDataV2(out requestID, emailTracking as AzureTBNService.EmailTrackingInputOfBulkSendSummaryInput);
                            retrunType  = bulkSummary;
                            break;
                        }
                        else
                        {
                            //Invoke Trigger send.
                            triggerSummary = client.GetEmailTrackingTriggerSendSummaryDataV2(out requestID, emailTracking as AzureTBNService.EmailTrackingInputOfTriggerSendSummaryInput);
                            retrunType     = triggerSummary;
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (client == null)
                        {
                            throw;
                        }

                        exceptionCount++;

                        // Throw exception if maxm retry attempts are exhausted
                        if (exceptionCount == retryAttempts)
                        {
                            throw new Exception(GenericExceptionMessagePredicate + ex.Message.ToString());
                        }
                    }
                }
            }
            requestID = Guid.Empty;
            return((TOutput)retrunType);
        }
        public AzureTBNService.DataFolderList GetFolderHierarchy(AzureTBNService.RequestForRetrieveFolder folderRequest, out System.Guid requestID)
        {
            AzureTBNService.TriggerRequestNotificationClient client = null;
            AzureTBNService.DataFolderList folderList = null;
            int exceptionCount = 0;

            if (folderRequest == null)
            {
                throw new ArgumentNullException(ErrorExceptionMessagePredicate + RequestNullException);
            }

            if (folderRequest.ExactTargetAccountId <= 0)
            {
                throw new Exception("Invalid AccountId.");
            }


            for (int retryCount = 0; retryCount < retryAttempts; retryCount++)
            {
                try
                {
                    using (client = new AzureTBNService.TriggerRequestNotificationClient(PrimaryEndpoint))
                    {
                        folderList = client.GetFolderHierarchyV2(out requestID, folderRequest);
                    }

                    return(folderList);
                }
                catch (Exception ex)
                {
                    if (client == null)
                    {
                        throw;
                    }

                    exceptionCount++;

                    // Throw exception if maxm retry attempts are exhausted
                    if (exceptionCount == retryAttempts)
                    {
                        throw new Exception(GenericExceptionMessagePredicate + ex.Message.ToString());
                    }
                }
            }
            requestID = Guid.Empty;
            return(folderList);
        }
        private static bool FileUpload(string filePath)
        {
            int bufferSize = (2 * 1024 * 1024);
            int readBuffer = 0;

            byte[]        bufferBytes = new byte[bufferSize], tempBuffer;
            DateTime      start = DateTime.Now, end = DateTime.Now;
            List <string> blockIds = new List <string>();
            long          fileSize = 0;
            string        fileName = string.Empty;

            //double elapsed = 0;

            fileName = Path.GetFileName(filePath);
            FileStream fileStream = File.OpenRead(filePath);

            fileSize = fileStream.Length;
            int blockCount = (int)(fileStream.Length / bufferSize) + 1;

            Parallel.For(0, blockCount, i =>
            {
                readBuffer            = fileStream.Read(bufferBytes, 0, bufferSize);
                string currentBlockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));
                blockIds.Add(currentBlockId);
                if (readBuffer < bufferSize)
                {
                    tempBuffer = new byte[readBuffer];
                    Array.Copy(bufferBytes, tempBuffer, readBuffer);
                    bufferBytes = tempBuffer;
                }

                using (AzureTBNService.TriggerRequestNotificationClient client = new AzureTBNService.TriggerRequestNotificationClient(PrimaryEndpoint))
                {
                    client.PutBlock(fileName, currentBlockId, Convert.ToBase64String(bufferBytes));
                }
            });

            using (AzureTBNService.TriggerRequestNotificationClient client = new AzureTBNService.TriggerRequestNotificationClient(PrimaryEndpoint))
            {
                client.PutBlockList(fileName, blockIds.ToArray());
            }

            return(true);
        }
        /// <summary>
        /// Method for Calling the Azure(Primary/Secondary) Endpoint.
        /// </summary>
        /// <param name="request">Request that is passed by Tenant.</param>
        /// <param name="endpointName">Azure endpoint to be used((Primary/Secondary).</param>
        /// <returns>Returns the Request Acceptance Message along with the EI ID to Calling method.</returns>
        internal AzureTBNService.EmailInterchangeResponseToken CallSend(AzureTBNService.RequestBase request, string endpointName)
        {
            int exceptionCount = 0;

            AzureTBNService.EmailInterchangeResponseToken emailInterchangeResponseToken = new AzureTBNService.EmailInterchangeResponseToken();
            emailInterchangeResponseToken.Result = AzureTBNService.EmailInterchangeResult.UnknownFailure;

            AzureTBNService.TriggerRequestNotificationClient client = null;
            for (int retryCount = 0; retryCount < retryAttempts; retryCount++)
            {
                try
                {
                    using (client = new AzureTBNService.TriggerRequestNotificationClient(endpointName))
                    {
                        emailInterchangeResponseToken = client.SendV2(request);
                    }

                    if (emailInterchangeResponseToken.Result != AzureTBNService.EmailInterchangeResult.UnknownFailure)
                    {
                        return(emailInterchangeResponseToken);
                    }
                }

                catch (Exception ex)
                {
                    if (client == null)
                    {
                        throw;
                    }

                    exceptionCount++;

                    // Throw exception if maxm retry attempts are exhausted
                    if (exceptionCount == retryAttempts)
                    {
                        throw new Exception(GenericExceptionMessagePredicate + ex.Message.ToString());
                    }
                }
            }
            return(emailInterchangeResponseToken);
        }
        /// <summary>
        /// Method for Calling the Azure(Primary/Secondary) Endpoint.
        /// </summary>
        /// <param name="requestId">RequestId that is passed by Tenant.</param>
        /// <param name="endpointName">Azure endpoint to be used((Primary/Secondary).</param>
        /// <returns>Returns the user friendly message along with the Event ID to the Calling method.</returns>
        internal AzureTBNService.InterchangeRequestStatus CallGetRequestStatus(System.Guid emailInterchangeId, string endpointName, AzureTBNService.RequestTypeForEnhancedAPI requestType)
        {
            int exceptionCount = 0;

            AzureTBNService.InterchangeRequestStatus         interchangeRequestStatus = new AzureTBNService.InterchangeRequestStatus();
            AzureTBNService.TriggerRequestNotificationClient client = null;

            for (int retryCount = 0; retryCount < retryAttempts; retryCount++)
            {
                try
                {
                    using (client = new AzureTBNService.TriggerRequestNotificationClient(endpointName))
                    {
                        interchangeRequestStatus = client.GetRequestStatus(emailInterchangeId, requestType);
                    }

                    if (string.IsNullOrEmpty(interchangeRequestStatus.RequestStatusMessage))
                    {
                        interchangeRequestStatus.RequestStatusMessage = EmptyRequestStatusMessage;
                    }
                    break;
                }
                catch (Exception ex)
                {
                    if (client == null)
                    {
                        throw;
                    }
                    exceptionCount++;

                    // Throw exception if maxm retry attempts are exhausted
                    if (exceptionCount == retryAttempts)
                    {
                        throw new Exception(GenericExceptionMessagePredicate + ex.Message.ToString());
                    }
                }
            }
            return(interchangeRequestStatus);
        }
        /// <summary>
        /// Method for Calling the Azure Endpoint.
        /// </summary>
        /// <param name="request">Request that is passed by Tenant.</param>
        /// <param name="endpointName">Azure endpoint to be used((Primary/Secondary).</param>
        /// <returns>Returns the FileRequestSendResult to Calling method.</returns>
        internal AzureTBNService.FileRequestSendResult CallSendFileRequest(AzureTBNService.FileRequest request)
        {
            int exceptionCount = 0;

            AzureTBNService.FileRequestSendResult            azure_FileRequestSendResult = new AzureTBNService.FileRequestSendResult();
            AzureTBNService.TriggerRequestNotificationClient client = null;
            for (int retryCount = 0; retryCount < retryAttempts; retryCount++)
            {
                try
                {
                    if (FileUpload((request as AzureTBNService.FileRequest).FilePath))
                    {
                        using (client = new AzureTBNService.TriggerRequestNotificationClient(PrimaryEndpoint))
                        {
                            azure_FileRequestSendResult = client.SendFileRequest(request as AzureTBNService.FileRequest);
                        }
                        return(azure_FileRequestSendResult);
                    }
                }

                catch (Exception ex)
                {
                    if (client == null)
                    {
                        throw;
                    }

                    exceptionCount++;

                    // Throw exception if maxm retry attempts are exhausted
                    if (exceptionCount == retryAttempts)
                    {
                        throw new Exception(GenericExceptionMessagePredicate + ex.Message.ToString());
                    }
                }
            }
            return(azure_FileRequestSendResult);
        }
        public AzureTBNService.EmailTrackingOutputOfEmilTrackingOutputDetails GetEmailTrackingStatusData(AzureTBNService.EmailTrackingInputOfEmailTrackingInputCriteria emailTracking, out System.Guid requestID)
        {
            AzureTBNService.TriggerRequestNotificationClient client = null;
            int exceptionCount = 0;

            if (emailTracking == null)
            {
                throw new Exception("Null reference exception.");
            }
            using (client = new AzureTBNService.TriggerRequestNotificationClient(PrimaryEndpoint))
            {
                for (int retryCount = 0; retryCount < retryAttempts; retryCount++)
                {
                    try
                    {
                        return(client.GetEmailTrackingDataV2(out requestID, emailTracking));
                    }
                    catch (Exception ex)
                    {
                        if (client == null)
                        {
                            throw;
                        }

                        exceptionCount++;

                        // Throw exception if maxm retry attempts are exhausted
                        if (exceptionCount == retryAttempts)
                        {
                            throw new Exception(GenericExceptionMessagePredicate + ex.Message.ToString());
                        }
                    }
                }
            }
            requestID = Guid.Empty;
            return(null);
        }