<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;
using QlikView.Qvx.QvxLibrary;
using SalesforceReportsConnector.Logger;
using SalesforceReportsConnector.QVX;

namespace SalesforceReportsConnector.SalesforceAPI
{
	public class EndpointCalls
	{
		public static T ValidateAccessTokenAndPerformRequest<T>(QvxConnection connection, IDictionary<string, string> connectionParams, Func<string, T> endpointCall)
		{
			try
			{
				return endpointCall(connectionParams[QvxSalesforceConnectionInfo.CONNECTION_ACCESS_TOKEN]);

			}
			catch (WebException e)
			{
				if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized || ((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Forbidden)
				{
					Uri authHostnameUri = new Uri(connectionParams[QvxSalesforceConnectionInfo.CONNECTION_AUTHHOST]);
					string newTokenPath = string.Format("/services/oauth2/token?grant_type=refresh_token&client_id={0}&refresh_token={1}", QvxSalesforceConnectionInfo.CLIENT_ID, connectionParams[QvxSalesforceConnectionInfo.CONNECTION_REFRESH_TOKEN]);
					HttpWebRequest newTokenRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(authHostnameUri, newTokenPath));
					newTokenRequest.Method = "POST";

					string newAccessToken = connectionParams[QvxSalesforceConnectionInfo.CONNECTION_ACCESS_TOKEN];
					using (HttpWebResponse newTokenResponse = (HttpWebResponse)newTokenRequest.GetResponse())
					{
						using (Stream stream = newTokenResponse.GetResponseStream())
						{
							StreamReader reader = new StreamReader(stream, Encoding.UTF8);
							String responseString = reader.ReadToEnd();
							JObject response = JObject.Parse(responseString);
							newAccessToken = response["access_token"].Value<string>();
						}
					}
					connection.MParameters[QvxSalesforceConnectionInfo.CONNECTION_ACCESS_TOKEN] = newAccessToken;
					TempLogger.Log("Access token:");
					TempLogger.Log(newAccessToken);
					try
					{
						return endpointCall(newAccessToken);
					}
					catch
					{
						return default(T);
					}
				}
				else
				{
					throw new Exception("Invalid Web Response");
				}
			}
			catch (Exception)
			{
				return default(T);
			}
		}

		public static string GetUsername(QvxConnection connection, string authHostname, string accessToken, string refreshToken, string hostname, string idURL)
		{
			Dictionary<string, string> connectionValues = new Dictionary<string, string>();
			connectionValues.Add(QvxSalesforceConnectionInfo.CONNECTION_HOST, hostname);
			connectionValues.Add(QvxSalesforceConnectionInfo.CONNECTION_AUTHHOST, authHostname);<commit_msg>small bug<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;
using QlikView.Qvx.QvxLibrary;
using SalesforceReportsConnector.Logger;
using SalesforceReportsConnector.QVX;

namespace SalesforceReportsConnector.SalesforceAPI
{
	public class EndpointCalls
	{
		public static T ValidateAccessTokenAndPerformRequest<T>(QvxConnection connection, IDictionary<string, string> connectionParams, Func<string, T> endpointCall)
		{
			try
			{
				return endpointCall(connectionParams[QvxSalesforceConnectionInfo.CONNECTION_ACCESS_TOKEN]);

			}
			catch (WebException e)
			{
				if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized || ((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Forbidden)
				{
					Uri authHostnameUri = new Uri(connectionParams[QvxSalesforceConnectionInfo.CONNECTION_AUTHHOST]);
					string newTokenPath = string.Format("/services/oauth2/token?grant_type=refresh_token&client_id={0}&refresh_token={1}", QvxSalesforceConnectionInfo.CLIENT_ID, connectionParams[QvxSalesforceConnectionInfo.CONNECTION_REFRESH_TOKEN]);
					HttpWebRequest newTokenRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(authHostnameUri, newTokenPath));
					newTokenRequest.Method = "POST";

					string newAccessToken = connectionParams[QvxSalesforceConnectionInfo.CONNECTION_ACCESS_TOKEN];
					using (HttpWebResponse newTokenResponse = (HttpWebResponse)newTokenRequest.GetResponse())
					{
						using (Stream stream = newTokenResponse.GetResponseStream())
						{
							StreamReader reader = new StreamReader(stream, Encoding.UTF8);
							String responseString = reader.ReadToEnd();
							JObject response = JObject.Parse(responseString);
							newAccessToken = response["access_token"].Value<string>();
						}
					}
					connection.MParameters[QvxSalesforceConnectionInfo.CONNECTION_ACCESS_TOKEN] = newAccessToken;

					try
					{
						return endpointCall(newAccessToken);
					}
					catch
					{
						return default(T);
					}
				}
				else
				{
					throw new Exception("Invalid Web Response");
				}
			}
			catch (Exception)
			{
				return default(T);
			}
		}

		public static string GetUsername(QvxConnection connection, string authHostname, string accessToken, string refreshToken, string hostname, string idURL)
		{
			Dictionary<string, string> connectionValues = new Dictionary<string, string>();
			connectionValues.Add(QvxSalesforceConnectionInfo.CONNECTION_HOST, hostname);
			connectionValues.Add(QvxSalesforceConnectionInfo.CONNECTION_AUTHHOST, authHostname);
{'fraction_non_alphanumeric': 0.09078996630475478, 'fraction_numerical': 0.001497566454511419, 'mean_word_length': 5.057823129251701, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '1214518', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1501, 'n_words': 263}