package com.diagbot.config;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.diagbot.exception.CommonException;
import com.diagbot.exception.ServiceErrorCode;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * @Description: 阿里大鱼配置项
 * @author: gaodm
 * @time: 2018/9/4 19:24
 */
@Configuration
@PropertySource("classpath:aliyun.properties")
public class AliyunConfigurer {
    @Bean
    @ConfigurationProperties(prefix = "aliyun")
    public AliyunConfig alidayuConfig() {
        return new AliyunConfig();
    }

    @Bean
    public IAcsClient iAcsClient(AliyunConfig aliyunConfig) throws ClientException {
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunConfig.getAccessKeyId(), aliyunConfig.getAccessKeySecret());
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", aliyunConfig.getProduct(), aliyunConfig.getDomain());
        } catch (ClientException e){
            e.printStackTrace();
            throw new CommonException(ServiceErrorCode.SMS_SEND_ERROR);
        }
        return new DefaultAcsClient(profile);
    }
}
