<commit_before>﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using KenticoCloud.Delivery.InlineContentItems;
using Microsoft.Extensions.Options;

namespace KenticoCloud.Delivery
{
    /// <summary>
    /// Executes requests against the Kentico Cloud Delivery API.
    /// </summary>
    public sealed class DeliveryClient : IDeliveryClient
    {
        private readonly DeliveryOptions _deliveryOptions;

        private HttpClient _httpClient;
        private DeliveryEndpointUrlBuilder _urlBuilder;

        private ICodeFirstModelProvider _codeFirstModelProvider;

        private IInlineContentItemsProcessor _inlineContentItemsProcessor;

        /// <summary>
        /// Gets or sets an object that resolves links to content items in Rich text element values.
        /// </summary>
        public IContentLinkUrlResolver ContentLinkUrlResolver { get; set; }

        /// <summary>
        /// Gets processor for richtext elements retrieved with this client.
        /// </summary>
        public IInlineContentItemsProcessor InlineContentItemsProcessor
        {
            get
            {
                if (_inlineContentItemsProcessor == null)
                {
                    var unretrievedInlineContentItemsResolver = new ReplaceWithWarningAboutUnretrievedItemResolver();
                    var defaultInlineContentItemsResolver = new ReplaceWithWarningAboutRegistrationResolver();
                    _inlineContentItemsProcessor = new InlineContentItemsProcessor(defaultInlineContentItemsResolver, unretrievedInlineContentItemsResolver);
                }
                return _inlineContentItemsProcessor;
            }
            private set
            {
                _inlineContentItemsProcessor = value;
            }
        }

        /// <summary>
        /// Gets or sets an object that performs conversion of content items to code-first models.
        /// </summary>
        public ICodeFirstModelProvider CodeFirstModelProvider
        {
            get { return _codeFirstModelProvider ?? (_codeFirstModelProvider = new CodeFirstModelProvider(this)); }
            set { _codeFirstModelProvider = value; }
        }

        private DeliveryEndpointUrlBuilder UrlBuilder
        {
            get { return _urlBuilder ?? (_urlBuilder = new DeliveryEndpointUrlBuilder(_deliveryOptions)); }
        }

