001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.sendgrid;
018
019import com.sendgrid.Client;
020import com.sendgrid.SendGrid;
021import org.apache.http.HttpHost;
022import org.apache.http.impl.client.HttpClientBuilder;
023
024import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
027import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
028import org.springframework.boot.context.properties.EnableConfigurationProperties;
029import org.springframework.context.annotation.Bean;
030import org.springframework.context.annotation.Configuration;
031
032/**
033 * {@link EnableAutoConfiguration Auto-configuration} for SendGrid.
034 *
035 * @author Maciej Walkowiak
036 * @author Patrick Bray
037 * @author Andy Wilkinson
038 * @since 1.3.0
039 */
040@Configuration
041@ConditionalOnClass(SendGrid.class)
042@ConditionalOnProperty(prefix = "spring.sendgrid", value = "api-key")
043@EnableConfigurationProperties(SendGridProperties.class)
044public class SendGridAutoConfiguration {
045
046        private final SendGridProperties properties;
047
048        public SendGridAutoConfiguration(SendGridProperties properties) {
049                this.properties = properties;
050        }
051
052        @Bean
053        @ConditionalOnMissingBean
054        public SendGrid sendGrid() {
055                if (this.properties.isProxyConfigured()) {
056                        HttpHost proxy = new HttpHost(this.properties.getProxy().getHost(),
057                                        this.properties.getProxy().getPort());
058                        return new SendGrid(this.properties.getApiKey(),
059                                        new Client(HttpClientBuilder.create().setProxy(proxy).build()));
060                }
061                return new SendGrid(this.properties.getApiKey());
062        }
063
064}