Ejemplo n.º 1
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);
            };
        }
        private HttpClient GetAuthorizedHttpClient()
        {
            var httpClient = _httpClientFactory();

            if (OAuthSpecified())
            {
                var accessToken = _accessTokenProvider.Get();
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer", accessToken);
            }
            else
            {
                SetUsernameAndPasswordAuthorization(httpClient);
            }
            return(httpClient);
        }