<commit_before>﻿/*
 * Copyright 2014, 2015 <NAME>, <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace IdentityModel.Client
{
    public class OAuth2Client
    {
        protected HttpClient _client;
        
        public enum ClientAuthenticationStyle
        {
            BasicAuthentication,
            PostValues,
            None
        };

        public ClientAuthenticationStyle AuthenticationStyle { get; set; }
        public string ClientId { get; set; }
        public string ClientSecret { get; set; }

        public OAuth2Client(Uri address)
            : this(address, new HttpClientHandler())
        { }

        public OAuth2Client(Uri address, HttpMessageHandler innerHttpClientHandler)
        {
            if (address == null) throw new ArgumentNullException("address");
            if (innerHttpClientHandler == null) throw new ArgumentNullException("innerHttpClientHandler");
            
            _client = new HttpClient(innerHttpClientHandler)
            {
                BaseAddress = address
            };

            AuthenticationStyle = ClientAuthenticationStyle.None;
        }

        public OAuth2Client(Uri address, string clientId, string clientSecret, ClientAuthenticationStyle style = ClientAuthenticationStyle.BasicAuthentication)
            : this(address, clientId, clientSecret, new HttpClientHandler(), style)
        { }

        public OAuth2Client(Uri address, string clientId, ClientAuthenticationStyle style = ClientAuthenticationStyle.BasicAuthentication)
            : this(address, clientId, string.Empty, new HttpClientHandler(), style)
        { }

        public OAuth2Client(Uri address, string clientId, HttpMessageHandler innerHttpClientHandler)
            : this(address, clientId, string.Empty, innerHttpClientHandler, ClientAuthenticationStyle.PostValues)
        { }

        public OAuth2Client(Uri address, string clientId, string clientSecret, HttpMessageHandler innerHttpClientHandler, ClientAuthenticationStyle style = ClientAuthenticationStyle.BasicAuthentication)
            : this(address, innerHttpClientHandler)
        {
            if (string.IsNullOrEmpty(clientId)) throw new ArgumentNullException("ClientId");

            if (style == ClientAuthenticationStyle.BasicAuthentication)
            {
                _client.DefaultRequestHeaders.Authorization = new BasicAuthenticationHeaderValue(clientId, clientSecret);
            }
            else if (style == ClientAuthenticationStyle.PostValues)
            {
                AuthenticationStyle = style;
                ClientId = clientId;
                ClientSecret = clientSecret;
            }
        }

        public TimeSpan Timeout 
        { 
            set
            {
                _client.Timeout = value;
            }
        }

        public virtual async Task<TokenResponse> RequestAsync(Dictionary<string, string> form, CancellationToken cancellationToken = default(CancellationToken))
        {
            var response = await _client.PostAsync(string.Empty, new FormUrlEncodedContent(form), cancellationToken).ConfigureAwait(false);

            if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.BadRequest)
            {
                var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                return new TokenResponse(content);
            }
            else
            {
                return new TokenResponse(response.StatusCode, response.ReasonPhrase);
            }
        }
    }
}<commit_msg>changed to IDictionary<commit_after>﻿/*
 * Copyright 2014, 2015 <NAME>, <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace IdentityModel.Client
{
    public class OAuth2Client
    {
        protected HttpClient _client;
        
        public enum ClientAuthenticationStyle
        {
            BasicAuthentication,
            PostValues,
            None
        };

        public ClientAuthenticationStyle AuthenticationStyle { get; set; }
        public string ClientId { get; set; }
        public string ClientSecret { get; set; }

        public OAuth2Client(Uri address)
            : this(address, new HttpClientHandler())
        { }

        public OAuth2Client(Uri address, HttpMessageHandler innerHttpClientHandler)
        {
            if (address == null) throw new ArgumentNullException("address");
            if (innerHttpClientHandler == null) throw new ArgumentNullException("innerHttpClientHandler");
            
            _client = new HttpClient(innerHttpClientHandler)
            {
                BaseAddress = address
            };

            AuthenticationStyle = ClientAuthenticationStyle.None;
        }

        public OAuth2Client(Uri address, string clientId, string clientSecret, ClientAuthenticationStyle style = ClientAuthenticationStyle.BasicAuthentication)
            : this(address, clientId, clientSecret, new HttpClientHandler(), style)
        { }

        public OAuth2Client(Uri address, string clientId, ClientAuthenticationStyle style = ClientAuthenticationStyle.BasicAuthentication)
            : this(address, clientId, string.Empty, new HttpClientHandler(), style)
        { }

        public OAuth2Client(Uri address, string clientId, HttpMessageHandler innerHttpClientHandler)
            : this(address, clientId, string.Empty, innerHttpClientHandler, ClientAuthenticationStyle.PostValues)
        { }

        public OAuth2Client(Uri address, string clientId, string clientSecret, HttpMessageHandler innerHttpClientHandler, ClientAuthenticationStyle style = ClientAuthenticationStyle.BasicAuthentication)
            : this(address, innerHttpClientHandler)
        {
            if (string.IsNullOrEmpty(clientId)) throw new ArgumentNullException("ClientId");

            if (style == ClientAuthenticationStyle.BasicAuthentication)
            {
                _client.DefaultRequestHeaders.Authorization = new BasicAuthenticationHeaderValue(clientId, clientSecret);
            }
            else if (style == ClientAuthenticationStyle.PostValues)
            {
                AuthenticationStyle = style;
                ClientId = clientId;
                ClientSecret = clientSecret;
            }
        }

        public TimeSpan Timeout 
        { 
            set
            {
                _client.Timeout = value;
            }
        }

        public virtual async Task<TokenResponse> RequestAsync(IDictionary<string, string> form, CancellationToken cancellationToken = default(CancellationToken))
        {
            var response = await _client.PostAsync(string.Empty, new FormUrlEncodedContent(form), cancellationToken).ConfigureAwait(false);

            if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.BadRequest)
            {
                var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                return new TokenResponse(content);
            }
            else
            {
                return new TokenResponse(response.StatusCode, response.ReasonPhrase);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07087172218284904, 'fraction_numerical': 0.0044885424049137725, 'mean_word_length': 2.5293872446852856, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5520681', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1905, 'n_words': 548}