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,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();
}
}
}