3 Commits
0.1.9 ... 1.1.0

38 changed files with 990 additions and 184 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,5 @@
# Project specific
tests/appsettings.tests.json
# Created by https://www.gitignore.io/api/dotnetcore,jetbrains+all,visualstudiocode # Created by https://www.gitignore.io/api/dotnetcore,jetbrains+all,visualstudiocode
# Edit at https://www.gitignore.io/?templates=dotnetcore,jetbrains+all,visualstudiocode # Edit at https://www.gitignore.io/?templates=dotnetcore,jetbrains+all,visualstudiocode

View File

@@ -5,7 +5,7 @@ VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0 MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Novaloop.PaymoApi", "src\Novaloop.PaymoApi.csproj", "{A9612B7C-67C1-4B6B-8260-167079A31FAF}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Novaloop.PaymoApi", "src\Novaloop.PaymoApi.csproj", "{A9612B7C-67C1-4B6B-8260-167079A31FAF}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Novaloop.PaymoApi.Tests", "tests\Novaloop.PaymoApi.Tests.csproj", "{202BCB4F-78AF-4E9A-B286-C3147374EB53}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Novaloop.PaymoApi.IntegrationTests", "tests\Novaloop.PaymoApi.IntegrationTests.csproj", "{202BCB4F-78AF-4E9A-B286-C3147374EB53}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=dc84371d_002D5ed8_002D46d3_002Dac3d_002D11c77b97626a/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from &amp;lt;Novaloop.PaymoApi.IntegrationTests&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Project Location="/home/riscie/novaloop/libs/Novaloop.paymoapi/tests" Presentation="&amp;lt;Novaloop.PaymoApi.IntegrationTests&amp;gt;" /&gt;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>

View File

@@ -0,0 +1,49 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Novaloop.PaymoApi.ClientContacts.Models;
using Novaloop.PaymoApi.Shared;
namespace Novaloop.PaymoApi.ClientContacts
{
public class ClientContactsApi : IClientContactsApi
{
private readonly IBaseApi<GetClientContactsResponse, ClientContact> _baseApi;
public ClientContactsApi(IBaseApi<GetClientContactsResponse, ClientContact> baseApi)
{
_baseApi = baseApi;
_baseApi.ResourceUri = "clientcontacts";
}
/// <inheritdoc />
public async Task<IEnumerable<ClientContact>> GetClientContacts()
{
return (await _baseApi.GetAll()).ClientContacts;
}
/// <inheritdoc />
public async Task<ClientContact> GetClientContact(int clientContactId)
{
return (await _baseApi.Get(clientContactId)).ClientContacts.Single();
}
/// <inheritdoc />
public async Task<ClientContact> CreateClientContact(ClientContact clientContact)
{
return (await _baseApi.Create(clientContact)).ClientContacts.Single();
}
/// <inheritdoc />
public async Task DeleteClientContact(int clientContactId)
{
await _baseApi.Delete(clientContactId);
}
/// <inheritdoc />
public async Task UpdateClientContact(ClientContact clientContact, int clientContactId)
{
await _baseApi.Update(clientContact, clientContactId);
}
}
}

View File

@@ -0,0 +1,41 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Novaloop.PaymoApi.ClientContacts.Models;
namespace Novaloop.PaymoApi.ClientContacts
{
public interface IClientContactsApi
{
/// <summary>
/// Receive all existing client contacts
/// </summary>
Task<IEnumerable<ClientContact>> GetClientContacts();
/// <summary>
/// Retrieve an existing client contact by id
/// </summary>
/// <param name="clientContactId">id of the contact</param>
/// <returns></returns>
Task<ClientContact> GetClientContact(int clientContactId);
/// <summary>
/// Create a new client contact
/// </summary>
Task<ClientContact> CreateClientContact(ClientContact clientContact);
/// <summary>
/// Delete a client contact
/// </summary>
/// <param name="clientContactId"></param>
/// <returns></returns>
Task DeleteClientContact(int clientContactId);
/// <summary>
/// Update an existing client contact
/// </summary>
/// <param name="clientContact"></param>
/// <param name="clientContactId"></param>
/// <returns></returns>
Task UpdateClientContact(ClientContact clientContact, int clientContactId);
}
}

View File

