public SendGridBinding(ParameterInfo parameter, SendGridAttribute attribute, SendGridConfiguration config, INameResolver nameResolver, BindingProviderContext context) { _parameter = parameter; _attribute = attribute; _config = config; _nameResolver = nameResolver; _sendGrid = new Web(_config.ApiKey); if (!string.IsNullOrEmpty(_attribute.To)) { _toFieldBindingTemplate = CreateBindingTemplate(_attribute.To, context.BindingDataContract); } if (!string.IsNullOrEmpty(_attribute.From)) { _fromFieldBindingTemplate = CreateBindingTemplate(_attribute.From, context.BindingDataContract); } if (!string.IsNullOrEmpty(_attribute.Subject)) { _subjectFieldBindingTemplate = CreateBindingTemplate(_attribute.Subject, context.BindingDataContract); } if (!string.IsNullOrEmpty(_attribute.Text)) { _textFieldBindingTemplate = CreateBindingTemplate(_attribute.Text, context.BindingDataContract); } }
public Task <IBinding> TryCreateAsync(BindingProviderContext context) { if (context == null) { throw new ArgumentNullException("context"); } ParameterInfo parameter = context.Parameter; SendGridAttribute attribute = parameter.GetCustomAttribute <SendGridAttribute>(inherit: false); if (attribute == null) { return(Task.FromResult <IBinding>(null)); } if (context.Parameter.ParameterType != typeof(SendGridMessage) && context.Parameter.ParameterType != typeof(SendGridMessage).MakeByRefType()) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Can't bind SendGridAttribute to type '{0}'.", parameter.ParameterType)); } if (string.IsNullOrEmpty(_config.ApiKey)) { throw new InvalidOperationException( string.Format("The SendGrid ApiKey must be set either via a '{0}' app setting, via a '{0}' environment variable, or directly in code via SendGridConfiguration.ApiKey.", SendGridConfiguration.AzureWebJobsSendGridApiKeyName)); } return(Task.FromResult <IBinding>(new SendGridBinding(parameter, attribute, _config, _nameResolver, context))); }
private IAsyncCollector <Mail> CreateCollector(SendGridAttribute attr) { string apiKey = FirstOrDefault(attr.ApiKey, ApiKey); ISendGridClient sendGrid = _sendGridClientCache.GetOrAdd(apiKey, a => ClientFactory.Create(a)); return(new SendGridMailAsyncCollector(this, attr, sendGrid)); }
private IAsyncCollector <SendGridMessage> CreateCollector(SendGridAttribute attr) { string apiKey = FirstOrDefault(attr.ApiKey, _options.Value.ApiKey); ISendGridClient sendGrid = _sendGridClientCache.GetOrAdd(apiKey, a => ClientFactory.Create(a)); return(new SendGridMessageAsyncCollector(_options.Value, attr, sendGrid)); }
public void Binding_UsesNameResolver() { ParameterInfo parameter = GetType().GetMethod("TestMethod", BindingFlags.Static | BindingFlags.NonPublic).GetParameters().First(); SendGridAttribute attribute = new SendGridAttribute { To = "%param1%", From = "%param2%", Subject = "%param3%", Text = "%param4%" }; SendGridConfiguration config = new SendGridConfiguration { ApiKey = "12345" }; Dictionary<string, Type> contract = new Dictionary<string, Type>(); BindingProviderContext context = new BindingProviderContext(parameter, contract, CancellationToken.None); var nameResolver = new TestNameResolver(); nameResolver.Values.Add("param1", "*****@*****.**"); nameResolver.Values.Add("param2", "*****@*****.**"); nameResolver.Values.Add("param3", "Value3"); nameResolver.Values.Add("param4", "Value4"); SendGridBinding binding = new SendGridBinding(parameter, attribute, config, nameResolver, context); Dictionary<string, object> bindingData = new Dictionary<string, object>(); SendGridMessage message = binding.CreateDefaultMessage(bindingData); Assert.Equal("*****@*****.**", message.To.Single().Address); Assert.Equal("*****@*****.**", message.From.Address); Assert.Equal("Value3", message.Subject); Assert.Equal("Value4", message.Text); }
public void Binding_UsesNameResolver() { ParameterInfo parameter = GetType().GetMethod("TestMethod", BindingFlags.Static | BindingFlags.NonPublic).GetParameters().First(); SendGridAttribute attribute = new SendGridAttribute { To = "%param1%", From = "%param2%", Subject = "%param3%", Text = "%param4%" }; SendGridConfiguration config = new SendGridConfiguration { ApiKey = "12345" }; Dictionary <string, Type> contract = new Dictionary <string, Type>(); BindingProviderContext context = new BindingProviderContext(parameter, contract, CancellationToken.None); var nameResolver = new TestNameResolver(); nameResolver.Values.Add("param1", "*****@*****.**"); nameResolver.Values.Add("param2", "*****@*****.**"); nameResolver.Values.Add("param3", "Value3"); nameResolver.Values.Add("param4", "Value4"); SendGridBinding binding = new SendGridBinding(parameter, attribute, config, nameResolver, context); Dictionary <string, object> bindingData = new Dictionary <string, object>(); SendGridMessage message = binding.CreateDefaultMessage(bindingData); Assert.Equal("*****@*****.**", message.To.Single().Address); Assert.Equal("*****@*****.**", message.From.Address); Assert.Equal("Value3", message.Subject); Assert.Equal("Value4", message.Text); }
private void ValidateBinding(SendGridAttribute attribute, Type type) { string apiKey = FirstOrDefault(attribute.ApiKey, ApiKey, _defaultApiKey); if (string.IsNullOrEmpty(apiKey)) { throw new InvalidOperationException( $"The SendGrid ApiKey must be set either via an '{AzureWebJobsSendGridApiKeyName}' app setting, via an '{AzureWebJobsSendGridApiKeyName}' environment variable, or directly in code via {nameof(SendGridConfiguration)}.{nameof(SendGridConfiguration.ApiKey)} or {nameof(SendGridAttribute)}.{nameof(SendGridAttribute.ApiKey)}."); } }
public void CreateDefaultMessage_CreatesExpectedMessage() { ParameterInfo parameter = GetType().GetMethod("TestMethod", BindingFlags.Static | BindingFlags.NonPublic).GetParameters().First(); SendGridAttribute attribute = new SendGridAttribute { To = "{Param1}", Subject = "Test {Param2}", Text = "Test {Param3}" }; SendGridConfiguration config = new SendGridConfiguration { ApiKey = "12345", FromAddress = new MailAddress("*****@*****.**", "Test2"), ToAddress = "*****@*****.**" }; Dictionary <string, Type> contract = new Dictionary <string, Type>(); contract.Add("Param1", typeof(string)); contract.Add("Param2", typeof(string)); contract.Add("Param3", typeof(string)); BindingProviderContext context = new BindingProviderContext(parameter, contract, CancellationToken.None); var nameResolver = new TestNameResolver(); SendGridBinding binding = new SendGridBinding(parameter, attribute, config, nameResolver, context); Dictionary <string, object> bindingData = new Dictionary <string, object>(); bindingData.Add("Param1", "*****@*****.**"); bindingData.Add("Param2", "Value2"); bindingData.Add("Param3", "Value3"); SendGridMessage message = binding.CreateDefaultMessage(bindingData); Assert.Same(config.FromAddress, message.From); Assert.Equal("*****@*****.**", message.To.Single().Address); Assert.Equal("Test Value2", message.Subject); Assert.Equal("Test Value3", message.Text); // If no To value specified, verify it is defaulted from config attribute = new SendGridAttribute { Subject = "Test {Param2}", Text = "Test {Param3}" }; binding = new SendGridBinding(parameter, attribute, config, nameResolver, context); message = binding.CreateDefaultMessage(bindingData); Assert.Equal("*****@*****.**", message.To.Single().Address); }
public void CreateDefaultMessage_CreatesExpectedMessage() { ParameterInfo parameter = GetType().GetMethod("TestMethod", BindingFlags.Static | BindingFlags.NonPublic).GetParameters().First(); SendGridAttribute attribute = new SendGridAttribute { To = "{Param1}", Subject = "Test {Param2}", Text = "Test {Param3}" }; SendGridConfiguration config = new SendGridConfiguration { ApiKey = "12345", FromAddress = new MailAddress("*****@*****.**", "Test2"), ToAddress = "*****@*****.**" }; Dictionary<string, Type> contract = new Dictionary<string, Type>(); contract.Add("Param1", typeof(string)); contract.Add("Param2", typeof(string)); contract.Add("Param3", typeof(string)); BindingProviderContext context = new BindingProviderContext(parameter, contract, CancellationToken.None); var nameResolver = new TestNameResolver(); SendGridBinding binding = new SendGridBinding(parameter, attribute, config, nameResolver, context); Dictionary<string, object> bindingData = new Dictionary<string, object>(); bindingData.Add("Param1", "*****@*****.**"); bindingData.Add("Param2", "Value2"); bindingData.Add("Param3", "Value3"); SendGridMessage message = binding.CreateDefaultMessage(bindingData); Assert.Same(config.FromAddress, message.From); Assert.Equal("*****@*****.**", message.To.Single().Address); Assert.Equal("Test Value2", message.Subject); Assert.Equal("Test Value3", message.Text); // If no To value specified, verify it is defaulted from config attribute = new SendGridAttribute { Subject = "Test {Param2}", Text = "Test {Param3}" }; binding = new SendGridBinding(parameter, attribute, config, nameResolver, context); message = binding.CreateDefaultMessage(bindingData); Assert.Equal("*****@*****.**", message.To.Single().Address); }
public void DefaultMessageProperties_CreatesExpectedMessage() { SendGridAttribute attribute = new SendGridAttribute(); SendGridConfiguration config = new SendGridConfiguration { ApiKey = "12345", FromAddress = new EmailAddress("*****@*****.**", "Test2"), ToAddress = new EmailAddress("*****@*****.**", "Test") }; SendGridMessage message = new SendGridMessage(); message.Subject = "TestSubject"; message.AddContent("text/plain", "TestText"); SendGridHelpers.DefaultMessageProperties(message, config, attribute); Assert.Same(config.FromAddress, config.FromAddress); Assert.Equal("*****@*****.**", message.Personalizations.Single().Tos.Single().Email); Assert.Equal("TestSubject", message.Subject); Assert.Equal("TestText", message.Contents.Single().Value); }
public void DefaultMessageProperties_CreatesExpectedMessage() { SendGridAttribute attribute = new SendGridAttribute(); SendGridConfiguration config = new SendGridConfiguration { ApiKey = "12345", FromAddress = new MailAddress("*****@*****.**", "Test2"), ToAddress = new MailAddress("*****@*****.**", "Test") }; SendGridMessage message = new SendGridMessage { Subject = "TestSubject", Text = "TestText" }; SendGridHelpers.DefaultMessageProperties(message, config, attribute); Assert.Same(config.FromAddress, config.FromAddress); Assert.Equal("*****@*****.**", message.To.Single().Address); Assert.Equal("TestSubject", message.Subject); Assert.Equal("TestText", message.Text); }
private void Process <B>(string from, string[] tos, B body) { var attr = SendGridAttribute.Parse(body.GetType()); var nFrom = FromEmailAddress(from, _option.FromRandom(), attr.From); var nTemplateId = attr.TemplateId; var nTos = tos.Select(x => new EmailAddress(x)) .ToList(); _client.SendEmailAsync(new SendGridMessage() { From = nFrom, TemplateId = nTemplateId, Personalizations = new List <Personalization>() { new Personalization() { TemplateData = body, Tos = nTos } } }); }
public SendGridMessageAsyncCollector(SendGridOptions options, SendGridAttribute attribute, ISendGridClient sendGrid) { _options = options; _attribute = attribute; _sendGrid = sendGrid; }
public SendGridMailAsyncCollector(SendGridConfiguration config, SendGridAttribute attribute, ISendGridClient sendGrid) { _config = config; _attribute = attribute; _sendGrid = sendGrid; }
internal static void DefaultMessageProperties(SendGridMessage mail, SendGridConfiguration config, SendGridAttribute attribute) { // Apply message defaulting if (mail.From == null) { if (!string.IsNullOrEmpty(attribute.From)) { EmailAddress from = null; if (!TryParseAddress(attribute.From, out from)) { throw new ArgumentException("Invalid 'From' address specified"); } mail.From = from; } else if (config.FromAddress != null) { mail.From = config.FromAddress; } } if (!IsToValid(mail)) { if (!string.IsNullOrEmpty(attribute.To)) { EmailAddress to = null; if (!TryParseAddress(attribute.To, out to)) { throw new ArgumentException("Invalid 'To' address specified"); } mail.AddTo(to); } else if (config.ToAddress != null) { mail.AddTo(config.ToAddress); } } if (string.IsNullOrEmpty(mail.Subject) && !string.IsNullOrEmpty(attribute.Subject)) { mail.Subject = attribute.Subject; } if ((mail.Contents == null || mail.Contents.Count == 0) && !string.IsNullOrEmpty(attribute.Text)) { mail.AddContent("text/plain", attribute.Text); } }
private void ValidateBinding(SendGridAttribute attribute, Type type) { ValidateBinding(attribute); }
public SendGridMessageAsyncCollector(SendGridConfiguration config, SendGridAttribute attribute, Web sendGrid) { _config = config; _attribute = attribute; _sendGrid = sendGrid; }
internal static void DefaultMessageProperties(SendGridMessage message, SendGridConfiguration config, SendGridAttribute attribute) { // Apply message defaulting if (message.From == null) { if (!string.IsNullOrEmpty(attribute.From)) { MailAddress from = null; if (!TryParseAddress(attribute.From, out from)) { throw new ArgumentException("Invalid 'From' address specified"); } message.From = from; } else if (config.FromAddress != null) { message.From = config.FromAddress; } } if (message.To == null || message.To.Length == 0) { if (!string.IsNullOrEmpty(attribute.To)) { MailAddress to = null; if (!TryParseAddress(attribute.To, out to)) { throw new ArgumentException("Invalid 'To' address specified"); } message.To = new MailAddress[] { to }; } else if (config.ToAddress != null) { message.To = new MailAddress[] { config.ToAddress }; } } if (string.IsNullOrEmpty(message.Subject) && !string.IsNullOrEmpty(attribute.Subject)) { message.Subject = attribute.Subject; } if (string.IsNullOrEmpty(message.Text) && !string.IsNullOrEmpty(attribute.Text)) { message.Text = attribute.Text; } }