        private HttpClient Client
        {
            get
            {
                if (_httpClient == null)
                {
                    _httpClient = new HttpClient();
                    if (!string.IsNullOrEmpty(_deliveryOptions.PreviewApiKey))
                    {
                        _httpClient.DefaultRequestHeaders.Add("Authorization", string.Format("Bearer {0}", _deliveryOptions.PreviewApiKey));
                    }
                }
                return _httpClient;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DeliveryClient"/> class for retrieving content of the specified project.
        /// </summary>
        /// <param name="deliveryOptions">The settings of the Kentico Cloud project.</param>
        public DeliveryClient(DeliveryOptions deliveryOptions)
        {
            _deliveryOptions = deliveryOptions ?? throw new ArgumentNullException(nameof(deliveryOptions), "The Delivery options object is not specified.");

            if (_deliveryOptions.ProjectId == null)
            {
                throw new ArgumentNullException(nameof(_deliveryOptions.ProjectId), "Kentico Cloud project identifier is not specified.");
            }

            if (_deliveryOptions.ProjectId == string.Empty)
            {
                throw new ArgumentException("Kentico Cloud project identifier is not specified.", nameof(_deliveryOptions.ProjectId));
            }

            Guid projectIdGuid;
            if (!Guid.TryParse(_deliveryOptions.ProjectId, out projectIdGuid))
            {
                throw new ArgumentException("Provided string is not a valid project identifier ({ProjectId}). Haven't you accidentally passed the Preview API key instead of the project identifier?", nameof(_deliveryOptions.ProjectId));
            }

            if (_deliveryOptions.UsePreviewApi)
            {
                if (_deliveryOptions.PreviewApiKey == null)
                {
                    throw new ArgumentNullException(nameof(_deliveryOptions.PreviewApiKey), "The Preview API key is not specified.");
                }

                if (_deliveryOptions.PreviewApiKey == string.Empty)
                {
                    throw new ArgumentException("The Preview API key is not specified.", nameof(_deliveryOptions.PreviewApiKey));
                }
            }

            _deliveryOptions.ProjectId = projectIdGuid.ToString("D");<commit_msg>fixed info message<commit_after>﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using KenticoCloud.Delivery.InlineContentItems;
using Microsoft.Extensions.Options;

namespace KenticoCloud.Delivery
{
    /// <summary>
    /// Executes requests against the Kentico Cloud Delivery API.
    /// </summary>
    public sealed class DeliveryClient : IDeliveryClient
    {
        private readonly DeliveryOptions _deliveryOptions;

        private HttpClient _httpClient;
        private DeliveryEndpointUrlBuilder _urlBuilder;

        private ICodeFirstModelProvider _codeFirstModelProvider;

        private IInlineContentItemsProcessor _inlineContentItemsProcessor;

        /// <summary>
        /// Gets or sets an object that resolves links to content items in Rich text element values.
        /// </summary>
        public IContentLinkUrlResolver ContentLinkUrlResolver { get; set; }

        /// <summary>
        /// Gets processor for richtext elements retrieved with this client.
        /// </summary>
        public IInlineContentItemsProcessor InlineContentItemsProcessor
        {
            get
            {
                if (_inlineContentItemsProcessor == null)
                {
                    var unretrievedInlineContentItemsResolver = new ReplaceWithWarningAboutUnretrievedItemResolver();
                    var defaultInlineContentItemsResolver = new ReplaceWithWarningAboutRegistrationResolver();
                    _inlineContentItemsProcessor = new InlineContentItemsProcessor(defaultInlineContentItemsResolver, unretrievedInlineContentItemsResolver);
                }
                return _inlineContentItemsProcessor;
            }
            private set
            {
                _inlineContentItemsProcessor = value;
            }
        }

        /// <summary>
        /// Gets or sets an object that performs conversion of content items to code-first models.
        /// </summary>
        public ICodeFirstModelProvider CodeFirstModelProvider
        {
            get { return _codeFirstModelProvider ?? (_codeFirstModelProvider = new CodeFirstModelProvider(this)); }
            set { _codeFirstModelProvider = value; }
        }

        private DeliveryEndpointUrlBuilder UrlBuilder
        {
            get { return _urlBuilder ?? (_urlBuilder = new DeliveryEndpointUrlBuilder(_deliveryOptions)); }
        }

        private HttpClient Client
        {
            get
            {
                if (_httpClient == null)
                {
                    _httpClient = new HttpClient();
                    if (!string.IsNullOrEmpty(_deliveryOptions.PreviewApiKey))
                    {
                        _httpClient.DefaultRequestHeaders.Add("Authorization", string.Format("Bearer {0}", _deliveryOptions.PreviewApiKey));
                    }
                }
                return _httpClient;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DeliveryClient"/> class for retrieving content of the specified project.
        /// </summary>
        /// <param name="deliveryOptions">The settings of the Kentico Cloud project.</param>
        public DeliveryClient(DeliveryOptions deliveryOptions)
        {
            _deliveryOptions = deliveryOptions ?? throw new ArgumentNullException(nameof(deliveryOptions), "The Delivery options object is not specified.");

            if (_deliveryOptions.ProjectId == null)
            {
                throw new ArgumentNullException(nameof(_deliveryOptions.ProjectId), "Kentico Cloud project identifier is not specified.");
            }

            if (_deliveryOptions.ProjectId == string.Empty)
            {
                throw new ArgumentException("Kentico Cloud project identifier is not specified.", nameof(_deliveryOptions.ProjectId));
            }

            if (!Guid.TryParse(_deliveryOptions.ProjectId, out Guid projectIdGuid))
            {
                throw new ArgumentException("Provided string is not a valid project identifier ({ProjectId}). Haven't you accidentally passed the Preview API key instead of the project identifier?", nameof(_deliveryOptions.ProjectId));
            }

            if (_deliveryOptions.UsePreviewApi)
            {
                if (_deliveryOptions.PreviewApiKey == null)
                {
                    throw new ArgumentNullException(nameof(_deliveryOptions.PreviewApiKey), "The Preview API key is not specified.");
                }

                if (_deliveryOptions.PreviewApiKey == string.Empty)
                {
                    throw new ArgumentException("The Preview API key is not specified.", nameof(_deliveryOptions.PreviewApiKey));
                }
            }

            _deliveryOptions.ProjectId = projectIdGuid.ToString("D");
{'fraction_non_alphanumeric': 0.07203994293865906, 'fraction_numerical': 0.0002037905033625433, 'mean_word_length': 2.098169191919192, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, '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': '1068261', 'n_tokens_mistral': 2239, 'n_tokens_neox': 2136, 'n_words': 582}