37 lines
1.3 KiB
C#
37 lines
1.3 KiB
C#
using System;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Novaloop.PaymoApi.Extensions
|
|
{
|
|
internal static class HttpClientExtensions
|
|
{
|
|
internal static void SetApiKeyHeader(this HttpClient client, string apiKey)
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Base64Encode($"{apiKey}:random"));
|
|
}
|
|
|
|
internal static async Task<HttpResponseMessage> PatchAsync(this HttpClient client, string uri, HttpContent content)
|
|
{
|
|
var method = new HttpMethod("PATCH");
|
|
if (client.BaseAddress is null)
|
|
{
|
|
throw new ArgumentException("Can not handle 'BaseAddress' null value configuration.");
|
|
}
|
|
|
|
var request = new HttpRequestMessage(method, new Uri(CombineBaseUrlWithSegment(client.BaseAddress.ToString(), uri))) {Content = content};
|
|
return await client.SendAsync(request);
|
|
}
|
|
|
|
private static string CombineBaseUrlWithSegment(string uri1, string uri2)
|
|
{
|
|
return $"{uri1.TrimEnd('/')}/{uri2.TrimStart('/')}";
|
|
}
|
|
|
|
private static string Base64Encode(string plainText)
|
|
{
|
|
return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(plainText));
|
|
}
|
|
}
|
|
} |