<commit_before>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Services.Client;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Runtime.Versioning;
using System.Windows;
using NuGet.Resources;

namespace NuGet
{
    public class DataServicePackageRepository : 
        PackageRepositoryBase, 
        IHttpClientEvents, 
        IServiceBasedRepository, 
        ICloneableRepository, 
        ICultureAwareRepository, 
        IOperationAwareRepository,
        IPackageLookup,
        ILatestPackageLookup,
        IWeakEventListener
    {
        private const string FindPackagesByIdSvcMethod = "FindPackagesById";
        private const string PackageServiceEntitySetName = "Packages";
        private const string SearchSvcMethod = "Search";
        private const string GetUpdatesSvcMethod = "GetUpdates";

        private IDataServiceContext _context;
        private readonly IHttpClient _httpClient;
        private readonly IPackageDownloader _packageDownloader;
        private CultureInfo _culture;
        private Tuple<string, string, string> _currentOperation;
        private event EventHandler<WebRequestEventArgs> _sendingRequest;

        public DataServicePackageRepository(Uri serviceRoot)
            : this(new HttpClient(serviceRoot))
        {
        }

        public DataServicePackageRepository(IHttpClient client)
            : this(client, new PackageDownloader())
        {
        }

        public DataServicePackageRepository(IHttpClient client, IPackageDownloader packageDownloader)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }
            if (packageDownloader == null)
            {
                throw new ArgumentNullException("packageDownloader");
            }

            _httpClient = client;
            _httpClient.AcceptCompression = true;
            
            _packageDownloader = packageDownloader;

            if (EnvironmentUtility.RunningFromCommandLine || EnvironmentUtility.IsMonoRuntime)
            {
                _packageDownloader.SendingRequest += OnPackageDownloaderSendingRequest;
            }
            else
            {
                // weak event pattern            
                SendingRequestEventManager.AddListener(_packageDownloader, this);
            }
        }

        private void OnPackageDownloaderSendingRequest(object sender, WebRequestEventArgs e)
        {
            if (_currentOperation != null)
            {
                string operation = _currentOperation.Item1;
                string mainPackageId = _currentOperation.Item2;
                string mainPackageVersion = _currentOperation.Item3;

                if (!String.IsNullOrEmpty(mainPackageId) && !String.IsNullOrEmpty(_packageDownloader.CurrentDownloadPackageId))
                {
                    if (!mainPackageId.Equals(_packageDownloader.CurrentDownloadPackageId, StringComparison.OrdinalIgnoreCase))
                    {
                        operation = operation + "-Dependency";
                    }
                }

                e.Request.Headers[RepositoryOperationNames.OperationHeaderName] = operation;

                if (!operation.Equals(_currentOperation.Item1, StringComparison.OrdinalIgnoreCase))
                {
                    e.Request.Headers[RepositoryOperationNames.DependentPackageHeaderName] = mainPackageId;
                    if (!String.IsNullOrEmpty(mainPackageVersion))
                    {
                        e.Request.Headers[RepositoryOperationNames.DependentPackageVersionHeaderName] = mainPackageVersion;
                    }
                }

                RaiseSendingRequest(e);
            }
        }

        // Just forward calls to the package downloader
        public event EventHandler<ProgressEventArgs> ProgressAvailable
        {
            add
            {
                _packageDownloader.ProgressAvailable += value;
            }
            remove
            {
                _packageDownloader.ProgressAvailable -= value;
            }
        }

        public event EventHandler<WebRequestEventArgs> SendingRequest
        {
            add
            {
                _packageDownloader.SendingRequest += value;
                _httpClient.SendingRequest += value;
                _sendingRequest += value;
            }
            remove
            {
                _packageDownloader.SendingRequest -= value;
                _httpClient.SendingRequest -= value;
                _sendingRequest -= value;
            }
        }

        public CultureInfo Culture
        {
            get
            {
                if (_culture == null)
                {
                    // TODO: Technically, if this is a remote server, we have to return the culture of the server
                    // instead of invariant culture. However, there is no trivial way to retrieve the server's culture,
                    // So temporarily use Invariant culture here. 
                    _culture = _httpClient.Uri.IsLoopback ? CultureInfo.CurrentCulture : CultureInfo.InvariantCulture;
                }
                return _culture;
            }
        }

        // Do NOT delete this property. It is used by the functional test.
        public IPackageDownloader PackageDownloader
        {
            get { return _packageDownloader; }
        }

        public override string Source
        {
            get
            {
                return _httpClient.Uri.OriginalString;
            }
        }<commit_msg>(GH-14) Do not fail grabbing source string<commit_after>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Services.Client;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Runtime.Versioning;
using System.Windows;
using NuGet.Resources;

