<commit_before>﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Solomobro.Instagram.Exceptions;
using Solomobro.Instagram.Interfaces;
using Solomobro.Instagram.Models;

namespace Solomobro.Instagram.Authentication
{
    /// <summary>
    /// Internal-only class to help make OAuth testable by abstracting code that calls instagram over the network
    /// </summary>
    internal class AccessTokenRetriever : IAccessTokenRetriever
    {
        private readonly string _clientId;
        private readonly string _clientSecret;
        private readonly string _redirectUri;
        private readonly string _accessCode;

        public AccessTokenRetriever(string clientId, string clientSecret, string redirectUri, string accessCode)
        {
            _clientId = clientId;
            _clientSecret = clientSecret;
            _redirectUri = redirectUri;
            _accessCode = accessCode;
        }

        public async Task<ExplicitAuthResponse> Authenticate(Uri authEndpoint, IEnumerable<KeyValuePair<string, string>> authParams)
        {
            var data = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("client_id", _clientId),
                    new KeyValuePair<string, string>("client_secret", _clientSecret),
                    new KeyValuePair<string, string>("grant_type", "authorization_code"),
                    new KeyValuePair<string, string>("redirect_uri", _redirectUri),
                    new KeyValuePair<string, string>("code", _accessCode)
                });

            using (var http = new HttpClient(new HttpClientHandler {AllowAutoRedirect = false}))
            {
                var resp = await http.PostAsync(authEndpoint, data).ConfigureAwait(false);
                resp.EnsureSuccessStatusCode();

                var content = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);

                var userInfo = JsonConvert.DeserializeObject<ExplicitAuthResponse>(content);
                if (userInfo == null)
                {
                    throw new OAuthException(content);
                }

                return userInfo;
            }
        }
    }
}<commit_msg>dispose of http response in AccessTokenRetriever<commit_after>﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Solomobro.Instagram.Exceptions;
using Solomobro.Instagram.Interfaces;
using Solomobro.Instagram.Models;

namespace Solomobro.Instagram.Authentication
{
    /// <summary>
    /// Internal-only class to help make OAuth testable by abstracting code that calls instagram over the network
    /// </summary>
    internal class AccessTokenRetriever : IAccessTokenRetriever
    {
        private readonly string _clientId;
        private readonly string _clientSecret;
        private readonly string _redirectUri;
        private readonly string _accessCode;

        public AccessTokenRetriever(string clientId, string clientSecret, string redirectUri, string accessCode)
        {
            _clientId = clientId;
            _clientSecret = clientSecret;
            _redirectUri = redirectUri;
            _accessCode = accessCode;
        }

        public async Task<ExplicitAuthResponse> Authenticate(Uri authEndpoint, IEnumerable<KeyValuePair<string, string>> authParams)
        {
            var data = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("client_id", _clientId),
                    new KeyValuePair<string, string>("client_secret", _clientSecret),
                    new KeyValuePair<string, string>("grant_type", "authorization_code"),
                    new KeyValuePair<string, string>("redirect_uri", _redirectUri),
                    new KeyValuePair<string, string>("code", _accessCode)
                });

            using (var http = new HttpClient(new HttpClientHandler {AllowAutoRedirect = false}))
            using (var resp = await http.PostAsync(authEndpoint, data).ConfigureAwait(false))
            {
                resp.EnsureSuccessStatusCode();

                var content = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);

                var userInfo = JsonConvert.DeserializeObject<ExplicitAuthResponse>(content);
                if (userInfo == null)
                {
                    throw new OAuthException(content);
                }

                return userInfo;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07891304347826086, 'fraction_numerical': 0.0, 'mean_word_length': 2.365764447695684, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '2141851', 'n_tokens_mistral': 1150, 'n_tokens_neox': 1119, 'n_words': 274}