<commit_before>﻿using Renci.SshNet;
using Renci.SshNet.Common;
using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;

namespace Silverpop.Client
{
    internal class SilverpopCommunicationsClient : ISilverpopCommunicationsClient
    {
        private readonly TransactClientConfiguration _configuration;
        private readonly AccessTokenProvider _accessTokenProvider;
        private readonly string _transactHttpsUrl;
        private readonly Func<HttpClient> _httpClientFactory;
        private readonly Func<SftpClient> _sftpConnectedClientFactory;

        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;
                };
            }
        }

        public string HttpUpload(string data, bool tryRefreshingOAuthAccessToken = true)
        {
            var httpClient = GetAuthorizedHttpClient();

            if (OAuthSpecified())
            {
                try
                {
                    var response = httpClient.PostAsync(_transactHttpsUrl, new StringContent(data)).Result;
                    return response.Content.ReadAsStringAsync()
                        .ConfigureAwait(false).GetAwaiter().GetResult();
                }
                catch (WebException ex)
                {
                    var response = ex.Response as HttpWebResponse;
                    if (response != null && response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        _accessTokenProvider.Refresh();<commit_msg>Throw InvalidOperationException for missing SFTP Username/Password<commit_after>﻿using Renci.SshNet;
using Renci.SshNet.Common;
using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;

namespace Silverpop.Client
{
    internal class SilverpopCommunicationsClient : ISilverpopCommunicationsClient
    {
        private readonly TransactClientConfiguration _configuration;
        private readonly AccessTokenProvider _accessTokenProvider;
        private readonly string _transactHttpsUrl;
        private readonly Func<HttpClient> _httpClientFactory;
        private readonly Func<SftpClient> _sftpConnectedClientFactory;

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

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

            _httpClientFactory = () => new HttpClient();

            if (string.IsNullOrEmpty(_configuration.Username) ||
                string.IsNullOrEmpty(_configuration.Password))
            {
                _sftpConnectedClientFactory = () =>
                {
                    throw new InvalidOperationException(
                        $"{nameof(_configuration.Username)} and {nameof(_configuration.Password)} must be configured for SFTP usage.");
                };
            }
            else
            {
                _sftpConnectedClientFactory = () =>
                {
                    var sftpClient = new SftpClient(
                        $"transfer{configuration.PodNumber}.silverpop.com",
                        _configuration.Username,
                        _configuration.Password);

                    sftpClient.Connect();

                    return sftpClient;
                };
            }
        }

        public string HttpUpload(string data, bool tryRefreshingOAuthAccessToken = true)
        {
            var httpClient = GetAuthorizedHttpClient();

            if (OAuthSpecified())
            {
                try
                {
                    var response = httpClient.PostAsync(_transactHttpsUrl, new StringContent(data)).Result;
                    return response.Content.ReadAsStringAsync()
                        .ConfigureAwait(false).GetAwaiter().GetResult();
                }
                catch (WebException ex)
                {
                    var response = ex.Response as HttpWebResponse;
                    if (response != null && response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        _accessTokenProvider.Refresh();
{'fraction_non_alphanumeric': 0.07194635737586082, 'fraction_numerical': 0.0005436752446538601, 'mean_word_length': 1.8463125322331098, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4328416', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1237, 'n_words': 249}