chore: refactoring some code to introduce a common base class for all api classes
This commit is contained in:
49
src/Clients/ClientsApi.cs
Normal file
49
src/Clients/ClientsApi.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Novaloop.PaymoApi.Clients.Models;
|
||||
using Novaloop.PaymoApi.Shared;
|
||||
|
||||
namespace Novaloop.PaymoApi.Clients
|
||||
{
|
||||
public class ClientsApi : IClientsApi
|
||||
{
|
||||
private readonly IBaseApi<ClientsResponse, Client> _baseApi;
|
||||
|
||||
public ClientsApi(IBaseApi<ClientsResponse, Client> baseApi)
|
||||
{
|
||||
_baseApi = baseApi;
|
||||
_baseApi.ResourceUri = "clients";
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IEnumerable<Client>> GetClients()
|
||||
{
|
||||
return (await _baseApi.GetAll()).Clients;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<Client> GetClient(int clientId)
|
||||
{
|
||||
return (await _baseApi.Get(clientId)).Clients.Single();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<Client> CreateClient(Client client)
|
||||
{
|
||||
return (await _baseApi.Create(client)).Clients.Single();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task DeleteClient(int clientId)
|
||||
{
|
||||
await _baseApi.Delete(clientId);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task UpdateClient(Client client, int clientId)
|
||||
{
|
||||
await _baseApi.Update(client, clientId);
|
||||
}
|
||||
}
|
||||
}
|
||||
41
src/Clients/IClientsApi.cs
Normal file
41
src/Clients/IClientsApi.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Novaloop.PaymoApi.Clients.Models;
|
||||
|
||||
namespace Novaloop.PaymoApi.Clients
|
||||
{
|
||||
public interface IClientsApi
|
||||
{
|
||||
/// <summary>
|
||||
/// Receive all existing clients
|
||||
/// </summary>
|
||||
Task<IEnumerable<Client>> GetClients();
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve an existing client by id
|
||||
/// </summary>
|
||||
/// <param name="clientId">id of the contact</param>
|
||||
/// <returns></returns>
|
||||
Task<Client> GetClient(int clientId);
|
||||
|
||||
/// <summary>
|
||||
/// Create a new client
|
||||
/// </summary>
|
||||
Task<Client> CreateClient(Client client);
|
||||
|
||||
/// <summary>
|
||||
/// Delete a client
|
||||
/// </summary>
|
||||
/// <param name="clientId"></param>
|
||||
/// <returns></returns>
|
||||
Task DeleteClient(int clientId);
|
||||
|
||||
/// <summary>
|
||||
/// Update an existing client
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="clientId"></param>
|
||||
/// <returns></returns>
|
||||
Task UpdateClient(Client client, int clientId);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Novaloop.PaymoApi.Clients.Models;
|
||||
|
||||
namespace Novaloop.PaymoApi.Clients
|
||||
{
|
||||
public interface IPaymoClientsApi
|
||||
{
|
||||
Task<IEnumerable<PaymoClient>> GetClients();
|
||||
Task<PaymoClient> GetClient(int clientId);
|
||||
Task<PaymoClient> CreateClient(PaymoClient client);
|
||||
Task DeleteClient(int clientId);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ using Novaloop.PaymoApi.Shared;
|
||||
|
||||
namespace Novaloop.PaymoApi.Clients.Models
|
||||
{
|
||||
public class PaymoClient : BasePaymoModel
|
||||
public class Client : BaseModel
|
||||
{
|
||||
[JsonProperty("name")]
|
||||
public string Name { get; set; }
|
||||
@@ -42,7 +42,7 @@ namespace Novaloop.PaymoApi.Clients.Models
|
||||
public string FiscalInformation { get; set; }
|
||||
|
||||
[JsonProperty("active")]
|
||||
public bool Active { get; set; }
|
||||
public bool Active { get; set; } = true;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
@@ -3,9 +3,9 @@ using Newtonsoft.Json;
|
||||
|
||||
namespace Novaloop.PaymoApi.Clients.Models
|
||||
{
|
||||
public class GetClientsResponse
|
||||
public class ClientsResponse
|
||||
{
|
||||
[JsonProperty("clients")]
|
||||
public IEnumerable<PaymoClient> Clients { get; set; }
|
||||
public IEnumerable<Client> Clients { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Novaloop.PaymoApi.Clients.Models;
|
||||
using Novaloop.PaymoApi.Extensions;
|
||||
using Novaloop.PaymoApi.Shared;
|
||||
|
||||
namespace Novaloop.PaymoApi.Clients
|
||||
{
|
||||
public class PaymoClientsApi : IPaymoClientsApi
|
||||
{
|
||||
private readonly PaymoApiOptions _options;
|
||||
private readonly HttpClient _client;
|
||||
private const string ResourceUri = "clients";
|
||||
|
||||
public PaymoClientsApi(PaymoApiClient paymoApiClient, IOptions<PaymoApiOptions> options)
|
||||
{
|
||||
_options = options.Value;
|
||||
_client = paymoApiClient.Client;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Receive all existing clients
|
||||
/// </summary>
|
||||
public async Task<IEnumerable<PaymoClient>> GetClients()
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.GetAsync($"api/{ResourceUri}");
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
return (await response.Content.ReadAsAsync<GetClientsResponse>()).Clients;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve an existing client by id
|
||||
/// </summary>
|
||||
/// <param name="clientId">id of the contact</param>
|
||||
/// <returns></returns>
|
||||
public async Task<PaymoClient> GetClient(int clientId)
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.GetAsync($"api/{ResourceUri}/{clientId}");
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
return (await response.Content.ReadAsAsync<GetClientsResponse>()).Clients.Single();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create a new client
|
||||
/// </summary>
|
||||
public async Task<PaymoClient> CreateClient(PaymoClient client)
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.PostAsJsonAsync($"api/{ResourceUri}", client);
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
return (await response.Content.ReadAsAsync<GetClientsResponse>()).Clients.Single();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete a client
|
||||
/// </summary>
|
||||
/// <param name="clientId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task DeleteClient(int clientId)
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.DeleteAsync($"api/{ResourceUri}/{clientId}");
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user