chore: refactoring some code to introduce a common base class for all api classes
This commit is contained in:
49
src/ClientContacts/ClientContactsApi.cs
Normal file
49
src/ClientContacts/ClientContactsApi.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Novaloop.PaymoApi.ClientContacts.Models;
|
||||
using Novaloop.PaymoApi.Shared;
|
||||
|
||||
namespace Novaloop.PaymoApi.ClientContacts
|
||||
{
|
||||
public class ClientContactsApi : IClientContactsApi
|
||||
{
|
||||
private readonly IBaseApi<GetClientContactsResponse, ClientContact> _baseApi;
|
||||
|
||||
public ClientContactsApi(IBaseApi<GetClientContactsResponse, ClientContact> baseApi)
|
||||
{
|
||||
_baseApi = baseApi;
|
||||
_baseApi.ResourceUri = "clientcontacts";
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IEnumerable<ClientContact>> GetClientContacts()
|
||||
{
|
||||
return (await _baseApi.GetAll()).ClientContacts;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<ClientContact> GetClientContact(int clientContactId)
|
||||
{
|
||||
return (await _baseApi.Get(clientContactId)).ClientContacts.Single();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<ClientContact> CreateClientContact(ClientContact clientContact)
|
||||
{
|
||||
return (await _baseApi.Create(clientContact)).ClientContacts.Single();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task DeleteClientContact(int clientContactId)
|
||||
{
|
||||
await _baseApi.Delete(clientContactId);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task UpdateClientContact(ClientContact clientContact, int clientContactId)
|
||||
{
|
||||
await _baseApi.Update(clientContact, clientContactId);
|
||||
}
|
||||
}
|
||||
}
|
||||
41
src/ClientContacts/IClientContactsApi.cs
Normal file
41
src/ClientContacts/IClientContactsApi.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Novaloop.PaymoApi.ClientContacts.Models;
|
||||
|
||||
namespace Novaloop.PaymoApi.ClientContacts
|
||||
{
|
||||
public interface IClientContactsApi
|
||||
{
|
||||
/// <summary>
|
||||
/// Receive all existing client contacts
|
||||
/// </summary>
|
||||
Task<IEnumerable<ClientContact>> GetClientContacts();
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve an existing client contact by id
|
||||
/// </summary>
|
||||
/// <param name="clientContactId">id of the contact</param>
|
||||
/// <returns></returns>
|
||||
Task<ClientContact> GetClientContact(int clientContactId);
|
||||
|
||||
/// <summary>
|
||||
/// Create a new client contact
|
||||
/// </summary>
|
||||
Task<ClientContact> CreateClientContact(ClientContact clientContact);
|
||||
|
||||
/// <summary>
|
||||
/// Delete a client contact
|
||||
/// </summary>
|
||||
/// <param name="clientContactId"></param>
|
||||
/// <returns></returns>
|
||||
Task DeleteClientContact(int clientContactId);
|
||||
|
||||
/// <summary>
|
||||
/// Update an existing client contact
|
||||
/// </summary>
|
||||
/// <param name="clientContact"></param>
|
||||
/// <param name="clientContactId"></param>
|
||||
/// <returns></returns>
|
||||
Task UpdateClientContact(ClientContact clientContact, int clientContactId);
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Novaloop.PaymoApi.ClientContacts.Models;
|
||||
|
||||
namespace Novaloop.PaymoApi.ClientContacts
|
||||
{
|
||||
public interface IPaymoClientContactsApi
|
||||
{
|
||||
Task<IEnumerable<PaymoClientContact>> GetClientContacts();
|
||||
Task<PaymoClientContact> GetClientContact(int contactId);
|
||||
Task<PaymoClientContact> CreateClientContact(PaymoClientContact clientContact);
|
||||
|
||||
/// <summary>
|
||||
/// Deleta a client contact
|
||||
/// </summary>
|
||||
/// <param name="clientContactId"></param>
|
||||
/// <returns></returns>
|
||||
Task DeleteClientContact(int clientContactId);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ using Novaloop.PaymoApi.Shared;
|
||||
|
||||
namespace Novaloop.PaymoApi.ClientContacts.Models
|
||||
{
|
||||
public class PaymoClientContact : BasePaymoModel
|
||||
public class ClientContact : BaseModel
|
||||
{
|
||||
[JsonProperty("client_id")]
|
||||
public int ClientId { get; set; }
|
||||
@@ -6,6 +6,6 @@ namespace Novaloop.PaymoApi.ClientContacts.Models
|
||||
public class GetClientContactsResponse
|
||||
{
|
||||
[JsonProperty("clientcontacts")]
|
||||
public IEnumerable<PaymoClientContact> ClientContacts { get; set; }
|
||||
public IEnumerable<ClientContact> ClientContacts { 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.ClientContacts.Models;
|
||||
using Novaloop.PaymoApi.Extensions;
|
||||
using Novaloop.PaymoApi.Shared;
|
||||
|
||||
namespace Novaloop.PaymoApi.ClientContacts
|
||||
{
|
||||
public class PaymoClientContactsApi : IPaymoClientContactsApi
|
||||
{
|
||||
private readonly PaymoApiOptions _options;
|
||||
private readonly HttpClient _client;
|
||||
private const string ResourceUri = "clientcontacts";
|
||||
|
||||
public PaymoClientContactsApi(PaymoApiClient paymoApiClient, IOptions<PaymoApiOptions> options)
|
||||
{
|
||||
_options = options.Value;
|
||||
_client = paymoApiClient.Client;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Receive all existing client contacts
|
||||
/// </summary>
|
||||
public async Task<IEnumerable<PaymoClientContact>> GetClientContacts()
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.GetAsync($"api/{ResourceUri}");
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
return (await response.Content.ReadAsAsync<GetClientContactsResponse>()).ClientContacts;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve an existing client contact by id
|
||||
/// </summary>
|
||||
/// <param name="clientContactId">id of the contact</param>
|
||||
/// <returns></returns>
|
||||
public async Task<PaymoClientContact> GetClientContact(int clientContactId)
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.GetAsync($"api/{ResourceUri}/{clientContactId}");
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
return (await response.Content.ReadAsAsync<GetClientContactsResponse>()).ClientContacts.Single();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create a new client contact
|
||||
/// </summary>
|
||||
public async Task<PaymoClientContact> CreateClientContact(PaymoClientContact clientContact)
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.PostAsJsonAsync($"api/{ResourceUri}", clientContact);
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
return (await response.Content.ReadAsAsync<GetClientContactsResponse>()).ClientContacts.Single();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deleta a client contact
|
||||
/// </summary>
|
||||
/// <param name="clientContactId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task DeleteClientContact(int clientContactId)
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.DeleteAsync($"api/{ResourceUri}/{clientContactId}");
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
}
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@ using System;
|
||||
|
||||
namespace Novaloop.PaymoApi.Exceptions
|
||||
{
|
||||
public class PaymoApiException : Exception
|
||||
public class ApiException : Exception
|
||||
{
|
||||
public PaymoApiException(int statusCode, string message) : base($"[{statusCode}]: {message})")
|
||||
public ApiException(int statusCode, string message) : base($"[{statusCode}]: {message})")
|
||||
{
|
||||
StatusCode = statusCode;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ using System.Net.Http;
|
||||
|
||||
namespace Novaloop.PaymoApi.Shared
|
||||
{
|
||||
public class PaymoApiClient : IPaymoApiClient
|
||||
public class ApiClient : IApiClient
|
||||
{
|
||||
public PaymoApiClient(HttpClient client)
|
||||
public ApiClient(HttpClient client)
|
||||
{
|
||||
Client = client;
|
||||
}
|
||||
80
src/Shared/BaseApi.cs
Normal file
80
src/Shared/BaseApi.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Novaloop.PaymoApi.Extensions;
|
||||
|
||||
namespace Novaloop.PaymoApi.Shared
|
||||
{
|
||||
public class BaseApi<TReturnType, TCreatType> : IBaseApi<TReturnType, TCreatType>
|
||||
{
|
||||
public string ResourceUri { get; set; }
|
||||
private readonly ApiOptions _options;
|
||||
private readonly HttpClient _client;
|
||||
|
||||
public BaseApi(ApiClient apiClient, IOptions<ApiOptions> options)
|
||||
{
|
||||
_options = options.Value;
|
||||
_client = apiClient.Client;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Receive all entities
|
||||
/// </summary>
|
||||
public async Task<TReturnType> GetAll()
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.GetAsync($"api/{ResourceUri}");
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
return await response.Content.ReadAsAsync<TReturnType>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve an existing entity
|
||||
/// </summary>
|
||||
/// <param name="entityId">id of the entity</param>
|
||||
/// <returns></returns>
|
||||
public async Task<TReturnType> Get(int entityId)
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.GetAsync($"api/{ResourceUri}/{entityId}");
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
return await response.Content.ReadAsAsync<TReturnType>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new entity
|
||||
/// </summary>
|
||||
public async Task<TReturnType> Create(TCreatType entity)
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.PostAsJsonAsync($"api/{ResourceUri}", entity);
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
return await response.Content.ReadAsAsync<TReturnType>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete an entity
|
||||
/// </summary>
|
||||
/// <param name="entityId">id of the entity</param>
|
||||
/// <returns></returns>
|
||||
public async Task Delete(int entityId)
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.DeleteAsync($"api/{ResourceUri}/{entityId}");
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update an entity
|
||||
/// </summary>
|
||||
/// <param name="entity">entity information to update the entity with</param>
|
||||
/// <param name="id">id of the entity to update</param>
|
||||
/// <returns></returns>
|
||||
public async Task Update(TCreatType entity, int id)
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.PutAsJsonAsync($"api/{ResourceUri}/{id}", entity);
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ using System;
|
||||
|
||||
namespace Novaloop.PaymoApi.Shared
|
||||
{
|
||||
public class BasePaymoModel
|
||||
public class BaseModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public DateTime CreatedOn { get; set; }
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace Novaloop.PaymoApi.Shared
|
||||
{
|
||||
public interface IPaymoApiClient
|
||||
public interface IApiClient
|
||||
{
|
||||
}
|
||||
}
|
||||
41
src/Shared/IBaseApi.cs
Normal file
41
src/Shared/IBaseApi.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Novaloop.PaymoApi.Shared
|
||||
{
|
||||
public interface IBaseApi<TReturnType, TCreatType>
|
||||
{
|
||||
string ResourceUri { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Receive all entities
|
||||
/// </summary>
|
||||
Task<TReturnType> GetAll();
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve an existing entity
|
||||
/// </summary>
|
||||
/// <param name="entityId">id of the entity</param>
|
||||
/// <returns></returns>
|
||||
Task<TReturnType> Get(int entityId);
|
||||
|
||||
/// <summary>
|
||||
/// Create a new entity
|
||||
/// </summary>
|
||||
Task<TReturnType> Create( TCreatType entity);
|
||||
|
||||
/// <summary>
|
||||
/// Delete an entity
|
||||
/// </summary>
|
||||
/// <param name="entityId">id of the entity</param>
|
||||
/// <returns></returns>
|
||||
Task Delete(int entityId);
|
||||
|
||||
/// <summary>
|
||||
/// Update an entity
|
||||
/// </summary>
|
||||
/// <param name="entity">entity information to update the entity with</param>
|
||||
/// <param name="id">id of the entity to update</param>
|
||||
/// <returns></returns>
|
||||
Task Update( TCreatType entity, int id);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Novaloop.PaymoApi.Tasks.Models;
|
||||
|
||||
namespace Novaloop.PaymoApi.Tasks
|
||||
{
|
||||
public interface IPaymoTasksApi
|
||||
{
|
||||
Task<IEnumerable<PaymoTask>> GetTasks();
|
||||
Task<PaymoTask> GetTask(int taskId);
|
||||
Task<PaymoTask> CreateTask(PaymoTask task);
|
||||
Task DeleteTask(int taskId);
|
||||
}
|
||||
}
|
||||
40
src/Tasks/ITasksApi.cs
Normal file
40
src/Tasks/ITasksApi.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Novaloop.PaymoApi.Tasks
|
||||
{
|
||||
public interface ITasksApi
|
||||
{
|
||||
/// <summary>
|
||||
/// Receive all existing tasks
|
||||
/// </summary>
|
||||
Task<IEnumerable<Novaloop.PaymoApi.Tasks.Models.Task>> GetTasks();
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve an existing Task by id
|
||||
/// </summary>
|
||||
/// <param name="taskId">id of the task</param>
|
||||
/// <returns></returns>
|
||||
Task<Novaloop.PaymoApi.Tasks.Models.Task> GetTask(int taskId);
|
||||
|
||||
/// <summary>
|
||||
/// Create a new Task
|
||||
/// </summary>
|
||||
Task<Novaloop.PaymoApi.Tasks.Models.Task> CreateTask(Novaloop.PaymoApi.Tasks.Models.Task task);
|
||||
|
||||
/// <summary>
|
||||
/// Delete a task
|
||||
/// </summary>
|
||||
/// <param name="taskId"></param>
|
||||
/// <returns></returns>
|
||||
Task DeleteTask(int taskId);
|
||||
|
||||
/// <summary>
|
||||
/// Update an existing task
|
||||
/// </summary>
|
||||
/// <param name="task"></param>
|
||||
/// <param name="taskId"></param>
|
||||
/// <returns></returns>
|
||||
Task UpdateTask(Novaloop.PaymoApi.Tasks.Models.Task task, int taskId);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ using Novaloop.PaymoApi.Shared;
|
||||
|
||||
namespace Novaloop.PaymoApi.Tasks.Models
|
||||
{
|
||||
public class PaymoTask : BasePaymoModel
|
||||
public class Task : BaseModel
|
||||
{
|
||||
[JsonProperty("name")]
|
||||
public string Name { get; set; }
|
||||
@@ -3,9 +3,9 @@ using Newtonsoft.Json;
|
||||
|
||||
namespace Novaloop.PaymoApi.Tasks.Models
|
||||
{
|
||||
public class GetTasksResponse
|
||||
public class TasksResponse
|
||||
{
|
||||
[JsonProperty("tasks")]
|
||||
public IEnumerable<PaymoTask> Tasks { get; set; }
|
||||
public IEnumerable<Task> Tasks { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Novaloop.PaymoApi.Extensions;
|
||||
using Novaloop.PaymoApi.Shared;
|
||||
using Novaloop.PaymoApi.Tasks.Models;
|
||||
|
||||
namespace Novaloop.PaymoApi.Tasks
|
||||
{
|
||||
public class PaymoTasksApi : IPaymoTasksApi
|
||||
{
|
||||
private readonly PaymoApiOptions _options;
|
||||
private readonly HttpClient _client;
|
||||
private const string ResourceUri = "tasks";
|
||||
|
||||
|
||||
public PaymoTasksApi(PaymoApiClient paymoApiClient, IOptions<PaymoApiOptions> options)
|
||||
{
|
||||
_options = options.Value;
|
||||
_client = paymoApiClient.Client;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Receive all existing tasks
|
||||
/// </summary>
|
||||
public async Task<IEnumerable<PaymoTask>> GetTasks()
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.GetAsync($"api/{ResourceUri}");
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
return (await response.Content.ReadAsAsync<GetTasksResponse>()).Tasks;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve an existing Task by id
|
||||
/// </summary>
|
||||
/// <param name="taskId">id of the task</param>
|
||||
/// <returns></returns>
|
||||
public async Task<PaymoTask> GetTask(int taskId)
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.GetAsync($"api/{ResourceUri}/{taskId}");
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
return (await response.Content.ReadAsAsync<GetTasksResponse>()).Tasks.Single();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new Task
|
||||
/// </summary>
|
||||
public async Task<PaymoTask> CreateTask(PaymoTask task)
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.PostAsJsonAsync($"api/{ResourceUri}", task);
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
return (await response.Content.ReadAsAsync<GetTasksResponse>()).Tasks.Single();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete a task
|
||||
/// </summary>
|
||||
/// <param name="taskId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task DeleteTask(int taskId)
|
||||
{
|
||||
_client.SetApiKeyHeader(_options.ApiToken);
|
||||
var response = await _client.DeleteAsync($"api/{ResourceUri}/{taskId}");
|
||||
await response.ThrowExceptionWithDetailsIfUnsuccessful();
|
||||
}
|
||||
}
|
||||
}
|
||||
53
src/Tasks/TasksApi.cs
Normal file
53
src/Tasks/TasksApi.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Novaloop.PaymoApi.Shared;
|
||||
using Novaloop.PaymoApi.Tasks.Models;
|
||||
using Task = System.Threading.Tasks.Task;
|
||||
|
||||
namespace Novaloop.PaymoApi.Tasks
|
||||
{
|
||||
public class TasksApi : ITasksApi
|
||||
{
|
||||
private readonly IBaseApi<TasksResponse, Novaloop.PaymoApi.Tasks.Models.Task> _baseApi;
|
||||
|
||||
public TasksApi(IBaseApi<TasksResponse, Novaloop.PaymoApi.Tasks.Models.Task> baseApi)
|
||||
{
|
||||
_baseApi = baseApi;
|
||||
_baseApi.ResourceUri = "tasks";
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IEnumerable<Novaloop.PaymoApi.Tasks.Models.Task>> GetTasks()
|
||||
{
|
||||
return (await _baseApi.GetAll()).Tasks;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<Novaloop.PaymoApi.Tasks.Models.Task> GetTask(int taskId)
|
||||
{
|
||||
return (await _baseApi.Get(taskId)).Tasks.Single();
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<Novaloop.PaymoApi.Tasks.Models.Task> CreateTask(Novaloop.PaymoApi.Tasks.Models.Task task)
|
||||
{
|
||||
return (await _baseApi.Create(task)).Tasks.Single();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task DeleteTask(int taskId)
|
||||
{
|
||||
await _baseApi.Delete(taskId);
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task UpdateTask(Novaloop.PaymoApi.Tasks.Models.Task task, int taskId)
|
||||
{
|
||||
await _baseApi.Update(task, taskId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user