<commit_before>﻿// RestClient.cs
// DynamicRest provides REST service access using C# 4.0 dynamic programming.
// The latest information and code for the project can be found at
// https://github.com/NikhilK/dynamicrest
//
// This project is licensed under the BSD license. See the License.txt file for
// more information.
//

using System;
using System.Dynamic;
using System.Net;
using DynamicRest.HTTPInterfaces;

namespace DynamicRest {

    public sealed class RestClient : DynamicObject {
        //private static readonly Regex StripXmlnsRegex =
        //    new Regex(@"(xmlns:?[^=]*=[""][^""]*[""])",
        //              RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.CultureInvariant);

        private readonly string _operationGroup;
        private WebHeaderCollection _responseHeaders = new WebHeaderCollection();
        private readonly IBuildRequests _requestBuilder;

        private IProcessResponses _responseProcessor;

        public RestClient(IBuildRequests requestBuilder, IProcessResponses responseProcessor)
        {
            _responseProcessor = responseProcessor;
            _requestBuilder = requestBuilder;
        }

        private RestClient(RestClient restClient, string operationGroup)
            : this(restClient._requestBuilder, restClient._responseProcessor)
        {
            _operationGroup = operationGroup;
        }

        private RestOperation PerformOperation(string operationName, IProcessResponses responseProcessor, params object[] args) {
            JsonObject argsObject = null;
            if ((args != null) && (args.Length != 0)) {
                argsObject = (JsonObject)args[0];
            }

            var operation = new RestOperation();

            IHttpRequest webRequest = _requestBuilder.CreateRequest(operationName, argsObject);

            try {
                var webResponse = webRequest.GetResponse();
                responseProcessor.Process(webResponse, operation);
            }
            catch (WebException webException) {
                var response = (HttpWebResponse)webException.Response;
                operation.Complete(webException, response.StatusCode, response.StatusDescription);
            }

            return operation;
        }

        private RestOperation PerformOperationAsync(string operationName, IProcessResponses responseProcessor, params object[] args) {
            JsonObject argsObject = null;
            if ((args != null) && (args.Length != 0)) {
                argsObject = (JsonObject)args[0];
            }

            var operation = new RestOperation();

            IHttpRequest webRequest = _requestBuilder.CreateRequest(operationName, argsObject);

            webRequest.BeginGetResponse(ar => {
                try
                {
                    var webResponse = webRequest.EndGetResponse(ar);
                    responseProcessor.Process(webResponse, operation);
                }
                catch (WebException webException) {
                    var response = (HttpWebResponse)webException.Response;
                    operation.Complete(webException, response.StatusCode, response.StatusDescription);
                }
            }, null);

            return operation;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result) {
            object value = _requestBuilder.ParametersStore.GetParameter(binder.Name);
            if (value != null)
            {
                result = value;
                return true;
            }

            string operationGroup = binder.Name;
            if (_operationGroup != null) {
                operationGroup = _operationGroup + "." + operationGroup;
            }

            var operationGroupClient = new RestClient(this, operationGroup);

            result = operationGroupClient;
            return true;
        }

        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) {
            bool async = false;

            string operation = binder.Name;
            if (operation.EndsWith("Async", StringComparison.Ordinal)) {
                async = true;
                operation = operation.Substring(0, operation.Length - 5);
            }

            if (_operationGroup != null) {
                operation = _operationGroup + "." + operation;
            }

            if (async == false) {
                result = PerformOperation(operation, _responseProcessor, args);
            }
            else {
                result = PerformOperationAsync(operation, _responseProcessor, args);
            }
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value) {
            _requestBuilder.ParametersStore.SetParameter(binder.Name, value);
            return true;
        }

        public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) {
            if(indexes.Length == 1 && indexes[0].GetType() == typeof(HttpResponseHeader))
            {
                result = _responseHeaders[(HttpResponseHeader)indexes[0]];
                return true;
            }
            return base.TryGetIndex(binder, indexes, out result);
        }

        public RestClient WithCredentials(ICredentials credentials) {
            if (credentials == null) {
                throw new ArgumentNullException("credentials");
            }

            _requestBuilder.Credentials = credentials;
            return this;
        }

        public RestClient WithHeader(HttpRequestHeader headerType, string value) {
            _requestBuilder.AddHeader(headerType, value);
            return this;
        }

        public RestClient WithAuthorization(OAuth oAuth) {
            WithHeader(HttpRequestHeader.Authorization, oAuth.Token);
            return this;
        }

        public RestClient WithXmlBody(string xml) {
            _requestBuilder.Body = xml;
            return this;
        }

        public RestClient WithContentType(string contentType) {
            _requestBuilder.ContentType = contentType;
             return this;
        }

        public string this[HttpResponseHeader index] {
            get { return _responseHeaders[index]; }
        }
    }
}
<commit_msg>removed unused fluent interfaces on rest client<commit_after>﻿// RestClient.cs
// DynamicRest provides REST service access using C# 4.0 dynamic programming.
// The latest information and code for the project can be found at
// https://github.com/NikhilK/dynamicrest
//
// This project is licensed under the BSD license. See the License.txt file for
// more information.
//

