/// <remarks>
 /// I'm not a huge fan of this manual cloning.
 /// However, I'm choosing this over taking a dependency on a mapper
 /// or performing a deep clone that includes the Recipients collection unnecessarily.
 /// </remarks>
 private static TransactMessage CloneWithoutRecipients(TransactMessage message)
 {
     return(new TransactMessage()
     {
         CampaignId = message.CampaignId,
         TransactionId = message.TransactionId,
         ShowAllSendDetail = message.ShowAllSendDetail,
         SendAsBatch = message.SendAsBatch,
         NoRetryOnFailure = message.NoRetryOnFailure,
         SaveColumns = message.SaveColumns,
     });
 }
            public void ThrowsForTooManyRecipients()
            {
                var message = new TransactMessage()
                {
                    Recipients = Enumerable.Range(0, TransactClientConfiguration.MaxRecipientsPerNonBatchRequest + 1)
                        .Select(x => new TransactMessageRecipient())
                        .ToList()
                };

                var exception = Assert.Throws<ArgumentException>(
                    () => new TransactClientTester().SendMessage(message));

                Assert.Equal(TransactClient.ErrorExceededNonBatchRecipients, exception.Message);
            }
        public async Task<TransactMessageResponse> SendMessageAsync(TransactMessage message)
        {
            if (message == null) throw new ArgumentNullException("message");

            SendMessagePreCommunicationVerification(message);

            var encodedMessage = _encoder.Encode(message);

            string response;
            using (var silverpop = _silverpopFactory())
            {
                response = await silverpop.HttpUploadAsync(encodedMessage);
            }

            var decodedResponse = _decoder.Decode(response);

            if (decodedResponse.Status == TransactMessageResponseStatus.EncounteredErrorsNoMessagesSent)
                throw new TransactClientException(
                    decodedResponse.Error.Value, encodedMessage, decodedResponse.RawResponse);

            return decodedResponse;
        }
