public SilverpopCommunicationsClient(TransactClientConfiguration configuration)
        {
            _configuration       = configuration;
            _accessTokenProvider = new AccessTokenProvider(configuration);

            _transactHttpsUrl = string.Format(
                "https://transact{0}.silverpop.com/XTMail",
                configuration.PodNumber);

            _httpClientFactory = () => new HttpClient();

            var transactSftpHost = string.Format(
                "transfer{0}.silverpop.com",
                configuration.PodNumber);

            if (_configuration.Username != null &&
                _configuration.Password != null)
            {
                _sftpConnectedClientFactory = () =>
                {
                    var sftpClient = new SftpClient(
                        transactSftpHost,
                        _configuration.Username,
                        _configuration.Password);

                    sftpClient.Connect();

                    return(sftpClient);
                };
            }
        }
示例#2
0
 public TransactClient(
     TransactClientConfiguration configuration,
     TransactMessageEncoder encoder,
     TransactMessageResponseDecoder decoder,
     AddRecipientEncoder addRecipientEncoder,
     AddRecipientResponseDecoder addRecipientDecoder,
     SelectRecipientDataEncoder selectRecipientEncoder,
     SelectRecipientDataResponseDecoder selectRecipientDecoder,
     LoginEncoder loginEncoder,
     LoginResponseDecoder loginDecoder,
     SendMailingEncoder sendMailingEncoder,
     SendMailingResponseDecoder sendMailingDecoder,
     Func <ISilverpopCommunicationsClient> silverpopFactory)
 {
     Configuration           = configuration;
     _encoder                = encoder;
     _decoder                = decoder;
     _addRecipientEncoder    = addRecipientEncoder;
     _addRecipientDecoder    = addRecipientDecoder;
     _selectRecipientEncoder = selectRecipientEncoder;
     _selectRecipientDecoder = selectRecipientDecoder;
     _loginEncoder           = loginEncoder;
     _loginDecoder           = loginDecoder;
     _sendMailingEncoder     = sendMailingEncoder;
     _sendMailingDecoder     = sendMailingDecoder;
     _silverpopFactory       = silverpopFactory;
 }
        public virtual TransactClientConfiguration GetFromAppSettings()
        {
            if (!ConfigurationManager.AppSettings.AllKeys.Any(
                    x => x.StartsWith(_appSettingsPrefix, StringComparison.Ordinal)))
            {
                return null;
            }

            int podNumber;
            var parseablePodNumberSet = int.TryParse(
                GetAppSettingValueOrNull("PodNumber"),
                out podNumber);

            var config = new TransactClientConfiguration
            {
                PodNumber = parseablePodNumberSet ? podNumber : (int?)null,
                Username = GetAppSettingValueOrNull("Username"),
                Password = GetAppSettingValueOrNull("Password"),
                OAuthClientId = GetAppSettingValueOrNull("OAuthClientId"),
                OAuthClientSecret = GetAppSettingValueOrNull("OAuthClientSecret"),
                OAuthRefreshToken = GetAppSettingValueOrNull("OAuthRefreshToken")
            };

            return config;
        }
