Exemple #1
0
        public SilverpopCommunicationsClient(TransactClientConfiguration configuration)
        {
            _configuration       = configuration;
            _accessTokenProvider = new AccessTokenProvider(configuration);

            _transactHttpsUrl = string.Format(
                "transact-campaign-{0}.goacoustic.com/XTMail",
                configuration.EndpointIdentifier);

            _httpClientFactory = () => new HttpClient();

            _sftpConnectedClientFactory = () =>
            {
                var username = _configuration.Username;
                var password = _configuration.Password;

                // Prefer Username and Password as OAuth may not be enabled for SFTP usage.
                // See "Note" at
                // https://www.ibm.com/support/knowledgecenter/en/SSWU4L/Data/imc_Data/Data_q_a_watson_assistant/Authenticate_to_FTP_with_OAuth.html
                // for more details.
                if ((string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) && OAuthSpecified())
                {
                    username = "******";
                    password = _accessTokenProvider.Get();
                }

                if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
                {
                    throw new InvalidOperationException(
                              $"OAuth login details or {nameof(_configuration.Username)} and {nameof(_configuration.Password)} "
                              + "must be configured for SFTP usage.");
                }

                var sftpClient = new SftpClient(
                    $"transfer-campaign-{configuration.EndpointIdentifier}.goacoustic.com",
                    username,
                    password);

                sftpClient.Connect();

                return(sftpClient);
            };
        }