public async Task RunAsync(ILegacyClient client, TextWriter log, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return; } await log.WriteLineAsync("\n***** LEGACY CATEGORIES *****\n").ConfigureAwait(false); var categories = await client.Categories.GetAsync(null, 50, 0, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Number of categories: {categories.Length}").ConfigureAwait(false); await log.WriteLineAsync($"Categories: {string.Join(", ", categories)}").ConfigureAwait(false); }
public async Task RunAsync(ILegacyClient client, TextWriter log, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return; } await log.WriteLineAsync("\n***** LEGACY CONTACTS AND CUSTOM FIELDS *****\n").ConfigureAwait(false); // GET ALL FIELDS var fields = await client.CustomFields.GetAllAsync(null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"All custom fields retrieved. There are {fields.Length} fields").ConfigureAwait(false); // CLEANUP PREVIOUS INTEGRATION TESTS THAT MIGHT HAVE BEEN INTERRUPTED BEFORE THEY HAD TIME TO CLEANUP AFTER THEMSELVES var cleanUpTasks = fields .Where(f => f.Name.StartsWith("stronggrid_")) .Select(async oldField => { await client.CustomFields.DeleteAsync(oldField.Id, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Field {oldField.Id} deleted").ConfigureAwait(false); await Task.Delay(250).ConfigureAwait(false); // Brief pause to ensure SendGrid has time to catch up }); await Task.WhenAll(cleanUpTasks).ConfigureAwait(false); var nicknameField = await client.CustomFields.CreateAsync("stronggrid_nickname", FieldType.Text, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Field '{nicknameField.Name}' Id: {nicknameField.Id}").ConfigureAwait(false); var ageField = await client.CustomFields.CreateAsync("stronggrid_age", FieldType.Number, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Field '{ageField.Name}' Id: {ageField.Id}").ConfigureAwait(false); var customerSinceField = await client.CustomFields.CreateAsync("stronggrid_customer_since", FieldType.Date, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Field '{customerSinceField.Name}' Id: {customerSinceField.Id}").ConfigureAwait(false); fields = await client.CustomFields.GetAllAsync(null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"All custom fields retrieved. There are {fields.Length} fields").ConfigureAwait(false); var email = "*****@*****.**"; var firstName = "Robert"; var lastName = "Unknown"; var customFields = new Models.Legacy.Field[] { new Models.Legacy.Field <string>("stronggrid_nickname", "Bob"), new Models.Legacy.Field <long?>("stronggrid_age", 42), new Models.Legacy.Field <DateTime>("stronggrid_customer_since", new DateTime(2000, 12, 1)) }; var contactId = await client.Contacts.CreateAsync(email, firstName, lastName, customFields, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Contact {contactId} created: {firstName} {lastName}").ConfigureAwait(false); var newLastName = "Smith"; await client.Contacts.UpdateAsync(email, null, newLastName, cancellationToken : cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Contact {contactId} updated: {firstName} {newLastName}").ConfigureAwait(false); var contact = await client.Contacts.GetAsync(contactId, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Retrieved contact {contactId}").ConfigureAwait(false); await log.WriteLineAsync($"\tEmail: {contact.Email}").ConfigureAwait(false); await log.WriteLineAsync($"\tFirst Name: {contact.FirstName}").ConfigureAwait(false); await log.WriteLineAsync($"\tLast Name: {contact.LastName}").ConfigureAwait(false); await log.WriteLineAsync($"\tCreated On:{contact.CreatedOn}").ConfigureAwait(false); await log.WriteLineAsync($"\tModified On: {contact.ModifiedOn}").ConfigureAwait(false); await log.WriteLineAsync($"\tLast Clicked On: {contact.LastClickedOn}").ConfigureAwait(false); await log.WriteLineAsync($"\tLast Emailed On: {contact.LastEmailedOn}").ConfigureAwait(false); await log.WriteLineAsync($"\tLast Opened On: {contact.LastOpenedOn}").ConfigureAwait(false); foreach (var customField in contact.CustomFields.OfType <Models.Legacy.Field <string> >()) { await log.WriteLineAsync($"\t{customField.Name}: {customField.Value}").ConfigureAwait(false); } foreach (var customField in contact.CustomFields.OfType <Models.Legacy.Field <long?> >()) { await log.WriteLineAsync($"\t{customField.Name}: {customField.Value}").ConfigureAwait(false); } foreach (var customField in contact.CustomFields.OfType <Models.Legacy.Field <DateTime?> >()) { await log.WriteLineAsync($"\t{customField.Name}: {customField.Value}").ConfigureAwait(false); } var recordsPerPage = 5; var contacts = await client.Contacts.GetAsync(recordsPerPage, 1, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync(contacts.Length < recordsPerPage?$"Found {contacts.Length} contacts" : $"Retrieved the first {recordsPerPage} contacts").ConfigureAwait(false); foreach (var record in contacts) { await log.WriteLineAsync($"\t{record.FirstName} {record.LastName}").ConfigureAwait(false); } var firstNameCondition = new SearchCondition { Field = "first_name", Value = "Robert", Operator = ConditionOperator.Equal, LogicalOperator = LogicalOperator.None }; var LastNameCondition = new SearchCondition { Field = "last_name", Value = "Smith", Operator = ConditionOperator.Equal, LogicalOperator = LogicalOperator.And }; var searchResult = await client.Contacts.SearchAsync(new[] { firstNameCondition, LastNameCondition }, null, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Found {searchResult.Length} contacts named Robert Smith").ConfigureAwait(false); var billableCount = await client.Contacts.GetBillableCountAsync(null, cancellationToken).ConfigureAwait(false); var totalCount = await client.Contacts.GetTotalCountAsync(null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync("Record counts").ConfigureAwait(false); await log.WriteLineAsync($"\tBillable: {billableCount}").ConfigureAwait(false); await log.WriteLineAsync($"\tTotal: {totalCount}").ConfigureAwait(false); await client.Contacts.DeleteAsync(contactId, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Contact {contactId} deleted: {firstName} {newLastName}").ConfigureAwait(false); await client.CustomFields.DeleteAsync(nicknameField.Id, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Field {nicknameField.Id} deleted").ConfigureAwait(false); await client.CustomFields.DeleteAsync(ageField.Id, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Field {ageField.Id} deleted").ConfigureAwait(false); await client.CustomFields.DeleteAsync(customerSinceField.Id, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Field {customerSinceField.Id} deleted").ConfigureAwait(false); fields = await client.CustomFields.GetAllAsync(null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"All custom fields retrieved. There are {fields.Length} fields").ConfigureAwait(false); }
public async Task RunAsync(ILegacyClient client, TextWriter log, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return; } await log.WriteLineAsync("\n***** LEGACY LISTS AND SEGMENTS *****\n").ConfigureAwait(false); // GET LISTS var lists = await client.Lists.GetAllAsync(null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"All lists retrieved. There are {lists.Length} lists").ConfigureAwait(false); // GET SEGMENTS var segments = await client.Segments.GetAllAsync(null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"All segements retrieved. There are {segments.Length} segments").ConfigureAwait(false); // CLEANUP PREVIOUS INTEGRATION TESTS THAT MIGHT HAVE BEEN INTERRUPTED BEFORE THEY HAD TIME TO CLEANUP AFTER THEMSELVES var cleanUpTasks = lists .Where(l => l.Name.StartsWith("StrongGrid Integration Testing:")) .Select(async oldList => { await client.Lists.DeleteAsync(oldList.Id, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"List {oldList.Id} deleted").ConfigureAwait(false); await Task.Delay(250).ConfigureAwait(false); // Brief pause to ensure SendGrid has time to catch up }) .Union(segments.Where(s => s.Name.StartsWith("StrongGrid Integration Testing:")) .Select(async oldSegment => { await client.Segments.DeleteAsync(oldSegment.Id, false, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Segment {oldSegment.Id} deleted").ConfigureAwait(false); await Task.Delay(250).ConfigureAwait(false); // Brief pause to ensure SendGrid has time to catch up })); await Task.WhenAll(cleanUpTasks).ConfigureAwait(false); // CREATE A LIST var list = await client.Lists.CreateAsync("StrongGrid Integration Testing: list #1", null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"List '{list.Name}' created. Id: {list.Id}").ConfigureAwait(false); // UPDATE THE LIST await client.Lists.UpdateAsync(list.Id, "StrongGrid Integration Testing: new name", null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"List '{list.Id}' updated").ConfigureAwait(false); // CREATE A SEGMENT var millerLastNameCondition = new SearchCondition { Field = "last_name", Operator = ConditionOperator.Equal, Value = "Miller", LogicalOperator = LogicalOperator.None }; var clickedRecentlyCondition = new SearchCondition { Field = "last_clicked", Operator = ConditionOperator.GreatherThan, Value = DateTime.UtcNow.AddDays(-30).ToString("MM/dd/yyyy"), LogicalOperator = LogicalOperator.And }; var segment = await client.Segments.CreateAsync("StrongGrid Integration Testing: Last Name is Miller and clicked recently", new[] { millerLastNameCondition, clickedRecentlyCondition }, list.Id, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Segment '{segment.Name}' created. Id: {segment.Id}").ConfigureAwait(false); // UPDATE THE SEGMENT var hotmailCondition = new SearchCondition { Field = "email", Operator = ConditionOperator.Contains, Value = "hotmail.com", LogicalOperator = LogicalOperator.None }; segment = await client.Segments.UpdateAsync(segment.Id, "StrongGrid Integration Testing: Recipients @ Hotmail", null, new[] { hotmailCondition }, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Segment {segment.Id} updated. The new name is: '{segment.Name}'").ConfigureAwait(false); // CREATE 3 CONTACTS var contactId1 = await client.Contacts.CreateAsync("*****@*****.**", "Bob", "Dummy1", null, null, cancellationToken).ConfigureAwait(false); var contactId2 = await client.Contacts.CreateAsync("*****@*****.**", "Bob", "Dummy2", null, null, cancellationToken).ConfigureAwait(false); var contactId3 = await client.Contacts.CreateAsync("*****@*****.**", "Bob", "Dummy3", null, null, cancellationToken).ConfigureAwait(false); // ADD THE CONTACTS TO THE LIST (THEY WILL AUTOMATICALLY BE INCLUDED IN THE HOTMAIL SEGMENT) await client.Lists.AddRecipientAsync(list.Id, contactId1, null, CancellationToken.None).ConfigureAwait(false); await client.Lists.AddRecipientsAsync(list.Id, new[] { contactId2, contactId3 }, null, CancellationToken.None).ConfigureAwait(false); // REMOVE THE CONTACTS FROM THE LIST (THEY WILL AUTOMATICALLY BE REMOVED FROM THE HOTMAIL SEGMENT) await client.Lists.RemoveRecipientAsync(list.Id, contactId3, null, CancellationToken.None).ConfigureAwait(false); await client.Lists.RemoveRecipientsAsync(list.Id, new[] { contactId1, contactId2 }, null, CancellationToken.None).ConfigureAwait(false); // DELETE THE CONTACTS await client.Contacts.DeleteAsync(contactId2, null, cancellationToken).ConfigureAwait(false); await client.Contacts.DeleteAsync(new[] { contactId1, contactId3 }, null, cancellationToken).ConfigureAwait(false); // DELETE THE SEGMENT await client.Segments.DeleteAsync(segment.Id, false, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Segment {segment.Id} deleted").ConfigureAwait(false); // DELETE THE LIST await client.Lists.DeleteAsync(list.Id, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"List {list.Id} deleted").ConfigureAwait(false); }
public async Task RunAsync(ILegacyClient client, TextWriter log, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return; } await log.WriteLineAsync("\n***** LEGACY CAMPAIGNS *****\n").ConfigureAwait(false); // GET CAMPAIGNS var campaigns = await client.Campaigns.GetAllAsync(100, 0, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"All campaigns retrieved. There are {campaigns.Length} campaigns").ConfigureAwait(false); // CLEANUP PREVIOUS INTEGRATION TESTS THAT MIGHT HAVE BEEN INTERRUPTED BEFORE THEY HAD TIME TO CLEANUP AFTER THEMSELVES var cleanUpTasks = campaigns .Where(c => c.Title.StartsWith("StrongGrid Integration Testing:")) .Select(async oldCampaign => { await client.Campaigns.DeleteAsync(oldCampaign.Id, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Campaign {oldCampaign.Id} deleted").ConfigureAwait(false); await Task.Delay(250, cancellationToken).ConfigureAwait(false); // Brief pause to ensure SendGrid has time to catch up }); await Task.WhenAll(cleanUpTasks).ConfigureAwait(false); var senderIdentities = await client.SenderIdentities.GetAllAsync(null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"All sender identities retrieved. There are {senderIdentities.Length} identities").ConfigureAwait(false); var sender = senderIdentities.FirstOrDefault(s => s.NickName == "Integration Testing identity"); if (sender == null) { sender = await client.SenderIdentities.CreateAsync("Integration Testing identity", new MailAddress(YOUR_EMAIL, "John Doe"), new MailAddress(YOUR_EMAIL, "John Doe"), "123 Main Street", null, "Small Town", "ZZ", "12345", "USA", null, cancellationToken).ConfigureAwait(false); throw new Exception($"A new sender identity was created and a verification email was sent to {sender.From.Email}. You must complete the verification process before proceeding."); } else if (!sender.Verification.IsCompleted) { throw new Exception($"A verification email was previously sent to {sender.From.Email} but the process hasn't been completed yet (hint: there is a link in the email that you must click on)."); } var lists = await client.Lists.GetAllAsync(null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"All lists retrieved. There are {lists.Length} lists").ConfigureAwait(false); var list = lists.FirstOrDefault(l => l.Name == "Integration testing list"); if (list == null) { list = await client.Lists.CreateAsync("Integration testing list", null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync("List created").ConfigureAwait(false); } var unsubscribeGroups = await((IBaseClient)client).UnsubscribeGroups.GetAllAsync(null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"All unsubscribe groups retrieved. There are {unsubscribeGroups.Length} groups").ConfigureAwait(false); var unsubscribeGroup = unsubscribeGroups.FirstOrDefault(l => l.Name == "Integration testing group"); if (unsubscribeGroup == null) { unsubscribeGroup = await((IBaseClient)client).UnsubscribeGroups.CreateAsync("Integration testing group", "For testing purposes", false, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync("Unsubscribe group created").ConfigureAwait(false); } var campaign = await client.Campaigns.CreateAsync("StrongGrid Integration Testing: new campaign", sender.Id, "This is the subject", "<html><body>Hello <b>World</b><p><a href='[unsubscribe]'>Click Here to Unsubscribe</a></p></body></html", "Hello world. To unsubscribe, visit [unsubscribe]", new[] { list.Id }, null, null, unsubscribeGroup.Id, null, null, EditorType.Design, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Campaign '{campaign.Title}' created. Id: {campaign.Id}").ConfigureAwait(false); await client.Campaigns.UpdateAsync(campaign.Id, categories : new[] { "category1", "category2" }, cancellationToken : cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Campaign '{campaign.Id}' updated").ConfigureAwait(false); campaigns = await client.Campaigns.GetAllAsync(100, 0, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"All campaigns retrieved. There are {campaigns.Length} campaigns").ConfigureAwait(false); await client.Campaigns.SendTestAsync(campaign.Id, new[] { YOUR_EMAIL }, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync("Test sent").ConfigureAwait(false); await client.Lists.DeleteAsync(list.Id, null, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync("List deleted").ConfigureAwait(false); await client.Campaigns.DeleteAsync(campaign.Id, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"Campaign {campaign.Id} deleted").ConfigureAwait(false); campaigns = await client.Campaigns.GetAllAsync(100, 0, cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"All campaigns retrieved. There are {campaigns.Length} campaigns").ConfigureAwait(false); }