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

View File

@@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace Novaloop.PaymoApi.ClientContacts.Models
{
public class GetClientContactsResponse
{
public IEnumerable<PaymoClientContact> ClientContacts { get; set; }
}
}

View File

@@ -0,0 +1,44 @@
using Newtonsoft.Json;
using Novaloop.PaymoApi.Shared;
namespace Novaloop.PaymoApi.ClientContacts.Models
{
public class PaymoClientContact : BasePaymoModel
{
[JsonProperty("client_id")]
public int ClientId { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("mobile")]
public string Mobile { get; set; }
[JsonProperty("phone")]
public string Phone { get; set; }
[JsonProperty("fax")]
public string Fax { get; set; }
[JsonProperty("skype")]
public string Skype { get; set; }
[JsonProperty("notes")]
public object Notes { get; set; }
[JsonProperty("image")]
public string Image { get; set; }
[JsonProperty("is_main")]
public bool IsMain { get; set; }
[JsonProperty("position")]
public string Position { get; set; }
[JsonProperty("access")]
public bool Access { get; set; }
}
}

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

View 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);
}
}

View File

@@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace Novaloop.PaymoApi.Clients.Models
{
public class GetClientsResponse
{
public IEnumerable<PaymoClient> Clients { get; set; }
}
}

View 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}";
}
}
}

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

View File

@@ -1,6 +1,8 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Novaloop.PaymoApi.ClientContacts;
using Novaloop.PaymoApi.Clients;
using Novaloop.PaymoApi.Shared;
using Novaloop.PaymoApi.Tasks;
@@ -13,7 +15,9 @@ namespace Novaloop.PaymoApi.Extensions
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<IPaymoTasksApiService, PaymoTasksApiService>();
services.AddTransient<IPaymoTasksApi, PaymoTasksApi>();
services.AddTransient<IPaymoClientContactsApi, PaymoClientContactsApi>();
services.AddTransient<IPaymoClientsApi, PaymoClientsApi>();
return services;
}
}

View File

@@ -1,19 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Novaloop.PaymoApi</PackageId>
<title>Access your paymo instance for asp.net core</title>
<PackageTags>api;paymo;asp.net core;</PackageTags>
<Version>0.1.9</Version>
<Authors>Matthias Langhard</Authors>
<Company>Novaloop AG</Company>
<PackageProjectUrl>https://gitlab.com/novaloop-oss/novaloop.paymoapi</PackageProjectUrl>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Novaloop.PaymoApi</PackageId>
<title>Access your paymo instance for asp.net core</title>
<PackageTags>api;paymo;asp.net core;</PackageTags>
<Version>1.0.0</Version>
<Authors>Matthias Langhard</Authors>
<Company>Novaloop AG</Company>
<PackageProjectUrl>https://gitlab.com/novaloop-oss/novaloop.paymoapi</PackageProjectUrl>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,11 @@
using System;
namespace Novaloop.PaymoApi.Shared
{
public class BasePaymoModel
{
public int Id { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
}
}

View File

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

View File

@@ -1,12 +0,0 @@
using System.Threading.Tasks;
using Novaloop.PaymoApi.Tasks.Models;
namespace Novaloop.PaymoApi.Tasks
{
public interface IPaymoTasksApiService
{
Task<GetTasksResponse> GetTasks();
Task<GetTasksResponse> GetTask(int taskId);
Task<CreateTaskResponse> CreateTask(CreateTaskRequest createTask);
}
}

View File

@@ -1,20 +0,0 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Novaloop.PaymoApi.Tasks.Models
{
public class CreateTaskRequest
{
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "description")]
public string Description { get; set; }
[JsonProperty(PropertyName = "tasklist_id")]
public int TasklistId { get; set; }
[JsonProperty(PropertyName = "users")]
public List<int> Users { get; set; }
}
}

View File

@@ -1,7 +0,0 @@
namespace Novaloop.PaymoApi.Tasks.Models
{
public class CreateTaskResponse : PaymoTask
{
}
}

View File

@@ -1,25 +1,53 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Novaloop.PaymoApi.Shared;
namespace Novaloop.PaymoApi.Tasks.Models
{
public class PaymoTask
public class PaymoTask : BasePaymoModel
{
public int Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
[JsonProperty("project_id")]
public int ProjectId { get; set; }
[JsonProperty("tasklist_id")]
public int TasklistId { get; set; }
[JsonProperty("user_id")]
public int UserId { get; set; }
[JsonProperty("complete")]
public bool Complete { get; set; }
[JsonProperty("billable")]
public bool Billable { get; set; }
[JsonProperty("seq")]
public int Seq { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("price_per_hour")]
public object PricePerHour { get; set; }
[JsonProperty("due_date")]
public object DueDate { get; set; }
[JsonProperty("budget_hours")]
public object BudgetHours { get; set; }
[JsonProperty("users")]
public List<int> Users { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public override string ToString()
{
return $"[{Id}] {Name} ({TasklistId})";
}
}
}

View File

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

View File

@@ -1,57 +0,0 @@
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 PaymoTasksApiService : IPaymoTasksApiService
{
private readonly PaymoApiOptions _options;
private readonly HttpClient _client;
public PaymoTasksApiService(PaymoApiClient paymoApiClient, IOptions<PaymoApiOptions> options)
{
_options = options.Value;
_client = paymoApiClient.Client;
}
/// <summary>
/// Receive all existing tasks
/// </summary>
public async Task<GetTasksResponse> GetTasks()
{
_client.SetApiKeyHeader(_options.ApiToken);
var response = await _client.GetAsync($"api/tasks");
await response.ThrowExceptionWithDetailsIfUnsuccessful();
return await response.Content.ReadAsAsync<GetTasksResponse>();
}
/// <summary>
/// Retrieve an existing Task by id
/// </summary>
/// <param name="taskId">id of the task</param>
/// <returns></returns>
public async Task<GetTasksResponse> GetTask(int taskId)
{
_client.SetApiKeyHeader(_options.ApiToken);
var response = await _client.GetAsync($"api/tasks/{taskId}");
await response.ThrowExceptionWithDetailsIfUnsuccessful();
return await response.Content.ReadAsAsync<GetTasksResponse>();
}
/// <summary>
/// Creates a new Task
/// </summary>
public async Task<CreateTaskResponse> CreateTask(CreateTaskRequest createTaskRequest)
{
_client.SetApiKeyHeader(_options.ApiToken);
var response = await _client.PostAsJsonAsync("api/tasks", createTaskRequest);
await response.ThrowExceptionWithDetailsIfUnsuccessful();
return await response.Content.ReadAsAsync<CreateTaskResponse>();
}
}
}