feat: adds endpoints 'client contacts' and 'contacts' and integration tests for all existing endpoints.

This commit is contained in:
Matthias Langhard
2021-03-24 08:22:58 +01:00
parent dd92939a6f
commit c5734a065c
28 changed files with 775 additions and 145 deletions

View File

@@ -0,0 +1,72 @@
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();
}
}
}