<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml;
using Exceptions;
using SendGrid.SmtpApi;

// ReSharper disable MemberCanBePrivate.Global
namespace SendGrid
{
	public class Web : ITransport
	{
		#region Properties

		//TODO: Make this configurable
		public const String Endpoint = "https://api.sendgrid.com/api/mail.send.xml";

		private readonly NetworkCredential _credentials;
	    	private readonly TimeSpan _timeout;

		#endregion

		/// <summary>
		///     Creates a new Web interface for sending mail
		/// </summary>
		/// <param name="credentials">SendGridMessage user parameters</param>
		public Web(NetworkCredential credentials)
		{
			_credentials = credentials;
		    _timeout = TimeSpan.FromSeconds(100);
		}

        /// <summary>
        ///     Creates a new Web interface for sending mail.
        /// </summary>
        /// <param name="credentials">SendGridMessage user parameters</param>
        /// <param name="httpTimeout">HTTP request timeout</param>
	    public Web(NetworkCredential credentials, TimeSpan httpTimeout)
	    {
        	_credentials = credentials;
	    	_timeout = httpTimeout;
	    }

		/// <summary>
		///     Asynchronously delivers a message over SendGrid's Web interface
		/// </summary>
		/// <param name="message"></param>
		public async Task DeliverAsync(ISendGrid message)
		{
			var client = new HttpClient();

            var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();

			client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp");

			var content = new MultipartFormDataContent();
			AttachFormParams(message, content);
			AttachFiles(message, content);
			var response = await client.PostAsync(Endpoint, content);
			await CheckForErrorsAsync(response);
		}

	    #region Support Methods

		private void AttachFormParams(ISendGrid message, MultipartFormDataContent content)
		{
			var formParams = FetchFormParams(message);
			foreach (var keyValuePair in formParams)<commit_msg>Improve performance by reusing connections per instance of web transport<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml;
using Exceptions;
using SendGrid.SmtpApi;

// ReSharper disable MemberCanBePrivate.Global
namespace SendGrid
{
	public class Web : ITransport
	{
		#region Properties

		//TODO: Make this configurable
		public const String Endpoint = "https://api.sendgrid.com/api/mail.send.xml";
	    
		private readonly NetworkCredential _credentials;
	    private readonly HttpClient _client;

		#endregion

		/// <summary>
		///     Creates a new Web interface for sending mail
		/// </summary>
		/// <param name="credentials">SendGridMessage user parameters</param>
        public Web(NetworkCredential credentials)
            : this(credentials, TimeSpan.FromSeconds(100)) { }

        /// <summary>
        ///     Creates a new Web interface for sending mail.
        /// </summary>
        /// <param name="credentials">SendGridMessage user parameters</param>
        /// <param name="httpTimeout">HTTP request timeout</param>
	    public Web(NetworkCredential credentials, TimeSpan httpTimeout)
	    {
        	_credentials = credentials;
            _client = new HttpClient();
            
            var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            _client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp");
            _client.Timeout = httpTimeout;
	    }

		/// <summary>
		///     Asynchronously delivers a message over SendGrid's Web interface
		/// </summary>
		/// <param name="message"></param>
		public async Task DeliverAsync(ISendGrid message)
		{
			var content = new MultipartFormDataContent();
			AttachFormParams(message, content);
			AttachFiles(message, content);
			var response = await _client.PostAsync(Endpoint, content);
			await CheckForErrorsAsync(response);
		}

	    #region Support Methods

		private void AttachFormParams(ISendGrid message, MultipartFormDataContent content)
		{
			var formParams = FetchFormParams(message);
			foreach (var keyValuePair in formParams)
{'fraction_non_alphanumeric': 0.10431494661921709, 'fraction_numerical': 0.0013345195729537367, 'mean_word_length': 3.470178926441352, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '395329', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1205, 'n_words': 327}