chore: initial commit
This commit is contained in:
37
src/Extensions/HttpClientExtensions.cs
Normal file
37
src/Extensions/HttpClientExtensions.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
17
src/Extensions/HttpResponseMessageExtensions.cs
Normal file
17
src/Extensions/HttpResponseMessageExtensions.cs
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
src/Extensions/PaymoApiExtensions.cs
Normal file
20
src/Extensions/PaymoApiExtensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
8
src/Extensions/PaymoApiOptions.cs
Normal file
8
src/Extensions/PaymoApiOptions.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user