示例#4
0
        public virtual TransactClientConfiguration GetFromAppSettings()
        {
            if (!ConfigurationManager.AppSettings.AllKeys.Any(
                    x => x.StartsWith(_appSettingsPrefix, StringComparison.Ordinal)))
            {
                return(null);
            }

            int podNumber;
            var parseablePodNumberSet = int.TryParse(
                GetAppSettingValueOrNull("PodNumber"),
                out podNumber);

            var config = new TransactClientConfiguration
            {
                PodNumber         = parseablePodNumberSet ? podNumber : (int?)null,
                Username          = GetAppSettingValueOrNull("Username"),
                Password          = GetAppSettingValueOrNull("Password"),
                OAuthClientId     = GetAppSettingValueOrNull("OAuthClientId"),
                OAuthClientSecret = GetAppSettingValueOrNull("OAuthClientSecret"),
                OAuthRefreshToken = GetAppSettingValueOrNull("OAuthRefreshToken")
            };

            return(config);
        }
        /// <summary>
        /// Create the TransactClient using application configuration.
        /// This will attempt to read both the TransactClientConfigurationSection
        /// and appSettings from configuration. When a setting is set in both
        /// TransactClientConfigurationSection and appSettings the appSettings value is used.
        /// </summary>
        /// <param name="configProvider">
        /// Optional override for specifying a custom TransactClientConfigurationProvider.
        /// </param>
        public static TransactClient CreateUsingConfiguration(
            TransactClientConfigurationProvider configProvider = null)
        {
            var config = new TransactClientConfiguration();

            if (configProvider == null)
            {
                configProvider = new TransactClientConfigurationProvider();
            }

            var configSectionConfig = configProvider.GetFromConfigurationSection();
            var appSettingsConfig   = configProvider.GetFromAppSettings();

            if (configSectionConfig == null && appSettingsConfig == null)
            {
                throw new InvalidOperationException(
                          "Unable to create TransactClient using configuration. " +
                          "No configuration was provided.");
            }

            if (configSectionConfig != null)
            {
                config.HydrateUsing(configSectionConfig);
            }

            if (appSettingsConfig != null)
            {
                config.HydrateUsing(appSettingsConfig);
            }

            return(new TransactClient(config));
        }
 public TransactClient(TransactClientConfiguration configuration)
     : this(
         configuration,
         new TransactMessageEncoder(),
         new TransactMessageResponseDecoder(),
         () => new SilverpopCommunicationsClient(configuration))
 {
 }
 public TransactClient(TransactClientConfiguration configuration)
     : this(
         configuration,
         new TransactMessageEncoder(),
         new TransactMessageResponseDecoder(),
         () => new SilverpopCommunicationsClient(configuration))
 {
 }
 public AccessTokenProvider(TransactClientConfiguration configuration)
 {
     _configuration = configuration;
     _tokenUrl = string.Format(
         "https://api{0}.silverpop.com/oauth/token",
         configuration.PodNumber);
     _httpClient = new HttpClient();
 }
示例#9
0
 /// <param name="httpClient">
 /// Optional: If null a default implementation will be used.
 /// </param>
 public AccessTokenProvider(TransactClientConfiguration configuration, HttpClient httpClient = null)
 {
     _configuration = configuration;
     _tokenUrl      = string.Format(
         "https://api-campaign-{0}.goacoustic.com/oauth/token",
         configuration.EndpointIdentifier);
     _httpClient = httpClient ?? new HttpClient();
 }
示例#10
0
 /// <param name="httpClient">
 /// Optional: If null a default implementation will be used.
 /// </param>
 public AccessTokenProvider(TransactClientConfiguration configuration, HttpClient httpClient = null)
 {
     _configuration = configuration;
     _tokenUrl      = string.Format(
         "https://api{0}.silverpop.com/oauth/token",
         configuration.PodNumber);
     _httpClient = httpClient ?? new HttpClient();
 }
 public TransactClient(
     TransactClientConfiguration configuration,
     TransactMessageEncoder encoder,
     TransactMessageResponseDecoder decoder,
     Func<ISilverpopCommunicationsClient> silverpopFactory)
 {
     Configuration = configuration;
     _encoder = encoder;
     _decoder = decoder;
     _silverpopFactory = silverpopFactory;
 }
 public TransactClient(
     TransactClientConfiguration configuration,
     TransactMessageEncoder encoder,
     TransactMessageResponseDecoder decoder,
     Func <ISilverpopCommunicationsClient> silverpopFactory)
 {
     Configuration     = configuration;
     _encoder          = encoder;
     _decoder          = decoder;
     _silverpopFactory = silverpopFactory;
 }
示例#13
0
        public SilverpopCommunicationsClient(TransactClientConfiguration configuration)
        {
            _configuration       = configuration;
            _accessTokenProvider = new AccessTokenProvider(configuration);

            _transactHttpsUrl = string.Format(
                "https://transact{0}.ibmmarketingcloud.com/XTMail",
                configuration.PodNumber);

            _XMLAPIHttpsUrl = string.Format(
                "https://api{0}.ibmmarketingcloud.com/XMLAPI",
                configuration.PodNumber);

            _httpClientFactory = () =>
            {
                var client = new HttpClient();
                client.Timeout = new TimeSpan(0, 1, 0);
                return(client);
            };

            _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{configuration.PodNumber}.ibmmarketingcloud.com",
                    username,
                    password);

                sftpClient.Connect();

                return(sftpClient);
            };
        }
示例#14
0
 public TransactClient(TransactClientConfiguration configuration)
     : this(
         configuration,
         new TransactMessageEncoder(),
         new TransactMessageResponseDecoder(),
         new AddRecipientEncoder(),
         new AddRecipientResponseDecoder(),
         new SelectRecipientDataEncoder(),
         new SelectRecipientDataResponseDecoder(),
         new LoginEncoder(),
         new LoginResponseDecoder(),
         new SendMailingEncoder(),
         new SendMailingResponseDecoder(),
         () => new SilverpopCommunicationsClient(configuration))
 {
 }