using System;
using System.Dynamic;
using System.Net;
using DynamicRest.HTTPInterfaces;

namespace DynamicRest {

    public sealed class RestClient : DynamicObject {
        
        private readonly string _operationGroup;
        private WebHeaderCollection _responseHeaders = new WebHeaderCollection();
        private readonly IBuildRequests _requestBuilder;
        private IProcessResponses _responseProcessor;

        public RestClient(IBuildRequests requestBuilder, IProcessResponses responseProcessor)
        {
            _responseProcessor = responseProcessor;
            _requestBuilder = requestBuilder;
        }

        private RestClient(RestClient restClient, string operationGroup)
            : this(restClient._requestBuilder, restClient._responseProcessor)
        {
            _operationGroup = operationGroup;
        }

        private RestOperation PerformOperation(string operationName, IProcessResponses responseProcessor, params object[] args) {
            JsonObject argsObject = null;
            if ((args != null) && (args.Length != 0)) {
                argsObject = (JsonObject)args[0];
            }

            var operation = new RestOperation();

            IHttpRequest webRequest = _requestBuilder.CreateRequest(operationName, argsObject);

            try {
                var webResponse = webRequest.GetResponse();
                responseProcessor.Process(webResponse, operation);
            }
            catch (WebException webException) {
                var response = (HttpWebResponse)webException.Response;
                operation.Complete(webException, response.StatusCode, response.StatusDescription);
            }

            return operation;
        }

        private RestOperation PerformOperationAsync(string operationName, IProcessResponses responseProcessor, params object[] args) {
            JsonObject argsObject = null;
            if ((args != null) && (args.Length != 0)) {
                argsObject = (JsonObject)args[0];
            }

            var operation = new RestOperation();

            IHttpRequest webRequest = _requestBuilder.CreateRequest(operationName, argsObject);

            webRequest.BeginGetResponse(ar => {
                try
                {
                    var webResponse = webRequest.EndGetResponse(ar);
                    responseProcessor.Process(webResponse, operation);
                }
                catch (WebException webException) {
                    var response = (HttpWebResponse)webException.Response;
                    operation.Complete(webException, response.StatusCode, response.StatusDescription);
                }
            }, null);

            return operation;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result) {
            object value = _requestBuilder.ParametersStore.GetParameter(binder.Name);
            if (value != null)
            {
                result = value;
                return true;
            }

            string operationGroup = binder.Name;
            if (_operationGroup != null) {
                operationGroup = _operationGroup + "." + operationGroup;
            }

            var operationGroupClient = new RestClient(this, operationGroup);

            result = operationGroupClient;
            return true;
        }

        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) {
            bool async = false;

            string operation = binder.Name;
            if (operation.EndsWith("Async", StringComparison.Ordinal)) {
                async = true;
                operation = operation.Substring(0, operation.Length - 5);
            }

            if (_operationGroup != null) {
                operation = _operationGroup + "." + operation;
            }

            if (async == false) {
                result = PerformOperation(operation, _responseProcessor, args);
            }
            else {
                result = PerformOperationAsync(operation, _responseProcessor, args);
            }
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value) {
            _requestBuilder.ParametersStore.SetParameter(binder.Name, value);
            return true;
        }

        public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) {
            if(indexes.Length == 1 && indexes[0].GetType() == typeof(HttpResponseHeader))
            {
                result = _responseHeaders[(HttpResponseHeader)indexes[0]];
                return true;
            }
            return base.TryGetIndex(binder, indexes, out result);
        }

        public string this[HttpResponseHeader index] {
            get { return _responseHeaders[index]; }
        }
    }
}

{'fraction_non_alphanumeric': 0.07809935205183585, 'fraction_numerical': 0.001900647948164147, 'mean_word_length': 2.0098803952158084, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, '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': '4194312', 'n_tokens_mistral': 2714, 'n_tokens_neox': 2571, 'n_words': 667}