chore: initial commit

This commit is contained in:
Matthias Langhard
2020-12-07 22:00:24 +01:00
commit 75a687e31e
20 changed files with 507 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
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));
}
}
}

View File

@@ -0,0 +1,17 @@
using System.Net.Http;
using System.Threading.Tasks;
using Novaloop.PaymoApi.Exceptions;
namespace Novaloop.PaymoApi.Extensions
{
internal static class HttpResponseMessageExtensions
{
internal static async Task ThrowExceptionWithDetailsIfUnsuccessful(this HttpResponseMessage response)
{
if (!response.IsSuccessStatusCode)
{
throw new PaymoApiException((int) response.StatusCode, await response.Content.ReadAsStringAsync());
}
}
}
}

View File

@@ -0,0 +1,20 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Novaloop.PaymoApi.Shared;
using Novaloop.PaymoApi.Tasks;
namespace Novaloop.PaymoApi.Extensions
{
public static class PaymoApiExtensions
{
public static IServiceCollection AddPaymoApi(this IServiceCollection services, Action<PaymoApiOptions> options)
{
services.Configure(options);
var resolvedOptions = (IOptions<PaymoApiOptions>) services.BuildServiceProvider().GetService(typeof(IOptions<PaymoApiOptions>));
services.AddHttpClient<PaymoApiClient>(client => { client.BaseAddress = new Uri(resolvedOptions.Value.BaseUrl); });
services.AddTransient<IPaymoTasksApiService, PaymoTasksApiService>();
return services;
}
}
}

View File

@@ -0,0 +1,8 @@
namespace Novaloop.PaymoApi.Extensions
{
public class PaymoApiOptions
{
public string BaseUrl { get; set; } = "https://app.paymoapp.com/api";
public string ApiToken { get; set; }
}
}