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