@@ -0,0 +1,44 @@
using Newtonsoft.Json;
using Novaloop.PaymoApi.Shared;
namespace Novaloop.PaymoApi.ClientContacts.Models
{
public class ClientContact : BaseModel
{
[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,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Novaloop.PaymoApi.ClientContacts.Models
{
public class GetClientContactsResponse
{
[JsonProperty("clientcontacts")]
public IEnumerable<ClientContact> ClientContacts { get; set; }
}
}

49
src/Clients/ClientsApi.cs Normal file
View File

@@ -0,0 +1,49 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Novaloop.PaymoApi.Clients.Models;
using Novaloop.PaymoApi.Shared;
namespace Novaloop.PaymoApi.Clients
{
public class ClientsApi : IClientsApi
{
private readonly IBaseApi<ClientsResponse, Client> _baseApi;
public ClientsApi(IBaseApi<ClientsResponse, Client> baseApi)
{
_baseApi = baseApi;
_baseApi.ResourceUri = "clients";
}
/// <inheritdoc />
public async Task<IEnumerable<Client>> GetClients()
{
return (await _baseApi.GetAll()).Clients;
}
/// <inheritdoc />
public async Task<Client> GetClient(int clientId)
{
return (await _baseApi.Get(clientId)).Clients.Single();
}
/// <inheritdoc />
public async Task<Client> CreateClient(Client client)
{
return (await _baseApi.Create(client)).Clients.Single();
}
/// <inheritdoc />
public async Task DeleteClient(int clientId)
{
await _baseApi.Delete(clientId);
}
/// <inheritdoc />
public async Task UpdateClient(Client client, int clientId)
{
await _baseApi.Update(client, clientId);
}
}
}

View File

@@ -0,0 +1,41 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Novaloop.PaymoApi.Clients.Models;
namespace Novaloop.PaymoApi.Clients
{
public interface IClientsApi
{
/// <summary>
/// Receive all existing clients
/// </summary>
Task<IEnumerable<Client>> GetClients();
/// <summary>
/// Retrieve an existing client by id
/// </summary>
/// <param name="clientId">id of the contact</param>
/// <returns></returns>
Task<Client> GetClient(int clientId);
/// <summary>
/// Create a new client
/// </summary>
Task<Client> CreateClient(Client client);
/// <summary>
/// Delete a client
/// </summary>
/// <param name="clientId"></param>
/// <returns></returns>
Task DeleteClient(int clientId);
/// <summary>
/// Update an existing client
/// </summary>
/// <param name="client"></param>
/// <param name="clientId"></param>
/// <returns></returns>
Task UpdateClient(Client client, int clientId);
}
}

View File

@@ -0,0 +1,52 @@
using Newtonsoft.Json;
using Novaloop.PaymoApi.Shared;
namespace Novaloop.PaymoApi.Clients.Models
{
public class Client : BaseModel
{
[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; } = true;
public override string ToString()
{
return $"[{Id}] {Name}";
}
}
}

View File

@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Novaloop.PaymoApi.Clients.Models
{
public class ClientsResponse
{
[JsonProperty("clients")]
public IEnumerable<Client> Clients { get; set; }
}
}

View File

@@ -2,9 +2,9 @@ using System;
namespace Novaloop.PaymoApi.Exceptions namespace Novaloop.PaymoApi.Exceptions
{ {
public class PaymoApiException : Exception public class ApiException : Exception
{ {
public PaymoApiException(int statusCode, string message) : base($"[{statusCode}]: {message})") public ApiException(int statusCode, string message) : base($"[{statusCode}]: {message})")
{ {
StatusCode = statusCode; StatusCode = statusCode;
} }

View File

@@ -1,6 +1,6 @@
namespace Novaloop.PaymoApi.Extensions namespace Novaloop.PaymoApi.Extensions
{ {
public class PaymoApiOptions public class ApiOptions
{ {
public string BaseUrl { get; set; } = "https://app.paymoapp.com/"; public string BaseUrl { get; set; } = "https://app.paymoapp.com/";
public string ApiToken { get; set; } public string ApiToken { get; set; }

View File

@@ -10,7 +10,7 @@ namespace Novaloop.PaymoApi.Extensions
{ {
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
throw new PaymoApiException((int) response.StatusCode, await response.Content.ReadAsStringAsync()); throw new ApiException((int) response.StatusCode, await response.Content.ReadAsStringAsync());
} }
} }
} }

View File

@@ -1,6 +1,9 @@
using System; using System;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Novaloop.PaymoApi.ClientContacts;
using Novaloop.PaymoApi.Clients;
using Novaloop.PaymoApi.Clients.Models;
using Novaloop.PaymoApi.Shared; using Novaloop.PaymoApi.Shared;
using Novaloop.PaymoApi.Tasks; using Novaloop.PaymoApi.Tasks;
@@ -8,12 +11,15 @@ namespace Novaloop.PaymoApi.Extensions
{ {
public static class PaymoApiExtensions public static class PaymoApiExtensions
{ {
public static IServiceCollection AddPaymoApi(this IServiceCollection services, Action<PaymoApiOptions> options) public static IServiceCollection AddPaymoApi(this IServiceCollection services, Action<ApiOptions> options)
{ {
services.Configure(options); services.Configure(options);
var resolvedOptions = (IOptions<PaymoApiOptions>) services.BuildServiceProvider().GetService(typeof(IOptions<PaymoApiOptions>)); var resolvedOptions = (IOptions<ApiOptions>) services.BuildServiceProvider().GetService(typeof(IOptions<ApiOptions>));
services.AddHttpClient<PaymoApiClient>(client => { client.BaseAddress = new Uri(resolvedOptions.Value.BaseUrl); }); services.AddHttpClient<ApiClient>(client => { client.BaseAddress = new Uri(resolvedOptions.Value.BaseUrl); });
services.AddTransient<IPaymoTasksApiService, PaymoTasksApiService>(); services.AddTransient<IBaseApi<ClientsResponse, Client>, BaseApi<ClientsResponse, Client>>();
services.AddTransient<ITasksApi, TasksApi>();
services.AddTransient<IClientContactsApi, ClientContactsApi>();
services.AddTransient<IClientsApi, ClientsApi>();
return services; return services;
} }
} }

View File

@@ -5,7 +5,7 @@
<PackageId>Novaloop.PaymoApi</PackageId> <PackageId>Novaloop.PaymoApi</PackageId>
<title>Access your paymo instance for asp.net core</title> <title>Access your paymo instance for asp.net core</title>
<PackageTags>api;paymo;asp.net core;</PackageTags> <PackageTags>api;paymo;asp.net core;</PackageTags>
<Version>0.1.9</Version> <Version>1.0.0</Version>
<Authors>Matthias Langhard</Authors> <Authors>Matthias Langhard</Authors>
<Company>Novaloop AG</Company> <Company>Novaloop AG</Company>
<PackageProjectUrl>https://gitlab.com/novaloop-oss/novaloop.paymoapi</PackageProjectUrl> <PackageProjectUrl>https://gitlab.com/novaloop-oss/novaloop.paymoapi</PackageProjectUrl>

View File

@@ -2,9 +2,9 @@ using System.Net.Http;
namespace Novaloop.PaymoApi.Shared namespace Novaloop.PaymoApi.Shared
{ {
public class PaymoApiClient : IPaymoApiClient public class ApiClient : IApiClient
{ {
public PaymoApiClient(HttpClient client) public ApiClient(HttpClient client)
{ {
Client = client; Client = client;
} }

80
src/Shared/BaseApi.cs Normal file
View 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();
}
}
}

11
src/Shared/BaseModel.cs Normal file
View File

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

View File

@@ -1,6 +1,6 @@
namespace Novaloop.PaymoApi.Shared namespace Novaloop.PaymoApi.Shared
{ {
public interface IPaymoApiClient public interface IApiClient
{ {
} }
} }

41
src/Shared/IBaseApi.cs Normal file
View 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);
}
}

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

40
src/Tasks/ITasksApi.cs Normal file
View File

@@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Novaloop.PaymoApi.Tasks
{
public interface ITasksApi
{
/// <summary>
/// Receive all existing tasks
/// </summary>
Task<IEnumerable<Novaloop.PaymoApi.Tasks.Models.Task>> GetTasks();
/// <summary>
/// Retrieve an existing Task by id
/// </summary>
/// <param name="taskId">id of the task</param>
/// <returns></returns>
Task<Novaloop.PaymoApi.Tasks.Models.Task> GetTask(int taskId);
/// <summary>
/// Create a new Task
/// </summary>
Task<Novaloop.PaymoApi.Tasks.Models.Task> CreateTask(Novaloop.PaymoApi.Tasks.Models.Task task);
/// <summary>
/// Delete a task
/// </summary>
/// <param name="taskId"></param>
/// <returns></returns>
Task DeleteTask(int taskId);
/// <summary>
/// Update an existing task
/// </summary>
/// <param name="task"></param>
/// <param name="taskId"></param>
/// <returns></returns>
Task UpdateTask(Novaloop.PaymoApi.Tasks.Models.Task task, int taskId);
}
}

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,9 +0,0 @@
using System.Collections.Generic;
namespace Novaloop.PaymoApi.Tasks.Models
{
public class GetTasksResponse
{
public IEnumerable<PaymoTask> Tasks { get; set; }
}
}

View File

@@ -1,25 +0,0 @@
using System;
using System.Collections.Generic;
namespace Novaloop.PaymoApi.Tasks.Models
{
public class PaymoTask
{
public int Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public int ProjectId { get; set; }
public int TasklistId { get; set; }
public int UserId { get; set; }
public bool Complete { get; set; }
public bool Billable { get; set; }
public int Seq { get; set; }
public string Description { get; set; }
public object PricePerHour { get; set; }
public object DueDate { get; set; }
public object BudgetHours { get; set; }
public List<int> Users { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
}
}

53
src/Tasks/Models/Task.cs Normal file
View File

@@ -0,0 +1,53 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Novaloop.PaymoApi.Shared;
namespace Novaloop.PaymoApi.Tasks.Models
{
public class Task : BaseModel
{
[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 override string ToString()
{
return $"[{Id}] {Name} ({TasklistId})";
}
}
}

View File

@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Novaloop.PaymoApi.Tasks.Models
{
public class TasksResponse
{
[JsonProperty("tasks")]
public IEnumerable<Task> Tasks { get; set; }
}
}

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

53
src/Tasks/TasksApi.cs Normal file
View File

@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Novaloop.PaymoApi.Shared;
using Novaloop.PaymoApi.Tasks.Models;
using Task = System.Threading.Tasks.Task;
namespace Novaloop.PaymoApi.Tasks
{
public class TasksApi : ITasksApi
{
private readonly IBaseApi<TasksResponse, Novaloop.PaymoApi.Tasks.Models.Task> _baseApi;
public TasksApi(IBaseApi<TasksResponse, Novaloop.PaymoApi.Tasks.Models.Task> baseApi)
{
_baseApi = baseApi;
_baseApi.ResourceUri = "tasks";
}
/// <inheritdoc />
public async Task<IEnumerable<Novaloop.PaymoApi.Tasks.Models.Task>> GetTasks()
{
return (await _baseApi.GetAll()).Tasks;
}
/// <inheritdoc />
public async Task<Novaloop.PaymoApi.Tasks.Models.Task> GetTask(int taskId)
{
return (await _baseApi.Get(taskId)).Tasks.Single();
}
/// <inheritdoc />
public async Task<Novaloop.PaymoApi.Tasks.Models.Task> CreateTask(Novaloop.PaymoApi.Tasks.Models.Task task)
{
return (await _baseApi.Create(task)).Tasks.Single();
}
/// <inheritdoc />
public async Task DeleteTask(int taskId)
{
await _baseApi.Delete(taskId);
}
/// <inheritdoc />
public async Task UpdateTask(Novaloop.PaymoApi.Tasks.Models.Task task, int taskId)
{
await _baseApi.Update(task, taskId);
}
}
}

View File

@@ -0,0 +1,91 @@
using System.Linq;
using Novaloop.PaymoApi.ClientContacts;
using Novaloop.PaymoApi.ClientContacts.Models;
using Novaloop.PaymoApi.Clients;
using Novaloop.PaymoApi.Clients.Models;
using Novaloop.PaymoApi.Shared;
using Xunit;
namespace Novaloop.PaymoApi.Tests
{
public class ClientContactsApiTests
{
private readonly ClientContactsApi _clientContactsApi;
private readonly ClientsApi _clientsApi;
private readonly ClientContact _testClientContact = new ClientContact
{
Name = "Testclient",
Email = "test@client.de"
};
public ClientContactsApiTests()
{
_clientContactsApi = new ClientContactsApi(new BaseApi<GetClientContactsResponse, ClientContact>(DependencyFactory.GeneratePaymoApiClient(), DependencyFactory.GenerateOptions()));
_clientsApi = new ClientsApi(new BaseApi<ClientsResponse, Client>(DependencyFactory.GeneratePaymoApiClient(), DependencyFactory.GenerateOptions()));
}
[Fact]
public async void GetClientContacts()
{
// Arrange
// Act
var clientContacts = (await _clientContactsApi.GetClientContacts()).ToList();
// Assert
Assert.NotEmpty(clientContacts);
}
[Fact]
public async void GetClientContact()
{
// Arrange
// Act
var clientContacts = (await _clientContactsApi.GetClientContacts()).ToList();
var clientContact = await _clientContactsApi.GetClientContact(clientContacts.First().Id);
// Assert
Assert.NotNull(clientContact);
}
[Fact]
public async void CreateClientContact()
{
// Arrange
var existingClientContact = (await _clientsApi.GetClients()).First();
// Act
_testClientContact.ClientId = existingClientContact.Id;
var createdClientContact = await _clientContactsApi.CreateClientContact(_testClientContact);
var clientContact = await _clientContactsApi.GetClientContact(createdClientContact.Id);
// Assert
Assert.Equal(_testClientContact.Name, clientContact.Name);
Assert.Equal(_testClientContact.Email, clientContact.Email);
// Cleanup
await _clientContactsApi.DeleteClientContact(createdClientContact.Id);
}
[Fact]
public async void DeleteClientContact()
{
// Arrange
var existingClientContact = (await _clientsApi.GetClients()).First();
_testClientContact.ClientId = existingClientContact.Id;
var createdClientContact = await _clientContactsApi.CreateClientContact(_testClientContact);
// Act
await _clientContactsApi.DeleteClientContact(createdClientContact.Id);
var clientContacts = (await _clientContactsApi.GetClientContacts()).ToList();
// Assert
Assert.Empty(clientContacts.Where(c => c.Id == createdClientContact.Id));
}
}
}

98
tests/ClientsApiTests.cs Normal file
View File

@@ -0,0 +1,98 @@
using System.Linq;
using Novaloop.PaymoApi.Clients;
using Novaloop.PaymoApi.Clients.Models;
using Novaloop.PaymoApi.Shared;
using Xunit;
namespace Novaloop.PaymoApi.Tests
{
public class ClientsApiTests
{
private readonly ClientsApi _clientsApi;
private readonly Client _testClient = new Client
{
Name = "Testclient",
Email = "test@client.de"
};
public ClientsApiTests()
{
_clientsApi = new ClientsApi(new BaseApi<ClientsResponse, Client>(DependencyFactory.GeneratePaymoApiClient(), DependencyFactory.GenerateOptions()));
}
[Fact]
public async void GetClients()
{
// Arrange
// Act
var clients = (await _clientsApi.GetClients()).ToList();
// Assert
Assert.NotEmpty(clients);
}
[Fact]
public async void GetClient()
{
// Arrange
// Act
var clients = (await _clientsApi.GetClients()).ToList();
var client = await _clientsApi.GetClient(clients.First().Id);
// Assert
Assert.NotNull(client);
}
[Fact]
public async void CreateClient()
{
// Arrange
// Act
var createdClient = await _clientsApi.CreateClient(_testClient);
var client = await _clientsApi.GetClient(createdClient.Id);
// Assert
Assert.Equal(_testClient.Name, client.Name);
Assert.Equal(_testClient.Email, client.Email);
// Cleanup
await _clientsApi.DeleteClient(createdClient.Id);
}
[Fact]
public async void DeleteClient()
{
// Arrange
var createdClient = await _clientsApi.CreateClient(_testClient);
// Act
await _clientsApi.DeleteClient(createdClient.Id);
var clients = (await _clientsApi.GetClients()).ToList();
// Assert
Assert.Empty(clients.Where(c => c.Id == createdClient.Id));
}
[Fact]
public async void UpdateClient()
{
// Arrange
var createdClient = await _clientsApi.CreateClient(_testClient);
var clientUpdateInfo = new Client {Name = "Updated"};
// Act
await _clientsApi.UpdateClient(clientUpdateInfo, createdClient.Id);
var updatedClient = await _clientsApi.GetClient(createdClient.Id);
// Assert
Assert.Equal(clientUpdateInfo.Name, updatedClient.Name);
}
}
}

View File

@@ -0,0 +1,34 @@
using System;
using System.Net.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Novaloop.PaymoApi.Extensions;
using Novaloop.PaymoApi.Shared;
namespace Novaloop.PaymoApi.Tests
{
public static class DependencyFactory
{
public static ApiClient GeneratePaymoApiClient()
{
return new ApiClient(new HttpClient {BaseAddress = new Uri("https://app.paymoapp.com/")});
}
public static IOptions<ApiOptions> GenerateOptions()
{
var paymoToken = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.tests.json", true, true)
.AddEnvironmentVariables()
.Build()
.GetSection("PaymoApiIntegrationTests:PaymoTestProjectToken").Value;
if (string.IsNullOrWhiteSpace(paymoToken))
{
throw new ArgumentException("Please set the environment variable 'PAYMOAPIINTEGRATIONTESTS__PAYMOTESTPROJECTTOKEN' with the token of your paymo test-project. Look inside Bitwarden if you work @ Novaloop.");
}
return Options.Create(new ApiOptions {ApiToken = paymoToken});
}
}
}

View File

@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
<RootNamespace>Novaloop.PaymoApi.Tests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\Novaloop.PaymoApi.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.tests.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -1,15 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.1"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
</Project>

109
tests/TasksApiTests.cs Normal file
View File

@@ -0,0 +1,109 @@
using System.Linq;
using Novaloop.PaymoApi.Shared;
using Novaloop.PaymoApi.Tasks;
using Novaloop.PaymoApi.Tasks.Models;
using Xunit;
using Xunit.Abstractions;
namespace Novaloop.PaymoApi.Tests
{
public class TasksApiTests
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly TasksApi _tasksApi;
private readonly Task _testTask;
public TasksApiTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
_tasksApi = new TasksApi(new BaseApi<TasksResponse, Task>(DependencyFactory.GeneratePaymoApiClient(), DependencyFactory.GenerateOptions()));
_testTask = new Task
{
Name = "Testtask",
Description = "Just a little task"
};
}
[Fact]
public async void GetTasks()
{
// Arrange
// Act
var tasks = (await _tasksApi.GetTasks()).ToList();
// Assert
Assert.NotEmpty(tasks);
_testOutputHelper.WriteLine(string.Join(",", tasks));
}
[Fact]
public async void GetTask()
{
// Arrange
// Act
var tasks = (await _tasksApi.GetTasks()).ToList();
var task = await _tasksApi.GetTask(tasks.First().Id);
// Assert
Assert.NotNull(task);
}
[Fact]
public async void CreateTask()
{
// Arrange
var existingTaskListId = (await _tasksApi.GetTasks()).First().TasklistId;
// Act
_testTask.TasklistId = existingTaskListId;
var createdTask = await _tasksApi.CreateTask(_testTask);
var task = await _tasksApi.GetTask(createdTask.Id);
// Assert
Assert.Equal(_testTask.Name, task.Name);
Assert.Equal(_testTask.Description, task.Description);
// Cleanup
await _tasksApi.DeleteTask(createdTask.Id);
}
[Fact]
public async void DeleteTask()
{
// Arrange
var existingTaskListId = (await _tasksApi.GetTasks()).First().TasklistId;
_testTask.TasklistId = existingTaskListId;
var createdTask = await _tasksApi.CreateTask(_testTask);
// Act
await _tasksApi.DeleteTask(createdTask.Id);
var tasks = (await _tasksApi.GetTasks()).ToList();
// Assert
Assert.Empty(tasks.Where(c => c.Id == createdTask.Id));
}
[Fact]
public async void UpdateTask()
{
// Arrange
var existingTaskListId = (await _tasksApi.GetTasks()).First().TasklistId;
_testTask.TasklistId = existingTaskListId;
var createdTask = await _tasksApi.CreateTask(_testTask);
var taskUpdateInfo = new Task {Name = "Updated", TasklistId = _testTask.TasklistId};
// Act
await _tasksApi.UpdateTask(taskUpdateInfo, createdTask.Id);
var updatedTask = await _tasksApi.GetTask(createdTask.Id);
// Assert
Assert.Equal(taskUpdateInfo.Name, updatedTask.Name);
}
}
}

View File

@@ -1,13 +0,0 @@
using Xunit;
namespace Novaloop.PaymoApi.Tests
{
public class UnitTest1
{
[Fact]
public void Test1()
{
Assert.True(true);
}
}
}