chore: refactoring some code to introduce a common base class for all api classes

This commit is contained in:
Matthias Langhard
2021-05-21 22:13:04 +02:00
parent ac20e2e1d4
commit 89e7ce8449
34 changed files with 721 additions and 551 deletions

View File

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

View File

@@ -10,7 +10,7 @@ namespace Novaloop.PaymoApi.Extensions
{
if (!response.IsSuccessStatusCode)
{
throw new PaymoApiException((int) response.StatusCode, await response.Content.ReadAsStringAsync());
throw new ApiException((int) response.StatusCode, await response.Content.ReadAsStringAsync());
}
}
}

View File

@@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Novaloop.PaymoApi.ClientContacts;
using Novaloop.PaymoApi.Clients;
using Novaloop.PaymoApi.Clients.Models;
using Novaloop.PaymoApi.Shared;
using Novaloop.PaymoApi.Tasks;
@@ -10,14 +11,15 @@ namespace Novaloop.PaymoApi.Extensions
{
public static class PaymoApiExtensions
{
public static IServiceCollection AddPaymoApi(this IServiceCollection services, Action<PaymoApiOptions> options)
public static IServiceCollection AddPaymoApi(this IServiceCollection services, Action<ApiOptions> 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<IPaymoTasksApi, PaymoTasksApi>();
services.AddTransient<IPaymoClientContactsApi, PaymoClientContactsApi>();
services.AddTransient<IPaymoClientsApi, PaymoClientsApi>();
var resolvedOptions = (IOptions<ApiOptions>) services.BuildServiceProvider().GetService(typeof(IOptions<ApiOptions>));
services.AddHttpClient<ApiClient>(client => { client.BaseAddress = new Uri(resolvedOptions.Value.BaseUrl); });
services.AddTransient<IBaseApi<ClientsResponse, Client>, BaseApi<ClientsResponse, Client>>();
services.AddTransient<ITasksApi, TasksApi>();
services.AddTransient<IClientContactsApi, ClientContactsApi>();
services.AddTransient<IClientsApi, ClientsApi>();
return services;
}
}