Exemplo n.º 4
0
        public virtual string Encode(TransactMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            var xml = new XElement(XName.Get("XTMAILING"));

            xml.SetElementValue(XName.Get("CAMPAIGN_ID"), message.CampaignId);
            xml.SetElementValue(XName.Get("TRANSACTION_ID"),
                                message.TransactionId ?? "dotnet-api-" + Guid.NewGuid().ToString());
            xml.SetElementValue(XName.Get("SHOW_ALL_SEND_DETAIL"), message.ShowAllSendDetail);
            xml.SetElementValue(XName.Get("SEND_AS_BATCH"), message.SendAsBatch);
            xml.SetElementValue(XName.Get("NO_RETRY_ON_FAILURE"), message.NoRetryOnFailure);

            // Add SAVE_COLUMNS
            var saveColumnsXml = new XElement(XName.Get("SAVE_COLUMNS"));

            foreach (var saveColumn in message.SaveColumns)
            {
                var columnName = new XElement(XName.Get("COLUMN_NAME"))
                {
                    Value = saveColumn
                };

                saveColumnsXml.Add(columnName);
            }
            xml.Add(saveColumnsXml);

            // Add RECIPIENT nodes
            foreach (var recipient in message.Recipients)
            {
                var recipientXml = new XElement(XName.Get("RECIPIENT"));

                recipientXml.SetElementValue(XName.Get("EMAIL"), recipient.EmailAddress);

                var bodyType = recipient.BodyType ?? Constants.TransactMessageBodyTypeDefault;
                recipientXml.SetElementValue(XName.Get("BODY_TYPE"), bodyType.ToString().ToUpper());

                // Add PERSONALIZATION nodes for RECIPIENT
                foreach (var personalizationTag in recipient.PersonalizationTags)
                {
                    if (string.IsNullOrWhiteSpace(personalizationTag.Name))
                    {
                        throw new ArgumentException(
                                  "TransactMessageRecipientPersonalizationTag items must have a valid Name set.");
                    }

                    var personalizationXml = new XElement(XName.Get("PERSONALIZATION"));

                    personalizationXml.SetElementValue(XName.Get("TAG_NAME"), personalizationTag.Name);

                    // Prevent usage of XML CDATA sections,
                    // if there is reason to allow these it can be revisited.
                    if (ContainsCDATASection(personalizationTag.Value))
                    {
                        throw new ArgumentException(
                                  "XML CDATA sections should not be used in PersonalizationTags values.");
                    }

                    personalizationXml.SetElementValue(
                        XName.Get("VALUE"),
                        personalizationTag.Value ?? string.Empty);

                    recipientXml.Add(personalizationXml);
                }

                xml.Add(recipientXml);
            }

            return(xml.ToString());
        }
 public override string Encode(TransactMessage message)
 {
     return _encodeOutput;
 }
        /// <returns>Filenames that can be used for checking status of batches.</returns>
        public IEnumerable<string> SendMessageBatch(TransactMessage message)
        {
            if (message == null) throw new ArgumentNullException("message");

            MessageBatchPreCommunicationVerification();

            var filenames = new List<string>();

            using (var silverpop = _silverpopFactory())
            {
                var batchedMessages = message.GetRecipientBatchedMessages(
                    TransactClientConfiguration.MaxRecipientsPerBatchRequest);

                var identifier = Guid.NewGuid().ToString();

                foreach (var batchMessage in batchedMessages)
                {
                    var encodedMessage = _encoder.Encode(batchMessage);

                    var filename = string.Format(
                        "{0}.{1}.xml.gz",
                        identifier,
                        filenames.Count() + 1);

                    silverpop.SftpGzipUpload(
                        encodedMessage,
                        "transact/temp/" + filename);

                    silverpop.SftpMove(
                        "transact/temp/" + filename,
                        "transact/inbound/" + filename);

                    filenames.Add(filename + ".status");
                }
            }

            return filenames;
        }
        private void SendMessagePreCommunicationVerification(TransactMessage message)
        {
            if (message.Recipients.Count() > TransactClientConfiguration.MaxRecipientsPerNonBatchRequest)
                throw new ArgumentException(ErrorExceededNonBatchRecipients);

            if (!Configuration.PodNumber.HasValue)
                throw new ApplicationException(ErrorMissingPodNumber);
        }
        public virtual string Encode(TransactMessage message)
        {
            if (message == null) throw new ArgumentNullException("message");

            var xml = new XElement(XName.Get("XTMAILING"));

            xml.SetElementValue(XName.Get("CAMPAIGN_ID"), message.CampaignId);
            xml.SetElementValue(XName.Get("TRANSACTION_ID"),
                message.TransactionId ?? "dotnet-api-" + Guid.NewGuid().ToString());
            xml.SetElementValue(XName.Get("SHOW_ALL_SEND_DETAIL"), message.ShowAllSendDetail);
            xml.SetElementValue(XName.Get("SEND_AS_BATCH"), message.SendAsBatch);
            xml.SetElementValue(XName.Get("NO_RETRY_ON_FAILURE"), message.NoRetryOnFailure);

            // Add SAVE_COLUMNS
            var saveColumnsXml = new XElement(XName.Get("SAVE_COLUMNS"));
            foreach (var saveColumn in message.SaveColumns)
            {
                var columnName = new XElement(XName.Get("COLUMN_NAME"))
                {
                    Value = saveColumn
                };

                saveColumnsXml.Add(columnName);
            }
            xml.Add(saveColumnsXml);

            // Add RECIPIENT nodes
            foreach (var recipient in message.Recipients)
            {
                var recipientXml = new XElement(XName.Get("RECIPIENT"));

                recipientXml.SetElementValue(XName.Get("EMAIL"), recipient.EmailAddress);

                var bodyType = recipient.BodyType ?? Constants.TransactMessageBodyTypeDefault;
                recipientXml.SetElementValue(XName.Get("BODY_TYPE"), bodyType.ToString().ToUpper());

                // Add PERSONALIZATION nodes for RECIPIENT
                foreach (var personalizationTag in recipient.PersonalizationTags)
                {
                    if (string.IsNullOrWhiteSpace(personalizationTag.Name))
                        throw new ArgumentException(
                            "TransactMessageRecipientPersonalizationTag items must have a valid Name set.");

                    var personalizationXml = new XElement(XName.Get("PERSONALIZATION"));

                    personalizationXml.SetElementValue(XName.Get("TAG_NAME"), personalizationTag.Name);

                    // Prevent usage of XML CDATA sections,
                    // if there is reason to allow these it can be revisited.
                    if (ContainsCDATASection(personalizationTag.Value))
                        throw new ArgumentException(
                            "XML CDATA sections should not be used in PersonalizationTags values.");

                    personalizationXml.SetElementValue(
                        XName.Get("VALUE"),
                        personalizationTag.Value ?? string.Empty);

                    recipientXml.Add(personalizationXml);
                }

                xml.Add(recipientXml);
            }

            return xml.ToString();
        }
 /// <remarks>
 /// I'm not a huge fan of this manual cloning.
 /// However, I'm choosing this over taking a dependency on a mapper
 /// or performing a deep clone that includes the Recipients collection unnecessarily.
 /// </remarks>
 private static TransactMessage CloneWithoutRecipients(TransactMessage message)
 {
     return new TransactMessage()
     {
         CampaignId = message.CampaignId,
         TransactionId = message.TransactionId,
         ShowAllSendDetail = message.ShowAllSendDetail,
         SendAsBatch = message.SendAsBatch,
         NoRetryOnFailure = message.NoRetryOnFailure,
         SaveColumns = message.SaveColumns,
     };
 }