chore: refactoring some code to introduce a common base class for all api classes
This commit is contained in:
@@ -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; }
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user