private int CreateNewGroup(UnsubscribeGroup unsubscribeGroups) { string requestBody = JsonConvert.SerializeObject(unsubscribeGroups); Response response = _sendGridClient.RequestAsync(SendGridClient.Method.POST, requestBody, null, "asm/groups").Result; if (response != null && response.StatusCode == HttpStatusCode.Created) { string body = response.Body.ReadAsStringAsync().Result; var newGroup = JsonConvert.DeserializeObject <UnsubscribeGroup>(body); if (newGroup != null) { return(newGroup.id); } else { throw new Exception($"Unable to create group {unsubscribeGroups.name}"); } } else { throw new Exception("Invalid response from create group"); } }
public async Task <bool> SendDynamicEmail(string messageId, string templateName, string groupName, EmailBuildData emailBuildData) { var template = await GetTemplateWithCache(templateName, CancellationToken.None).ConfigureAwait(false); UnsubscribeGroup unsubscribeGroup = await GetUnsubscribeGroupWithCache(groupName, CancellationToken.None).ConfigureAwait(false); emailBuildData.BaseDynamicData.BaseUrl = _sendGridConfig.Value.BaseUrl; emailBuildData.BaseDynamicData.BaseCommunicationUrl = _sendGridConfig.Value.BaseCommunicationUrl; Personalization personalization = new Personalization() { Tos = new List <EmailAddress>() { new EmailAddress(emailBuildData.EmailToAddress, emailBuildData.EmailToName) }, TemplateData = emailBuildData.BaseDynamicData, }; var eml = new SendGridMessage() { From = new EmailAddress(_sendGridConfig.Value.FromEmail, _sendGridConfig.Value.FromName), ReplyTo = new EmailAddress(_sendGridConfig.Value.ReplyToEmail, _sendGridConfig.Value.ReplyToName), TemplateId = template.id, Asm = new ASM() { GroupId = unsubscribeGroup.id }, Personalizations = new List <Personalization>() { personalization }, CustomArgs = new Dictionary <string, string> { { "TemplateId", template.id }, { "RecipientUserID", emailBuildData.RecipientUserID.ToString() }, { "TemplateName", templateName }, { "GroupName", groupName }, { "MessageId", messageId }, { "JobId", emailBuildData.JobID.HasValue ? emailBuildData.JobID.ToString() : "null" }, { "GroupId", emailBuildData.GroupID.HasValue ? emailBuildData.GroupID.ToString() : "null" }, { "RequestId", emailBuildData.RequestID.HasValue ? emailBuildData.RequestID.ToString() : "null" }, { "ReferencedJobs", GetReferencedJobs(emailBuildData.ReferencedJobs) } } }; Response response = await _sendGridClient.SendEmailAsync(eml).ConfigureAwait(false); if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Accepted) { return(true); } else { return(false); } }
private bool UpdateUnsubscribeGroup(UnsubscribeGroup unsubscribeGroups) { string requestBody = JsonConvert.SerializeObject(unsubscribeGroups); Response response = _sendGridClient.RequestAsync(SendGridClient.Method.PATCH, requestBody, null, $"asm/groups/{unsubscribeGroups.id}").Result; if (response != null && response.StatusCode == HttpStatusCode.OK) { return(true); } else { throw new Exception("Unable to update unsubscribe group"); } }
public void SetUp() { _sendGridConfigSettings = new SendGridConfig() { ApiKey = "TestKey", FromName = "Test Name", FromEmail = "Test Email", BaseUrl = "Base Url" }; _sendGridConfig = new Mock <IOptions <SendGridConfig> >(); _sendGridConfig.SetupGet(x => x.Value).Returns(_sendGridConfigSettings); _sendGridClient = new Mock <ISendGridClient>(); _templateId = "testTemplateId"; Templates templates = new Templates(); Template template = new Template() { id = _templateId, name = "KnownTemplate" }; templates.templates = new Template[1] { template }; string responseBody = JsonConvert.SerializeObject(templates); _templatesResponse = Task.FromResult(new Response(System.Net.HttpStatusCode.OK, new StringContent(responseBody), null)); _sendGridClient.Setup(x => x.RequestAsync( It.IsAny <SendGridClient.Method>(), It.IsAny <string>(), It.IsAny <string>(), It.Is <String>(s => s.Contains("templates")), It.IsAny <CancellationToken>())) .Returns(() => _templatesResponse ); _memCacheTemplate = new Mock <IMemDistCache <Template> >(); _template = template; _memCacheTemplate.Setup(x => x.GetCachedDataAsync( It.IsAny <Func <CancellationToken, Task <Template> > >(), It.IsAny <string>(), It.IsAny <RefreshBehaviour>(), It.IsAny <CancellationToken>(), It.IsAny <NotInCacheBehaviour>())) .ReturnsAsync(() => _template); _memCacheUnsubscribeGroup = new Mock <IMemDistCache <UnsubscribeGroup> >(); _unsubscribeGroup = new UnsubscribeGroup() { id = -1 }; _memCacheUnsubscribeGroup.Setup(x => x.GetCachedDataAsync( It.IsAny <Func <CancellationToken, Task <UnsubscribeGroup> > >(), It.IsAny <string>(), It.IsAny <RefreshBehaviour>(), It.IsAny <CancellationToken>(), It.IsAny <NotInCacheBehaviour>())) .ReturnsAsync(() => _unsubscribeGroup); UnsubscribeGroup[] groups = new UnsubscribeGroup[1]; groups[0] = new UnsubscribeGroup() { id = 1, name = "KnownGroup", description = "KnownGroup description" }; string groupsResponseBody = JsonConvert.SerializeObject(groups); _groupsResponse = Task.FromResult(new Response(System.Net.HttpStatusCode.OK, new StringContent(groupsResponseBody), null)); _sendGridClient.Setup(x => x.RequestAsync( It.IsAny <SendGridClient.Method>(), It.IsAny <string>(), It.IsAny <string>(), It.Is <String>(s => s.Contains("groups")), It.IsAny <CancellationToken>())) .Returns(() => _groupsResponse ); _sendGridClient.Setup(x => x.SendEmailAsync( It.IsAny <SendGridMessage>(), It.IsAny <CancellationToken>() )).Returns(() => _sendEmailResponse); _classUnderTest = new ConnectSendGridService(_sendGridConfig.Object, _sendGridClient.Object, _memCacheTemplate.Object, _memCacheUnsubscribeGroup.Object); }