using System.Net.Http.Headers;
using System.Text;

namespace adas_core.module.Relays.Utils;

/// <summary>
/// Utility class for handling HTTP-related operations, such as adding basic authentication headers to HTTP requests.
/// </summary>
public static class HttpUtils
{
    /// <summary>
    /// Adds basic authentication to the provided HttpRequestHeaders.
    /// </summary>
    /// <param name="username">The username for basic authentication.</param>
    /// <param name="password">The password for basic authentication.</param>
    /// <param name="headers">The HttpRequestHeaders to which the authentication will be added.</param>
    /// <returns>The HttpRequestHeaders with the added basic authentication.</returns>
    public static HttpRequestHeaders AddBasicAuth(string username, string password, HttpRequestHeaders headers)
    {
        var auth = Encoding.ASCII.GetBytes($"{username}:{password}");
        var clientAuthorizationHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(auth));
        headers.Add("Authorization", clientAuthorizationHeader.ToString());
        return headers;
    }
}