namespace NuGet
{
    public class DataServicePackageRepository : 
        PackageRepositoryBase, 
        IHttpClientEvents, 
        IServiceBasedRepository, 
        ICloneableRepository, 
        ICultureAwareRepository, 
        IOperationAwareRepository,
        IPackageLookup,
        ILatestPackageLookup,
        IWeakEventListener
    {
        private const string FindPackagesByIdSvcMethod = "FindPackagesById";
        private const string PackageServiceEntitySetName = "Packages";
        private const string SearchSvcMethod = "Search";
        private const string GetUpdatesSvcMethod = "GetUpdates";

        private IDataServiceContext _context;
        private readonly IHttpClient _httpClient;
        private readonly IPackageDownloader _packageDownloader;
        private CultureInfo _culture;
        private Tuple<string, string, string> _currentOperation;
        private event EventHandler<WebRequestEventArgs> _sendingRequest;

        public DataServicePackageRepository(Uri serviceRoot)
            : this(new HttpClient(serviceRoot))
        {
        }

        public DataServicePackageRepository(IHttpClient client)
            : this(client, new PackageDownloader())
        {
        }

        public DataServicePackageRepository(IHttpClient client, IPackageDownloader packageDownloader)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }
            if (packageDownloader == null)
            {
                throw new ArgumentNullException("packageDownloader");
            }

            _httpClient = client;
            _httpClient.AcceptCompression = true;
            
            _packageDownloader = packageDownloader;

            if (EnvironmentUtility.RunningFromCommandLine || EnvironmentUtility.IsMonoRuntime)
            {
                _packageDownloader.SendingRequest += OnPackageDownloaderSendingRequest;
            }
            else
            {
                // weak event pattern            
                SendingRequestEventManager.AddListener(_packageDownloader, this);
            }
        }

        private void OnPackageDownloaderSendingRequest(object sender, WebRequestEventArgs e)
        {
            if (_currentOperation != null)
            {
                string operation = _currentOperation.Item1;
                string mainPackageId = _currentOperation.Item2;
                string mainPackageVersion = _currentOperation.Item3;

                if (!String.IsNullOrEmpty(mainPackageId) && !String.IsNullOrEmpty(_packageDownloader.CurrentDownloadPackageId))
                {
                    if (!mainPackageId.Equals(_packageDownloader.CurrentDownloadPackageId, StringComparison.OrdinalIgnoreCase))
                    {
                        operation = operation + "-Dependency";
                    }
                }

                e.Request.Headers[RepositoryOperationNames.OperationHeaderName] = operation;

                if (!operation.Equals(_currentOperation.Item1, StringComparison.OrdinalIgnoreCase))
                {
                    e.Request.Headers[RepositoryOperationNames.DependentPackageHeaderName] = mainPackageId;
                    if (!String.IsNullOrEmpty(mainPackageVersion))
                    {
                        e.Request.Headers[RepositoryOperationNames.DependentPackageVersionHeaderName] = mainPackageVersion;
                    }
                }

                RaiseSendingRequest(e);
            }
        }

        // Just forward calls to the package downloader
        public event EventHandler<ProgressEventArgs> ProgressAvailable
        {
            add
            {
                _packageDownloader.ProgressAvailable += value;
            }
            remove
            {
                _packageDownloader.ProgressAvailable -= value;
            }
        }

        public event EventHandler<WebRequestEventArgs> SendingRequest
        {
            add
            {
                _packageDownloader.SendingRequest += value;
                _httpClient.SendingRequest += value;
                _sendingRequest += value;
            }
            remove
            {
                _packageDownloader.SendingRequest -= value;
                _httpClient.SendingRequest -= value;
                _sendingRequest -= value;
            }
        }

        public CultureInfo Culture
        {
            get
            {
                if (_culture == null)
                {
                    // TODO: Technically, if this is a remote server, we have to return the culture of the server
                    // instead of invariant culture. However, there is no trivial way to retrieve the server's culture,
                    // So temporarily use Invariant culture here. 
                    _culture = _httpClient.Uri.IsLoopback ? CultureInfo.CurrentCulture : CultureInfo.InvariantCulture;
                }
                return _culture;
            }
        }

        // Do NOT delete this property. It is used by the functional test.
        public IPackageDownloader PackageDownloader
        {
            get { return _packageDownloader; }
        }

        public override string Source
        {
            get
            {
                return _httpClient.OriginalUri.OriginalString;
            }
        }
{'fraction_non_alphanumeric': 0.05863846893297212, 'fraction_numerical': 0.0008738967054094206, 'mean_word_length': 1.7391096218286262, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 50, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3829545', 'n_tokens_mistral': 2594, 'n_tokens_neox': 2469, 'n_words': 554}