示例#15
0
        public virtual TransactClientConfiguration GetFromConfigurationSection()
        {
            if (!(ConfigurationManager.GetSection("transactClientConfiguration") is TransactClientConfigurationSection configSection))
            {
                return(null);
            }

            var config = new TransactClientConfiguration
            {
                PodNumber         = configSection.PodNumber,
                Username          = GetValueOrNull(configSection.Username),
                Password          = GetValueOrNull(configSection.Password),
                OAuthClientId     = GetValueOrNull(configSection.OAuthClientId),
                OAuthClientSecret = GetValueOrNull(configSection.OAuthClientSecret),
                OAuthRefreshToken = GetValueOrNull(configSection.OAuthRefreshToken)
            };

            return(config);
        }
        public virtual TransactClientConfiguration GetFromConfigurationSection()
        {
            var configSection = ConfigurationManager.GetSection("transactClientConfiguration")
                as TransactClientConfigurationSection;

            if (configSection == null)
            {
                return null;
            }

            var config = new TransactClientConfiguration
            {
                PodNumber = configSection.PodNumber,
                Username = GetValueOrNull(configSection.Username),
                Password = GetValueOrNull(configSection.Password),
                OAuthClientId = GetValueOrNull(configSection.OAuthClientId),
                OAuthClientSecret = GetValueOrNull(configSection.OAuthClientSecret),
                OAuthRefreshToken = GetValueOrNull(configSection.OAuthRefreshToken)
            };

            return config;
        }
        public void HydrateUsing(TransactClientConfiguration config)
        {
            if (config == null)
                throw new ArgumentNullException("config");

            if (config.PodNumber.HasValue)
                PodNumber = config.PodNumber;

            if (!string.IsNullOrWhiteSpace(config.Username))
                Username = config.Username;

            if (!string.IsNullOrWhiteSpace(config.Password))
                Password = config.Password;

            if (!string.IsNullOrWhiteSpace(config.OAuthClientId))
                OAuthClientId = config.OAuthClientId;

            if (!string.IsNullOrWhiteSpace(config.OAuthClientSecret))
                OAuthClientSecret = config.OAuthClientSecret;

            if (!string.IsNullOrWhiteSpace(config.OAuthRefreshToken))
                OAuthRefreshToken = config.OAuthRefreshToken;
        }
示例#18
0
        public void HydrateUsing(TransactClientConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (config.PodNumber.HasValue)
            {
                PodNumber = config.PodNumber;
            }

            if (!string.IsNullOrWhiteSpace(config.Username))
            {
                Username = config.Username;
            }

            if (!string.IsNullOrWhiteSpace(config.Password))
            {
                Password = config.Password;
            }

            if (!string.IsNullOrWhiteSpace(config.OAuthClientId))
            {
                OAuthClientId = config.OAuthClientId;
            }

            if (!string.IsNullOrWhiteSpace(config.OAuthClientSecret))
            {
                OAuthClientSecret = config.OAuthClientSecret;
            }

            if (!string.IsNullOrWhiteSpace(config.OAuthRefreshToken))
            {
                OAuthRefreshToken = config.OAuthRefreshToken;
            }
        }
        /// <summary>
        /// Create the TransactClient using application configuration.
        /// This will attempt to read both the TransactClientConfigurationSection
        /// and appSettings from configuration. When a setting is set in both
        /// TransactClientConfigurationSection and appSettings the appSettings value is used.
        /// </summary>
        /// <param name="configProvider">
        /// Optional override for specifying a custom TransactClientConfigurationProvider.
        /// </param>
        public static TransactClient CreateUsingConfiguration(
            TransactClientConfigurationProvider configProvider = null)
        {
            var config = new TransactClientConfiguration();

            if (configProvider == null)
            {
                configProvider = new TransactClientConfigurationProvider();
            }

            var configSectionConfig = configProvider.GetFromConfigurationSection();
            var appSettingsConfig = configProvider.GetFromAppSettings();

            if (configSectionConfig == null && appSettingsConfig == null)
            {
                throw new InvalidOperationException(
                    "Unable to create TransactClient using configuration. " +
                    "No configuration was provided.");
            }

            if (configSectionConfig != null)
            {
                config.HydrateUsing(configSectionConfig);
            }

            if (appSettingsConfig != null)
            {
                config.HydrateUsing(appSettingsConfig);
            }

            return new TransactClient(config);
        }