feat: adds endpoints 'client contacts' and 'contacts' and integration tests for all existing endpoints.
This commit is contained in:
14
src/Clients/IPaymoClientsApi.cs
Normal file
14
src/Clients/IPaymoClientsApi.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
9
src/Clients/Models/GetClientsResponse.cs
Normal file
9
src/Clients/Models/GetClientsResponse.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Novaloop.PaymoApi.Clients.Models
|
||||
{
|
||||
public class GetClientsResponse
|
||||
{
|
||||
public IEnumerable<PaymoClient> Clients { get; set; }
|
||||
}
|
||||
}
|
||||
52
src/Clients/Models/PaymoClient.cs
Normal file
52
src/Clients/Models/PaymoClient.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Newtonsoft.Json;
|
||||
using Novaloop.PaymoApi.Shared;
|
||||
|
||||
namespace Novaloop.PaymoApi.Clients.Models
|
||||
{
|
||||
public class PaymoClient : BasePaymoModel
|
||||
{
|
||||
[JsonProperty("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonProperty("address")]
|
||||
public string Address { get; set; }
|
||||
|
||||
[JsonProperty("city")]
|
||||
public string City { get; set; }
|
||||
|
||||
[JsonProperty("state")]
|
||||
public string State { get; set; }
|
||||
|
||||
[JsonProperty("postal_code")]
|
||||
public string PostalCode { get; set; }
|
||||
|
||||
[JsonProperty("country")]
|
||||
public string Country { get; set; }
|
||||
|
||||
[JsonProperty("phone")]
|
||||
public string Phone { get; set; }
|
||||
|
||||
[JsonProperty("fax")]
|
||||
public string Fax { get; set; }
|
||||
|
||||
[JsonProperty("email")]
|
||||
public string Email { get; set; }
|
||||
|
||||
[JsonProperty("website")]
|
||||
public string Website { get; set; }
|
||||
|
||||
[JsonProperty("image")]
|
||||
public string Image { get; set; }
|
||||
|
||||
[JsonProperty("fiscal_information")]
|
||||
public string FiscalInformation { get; set; }
|
||||
|
||||
[JsonProperty("active")]
|
||||
public bool Active { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[{Id}] {Name}";
|
||||
}
|
||||
}
|
||||
}
|
||||
72
src/Clients/PaymoClientsApi.cs
Normal file
72
src/Clients/PaymoClientsApi.cs
Normal 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.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