17 Commits
1.0.0 ... 3.0.0

Author SHA1 Message Date
Matthias Langhard
64eb96d388 chore: bumps version number 2021-12-17 15:28:50 +01:00
Matthias Langhard
de0a0ec296 chore: makes integration tests run serially with a 5s delay to prevent 'too many requests' answer from paymo api 2021-12-17 15:28:16 +01:00
Matthias Langhard
9c7028c01d chore: removing DotSettings 2021-12-17 15:28:16 +01:00
Matthias Langhard
160600c3d8 chore: re-adding 'Paymo' prefix in front of all classes 2021-12-17 15:28:16 +01:00
Matthias Langhard
1f6fc0a769 Merge branch 'set-sast-config-1' into 'main'
Configure SAST in `.gitlab-ci.yml`, creating this file if it does not already exist

See merge request novaloop-oss/novaloop.paymoapi!1
2021-11-03 15:06:36 +00:00
Matthias Langhard
f1e0ab2450 Configure SAST in .gitlab-ci.yml, creating this file if it does not already exist 2021-11-03 15:04:57 +00:00
Matthias Langhard
306056b2f6 chore: bumps version number 2021-06-15 12:05:33 +02:00
Matthias Langhard
14bcf19ba8 chore: implements Debug Logging for request and response including body 2021-06-15 11:58:40 +02:00
Matthias Langhard
51f464bad0 chore: moves to ExpandoObject for updating entities because not all properties need to be part of the payload 2021-06-15 11:48:04 +02:00
Matthias Langhard
e63d357d36 deploy: makes tests only run for tags to prevent api rate-limit 2021-05-24 20:48:52 +02:00
Matthias Langhard
1a4414ad2c fix: fixes missing dependency registration 2021-05-24 20:30:31 +02:00
Matthias Langhard
75288e625a chore: bumps version number 2021-05-23 20:52:51 +02:00
Matthias Langhard
f5e33d3a2f fix: fixes missing dependency registration 2021-05-23 11:39:10 +02:00
Matthias Langhard
c6c4b7ebc8 fix: fixes missing dependency registration 2021-05-22 21:42:27 +02:00
Matthias Langhard
b1d4c35f11 chore: bumps version number 2021-05-22 20:24:44 +02:00
Matthias Langhard
89e7ce8449 chore: refactoring some code to introduce a common base class for all api classes 2021-05-21 22:13:04 +02:00
Matthias Langhard
ac20e2e1d4 chore: adds missing 'JsonProperty' annotations to models 2021-03-24 09:10:50 +01:00
29 changed files with 577 additions and 301 deletions

2
.gitignore vendored
View File

@@ -1,5 +1,6 @@
# Project specific
tests/appsettings.tests.json
Novaloop.PaymoApi.sln.DotSettings.user
# Created by https://www.gitignore.io/api/dotnetcore,jetbrains+all,visualstudiocode
# Edit at https://www.gitignore.io/?templates=dotnetcore,jetbrains+all,visualstudiocode
@@ -117,3 +118,4 @@ tests/bin
tests/obj
src/obj
src/bin

View File

@@ -1,22 +1,32 @@
# You can override the included template(s) by including variable overrides
# SAST customization: https://docs.gitlab.com/ee/user/application_security/sast/#customizing-the-sast-settings
# Secret Detection customization: https://docs.gitlab.com/ee/user/application_security/secret_detection/#customizing-settings
# Dependency Scanning customization: https://docs.gitlab.com/ee/user/application_security/dependency_scanning/#customizing-the-dependency-scanning-settings
# Note that environment variables can be set in several places
# See https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence
stages:
- test
- publish
- test
- publish
running tests for tag:
only:
- "/^\\d*.\\d*.\\d*$/"
tags:
- shared
- shared
image: mcr.microsoft.com/dotnet/core/sdk:3.1
stage: test
script:
- dotnet test ./tests
- dotnet test ./tests
publish to nuget:
tags:
- shared
- shared
only:
- /^\d*.\d*.\d*$/ # gets triggered if the commit tag is in the form n.n.n where n is any number
- "/^\\d*.\\d*.\\d*$/"
image: mcr.microsoft.com/dotnet/core/sdk:3.1
stage: publish
script:
- dotnet pack src -o ./packaged
- dotnet nuget push ./packaged/*.nupkg -k $NUGET_API_KEY -s https://api.nuget.org/v3/index.json
- dotnet pack src -o ./packaged
- dotnet nuget push ./packaged/*.nupkg -k $NUGET_API_KEY -s https://api.nuget.org/v3/index.json
sast:
stage: test
include:
- template: Security/SAST.gitlab-ci.yml

View File

@@ -1,4 +0,0 @@
<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

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Threading.Tasks;
using Novaloop.PaymoApi.ClientContacts.Models;
@@ -6,15 +7,36 @@ namespace Novaloop.PaymoApi.ClientContacts
{
public interface IPaymoClientContactsApi
{
/// <summary>
/// Receive all existing client contacts
/// </summary>
Task<IEnumerable<PaymoClientContact>> GetClientContacts();
Task<PaymoClientContact> GetClientContact(int contactId);
Task<PaymoClientContact> CreateClientContact(PaymoClientContact clientContact);
/// <summary>
/// Deleta a client contact
/// Retrieve an existing client contact by id
/// </summary>
/// <param name="clientContactId">id of the contact</param>
/// <returns></returns>
Task<PaymoClientContact> GetClientContact(int clientContactId);
/// <summary>
/// Create a new client contact
/// </summary>
Task<PaymoClientContact> CreateClientContact(PaymoClientContact paymoClientContact);
/// <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(ExpandoObject clientContact, int clientContactId);
}
}

View File

@@ -3,7 +3,7 @@ using Novaloop.PaymoApi.Shared;
namespace Novaloop.PaymoApi.ClientContacts.Models
{
public class PaymoClientContact : BasePaymoModel
public class PaymoClientContact : PaymoBaseModel
{
[JsonProperty("client_id")]
public int ClientId { get; set; }

View File

@@ -1,9 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Novaloop.PaymoApi.ClientContacts.Models
{
public class GetClientContactsResponse
public class PaymoClientContactsResponse
{
[JsonProperty("clientcontacts")]
public IEnumerable<PaymoClientContact> ClientContacts { get; set; }
}
}

View File

@@ -1,72 +1,50 @@
using System.Collections.Generic;
using System.Dynamic;
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";
private readonly IPaymoBaseApi<PaymoClientContactsResponse, PaymoClientContact> _paymoBaseApi;
public PaymoClientContactsApi(PaymoApiClient paymoApiClient, IOptions<PaymoApiOptions> options)
public PaymoClientContactsApi(IPaymoBaseApi<PaymoClientContactsResponse, PaymoClientContact> paymoBaseApi)
{
_options = options.Value;
_client = paymoApiClient.Client;
_paymoBaseApi = paymoBaseApi;
_paymoBaseApi.ResourceUri = "clientcontacts";
}
/// <summary>
/// Receive all existing client contacts
/// </summary>
/// <inheritdoc />
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;
return (await _paymoBaseApi.GetAll()).ClientContacts;
}
/// <summary>
/// Retrieve an existing client contact by id
/// </summary>
/// <param name="clientContactId">id of the contact</param>
/// <returns></returns>
/// <inheritdoc />
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();
return (await _paymoBaseApi.Get(clientContactId)).ClientContacts.Single();
}
/// <summary>
/// Create a new client contact
/// </summary>
public async Task<PaymoClientContact> CreateClientContact(PaymoClientContact clientContact)
/// <inheritdoc />
public async Task<PaymoClientContact> CreateClientContact(PaymoClientContact paymoClientContact)
{
_client.SetApiKeyHeader(_options.ApiToken);
var response = await _client.PostAsJsonAsync($"api/{ResourceUri}", clientContact);
await response.ThrowExceptionWithDetailsIfUnsuccessful();
return (await response.Content.ReadAsAsync<GetClientContactsResponse>()).ClientContacts.Single();
return (await _paymoBaseApi.Create(paymoClientContact)).ClientContacts.Single();
}
/// <summary>
/// Deleta a client contact
/// </summary>
/// <param name="clientContactId"></param>
/// <returns></returns>
/// <inheritdoc />
public async Task DeleteClientContact(int clientContactId)
{
_client.SetApiKeyHeader(_options.ApiToken);
var response = await _client.DeleteAsync($"api/{ResourceUri}/{clientContactId}");
await response.ThrowExceptionWithDetailsIfUnsuccessful();
await _paymoBaseApi.Delete(clientContactId);
}
/// <inheritdoc />
public async Task UpdateClientContact(ExpandoObject clientContact, int clientContactId)
{
await _paymoBaseApi.Update(clientContact, clientContactId);
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Threading.Tasks;
using Novaloop.PaymoApi.Clients.Models;
@@ -6,9 +7,36 @@ namespace Novaloop.PaymoApi.Clients
{
public interface IPaymoClientsApi
{
/// <summary>
/// Receive all existing clients
/// </summary>
Task<IEnumerable<PaymoClient>> GetClients();
/// <summary>
/// Retrieve an existing client by id
/// </summary>
/// <param name="clientId">id of the contact</param>
/// <returns></returns>
Task<PaymoClient> GetClient(int clientId);
Task<PaymoClient> CreateClient(PaymoClient client);
/// <summary>
/// Create a new client
/// </summary>
Task<PaymoClient> CreateClient(PaymoClient paymoClient);
/// <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(ExpandoObject client, int clientId);
}
}

View File

@@ -3,7 +3,7 @@ using Novaloop.PaymoApi.Shared;
namespace Novaloop.PaymoApi.Clients.Models
{
public class PaymoClient : BasePaymoModel
public class PaymoClient : PaymoBaseModel
{
[JsonProperty("name")]
public string Name { get; set; }
@@ -42,7 +42,7 @@ namespace Novaloop.PaymoApi.Clients.Models
public string FiscalInformation { get; set; }
[JsonProperty("active")]
public bool Active { get; set; }
public bool Active { get; set; } = true;
public override string ToString()
{

View File

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

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.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

@@ -0,0 +1,50 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;
using Novaloop.PaymoApi.Clients.Models;
using Novaloop.PaymoApi.Shared;
namespace Novaloop.PaymoApi.Clients
{
public class PaymoPaymoClientsApi : IPaymoClientsApi
{
private readonly IPaymoBaseApi<PaymoClientsResponse, PaymoClient> _paymoBaseApi;
public PaymoPaymoClientsApi(IPaymoBaseApi<PaymoClientsResponse, PaymoClient> paymoBaseApi)
{
_paymoBaseApi = paymoBaseApi;
_paymoBaseApi.ResourceUri = "clients";
}
/// <inheritdoc />
public async Task<IEnumerable<PaymoClient>> GetClients()
{
return (await _paymoBaseApi.GetAll()).Clients;
}
/// <inheritdoc />
public async Task<PaymoClient> GetClient(int clientId)
{
return (await _paymoBaseApi.Get(clientId)).Clients.Single();
}
/// <inheritdoc />
public async Task<PaymoClient> CreateClient(PaymoClient paymoClient)
{
return (await _paymoBaseApi.Create(paymoClient)).Clients.Single();
}
/// <inheritdoc />
public async Task DeleteClient(int clientId)
{
await _paymoBaseApi.Delete(clientId);
}
/// <inheritdoc />
public async Task UpdateClient(ExpandoObject client, int clientId)
{
await _paymoBaseApi.Update(client, clientId);
}
}
}

View File

@@ -2,9 +2,12 @@ using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Novaloop.PaymoApi.ClientContacts;
using Novaloop.PaymoApi.ClientContacts.Models;
using Novaloop.PaymoApi.Clients;
using Novaloop.PaymoApi.Clients.Models;
using Novaloop.PaymoApi.Shared;
using Novaloop.PaymoApi.Tasks;
using Novaloop.PaymoApi.Tasks.Models;
namespace Novaloop.PaymoApi.Extensions
{
@@ -14,10 +17,24 @@ 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.AddHttpClient<PaymoPaymoIApiClient>(client => { client.BaseAddress = new Uri(resolvedOptions.Value.BaseUrl); })
.AddHttpMessageHandler(s => s.GetService<PaymoLoggingHandler>());
// ClientContacts
services.AddTransient<IPaymoBaseApi<PaymoClientContactsResponse, PaymoClientContact>, PaymoBaseApi<PaymoClientContactsResponse, PaymoClientContact>>();
services.AddTransient<IPaymoClientsApi, PaymoPaymoClientsApi>();
// Tasks
services.AddTransient<IPaymoBaseApi<PaymoTasksResponse, PaymoTask>, PaymoBaseApi<PaymoTasksResponse, PaymoTask>>();
services.AddTransient<IPaymoTasksApi, PaymoTasksApi>();
// Contacts
services.AddTransient<IPaymoBaseApi<PaymoClientsResponse, PaymoClient>, PaymoBaseApi<PaymoClientsResponse, PaymoClient>>();
services.AddTransient<IPaymoClientContactsApi, PaymoClientContactsApi>();
services.AddTransient<IPaymoClientsApi, PaymoClientsApi>();
// Shared
services.AddTransient<PaymoLoggingHandler>();
return services;
}
}

View File

@@ -5,7 +5,7 @@
<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>
<Version>3.0.0</Version>
<Authors>Matthias Langhard</Authors>
<Company>Novaloop AG</Company>
<PackageProjectUrl>https://gitlab.com/novaloop-oss/novaloop.paymoapi</PackageProjectUrl>

View File

@@ -0,0 +1,42 @@
using System.Dynamic;
using System.Threading.Tasks;
namespace Novaloop.PaymoApi.Shared
{
public interface IPaymoBaseApi<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(ExpandoObject entity, int id);
}
}

View File

@@ -0,0 +1,87 @@
using System.Dynamic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Novaloop.PaymoApi.Extensions;
namespace Novaloop.PaymoApi.Shared
{
public class PaymoBaseApi<TReturnType, TCreatType> : IPaymoBaseApi<TReturnType, TCreatType>
{
public string ResourceUri { get; set; }
private readonly PaymoApiOptions _options;
private readonly HttpClient _client;
public PaymoBaseApi(PaymoPaymoIApiClient paymoPaymoIApiClient, IOptions<PaymoApiOptions> options)
{
_options = options.Value;
_client = paymoPaymoIApiClient.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(ExpandoObject entity, int id)
{
_client.SetApiKeyHeader(_options.ApiToken);
var serializerSettings = new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver()};
var body = JsonConvert.SerializeObject(entity, serializerSettings);
var stringContent = new StringContent(body, Encoding.UTF8, "application/json");
var response = await _client.PutAsync($"api/{ResourceUri}/{id}", stringContent);
await response.ThrowExceptionWithDetailsIfUnsuccessful();
}
}
}

View File

@@ -2,7 +2,7 @@ using System;
namespace Novaloop.PaymoApi.Shared
{
public class BasePaymoModel
public class PaymoBaseModel
{
public int Id { get; set; }
public DateTime CreatedOn { get; set; }

View File

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

View File

@@ -0,0 +1,45 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace Novaloop.PaymoApi.Shared
{
public class PaymoLoggingHandler : DelegatingHandler
{
private readonly ILogger<PaymoLoggingHandler> _logger;
public PaymoLoggingHandler(ILogger<PaymoLoggingHandler> logger)
{
_logger = logger;
}
public PaymoLoggingHandler(HttpMessageHandler innerHandler, ILogger<PaymoLoggingHandler> logger)
: base(innerHandler)
{
_logger = logger;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
_logger.LogDebug($"Request:\n{request}");
if (request.Content != null)
{
var body = await request.Content.ReadAsStringAsync();
_logger.LogDebug(body);
}
var response = await base.SendAsync(request, cancellationToken);
_logger.LogDebug($"Response:\n{response}");
if (response.Content != null)
{
var body = await response.Content.ReadAsStringAsync();
_logger.LogDebug(body);
}
return response;
}
}
}

View File

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

View File

@@ -1,14 +1,41 @@
using System.Collections.Generic;
using System.Dynamic;
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);
/// <summary>
/// Receive all existing tasks
/// </summary>
Task<IEnumerable<Models.PaymoTask>> GetTasks();
/// <summary>
/// Retrieve an existing Task by id
/// </summary>
/// <param name="taskId">id of the task</param>
/// <returns></returns>
Task<Models.PaymoTask> GetTask(int taskId);
/// <summary>
/// Create a new Task
/// </summary>
Task<Models.PaymoTask> CreateTask(Models.PaymoTask paymoTask);
/// <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(ExpandoObject task, int taskId);
}
}

View File

@@ -4,7 +4,7 @@ using Novaloop.PaymoApi.Shared;
namespace Novaloop.PaymoApi.Tasks.Models
{
public class PaymoTask : BasePaymoModel
public class PaymoTask : PaymoBaseModel
{
[JsonProperty("name")]
public string Name { get; set; }

View File

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

View File

@@ -1,73 +1,54 @@
using System.Collections.Generic;
using System.Dynamic;
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;
using Task = System.Threading.Tasks.Task;
namespace Novaloop.PaymoApi.Tasks
{
public class PaymoTasksApi : IPaymoTasksApi
{
private readonly PaymoApiOptions _options;
private readonly HttpClient _client;
private const string ResourceUri = "tasks";
private readonly IPaymoBaseApi<PaymoTasksResponse, PaymoTask> _paymoBaseApi;
public PaymoTasksApi(PaymoApiClient paymoApiClient, IOptions<PaymoApiOptions> options)
public PaymoTasksApi(IPaymoBaseApi<PaymoTasksResponse, PaymoTask> paymoBaseApi)
{
_options = options.Value;
_client = paymoApiClient.Client;
_paymoBaseApi = paymoBaseApi;
_paymoBaseApi.ResourceUri = "tasks";
}
/// <summary>
/// Receive all existing tasks
/// </summary>
/// <inheritdoc />
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;
return (await _paymoBaseApi.GetAll()).Tasks;
}
/// <summary>
/// Retrieve an existing Task by id
/// </summary>
/// <param name="taskId">id of the task</param>
/// <returns></returns>
/// <inheritdoc />
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();
return (await _paymoBaseApi.Get(taskId)).Tasks.Single();
}
/// <summary>
/// Creates a new Task
/// </summary>
public async Task<PaymoTask> CreateTask(PaymoTask task)
/// <inheritdoc />
public async Task<PaymoTask> CreateTask(PaymoTask paymoTask)
{
_client.SetApiKeyHeader(_options.ApiToken);
var response = await _client.PostAsJsonAsync($"api/{ResourceUri}", task);
await response.ThrowExceptionWithDetailsIfUnsuccessful();
return (await response.Content.ReadAsAsync<GetTasksResponse>()).Tasks.Single();
return (await _paymoBaseApi.Create(paymoTask)).Tasks.Single();
}
/// <summary>
/// Delete a task
/// </summary>
/// <param name="taskId"></param>
/// <returns></returns>
/// <inheritdoc />
public async Task DeleteTask(int taskId)
{
_client.SetApiKeyHeader(_options.ApiToken);
var response = await _client.DeleteAsync($"api/{ResourceUri}/{taskId}");
await response.ThrowExceptionWithDetailsIfUnsuccessful();
await _paymoBaseApi.Delete(taskId);
}
/// <inheritdoc />
public async Task UpdateTask(ExpandoObject task, int taskId)
{
await _paymoBaseApi.Update(task, taskId);
}
}
}

View File

@@ -1,30 +1,34 @@
using System.Linq;
using System.Threading;
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 PaymoClientContactsApiTests
[Collection("IntegrationTests")]
public class ClientContactsApiTests
{
private readonly PaymoClientContactsApi _paymoClientContactsApi;
private readonly PaymoClientsApi _paymoClientsApi;
private readonly PaymoPaymoClientsApi _paymoPaymoClientsApi;
private readonly PaymoClientContact _testClientContact = new PaymoClientContact
private readonly PaymoClientContact _testPaymoClientContact = new PaymoClientContact
{
Name = "Testclient",
Email = "test@client.de"
};
public PaymoClientContactsApiTests()
public ClientContactsApiTests()
{
_paymoClientContactsApi = new PaymoClientContactsApi(DependencyFactory.GeneratePaymoApiClient(), DependencyFactory.GenerateOptions());
_paymoClientsApi = new PaymoClientsApi(DependencyFactory.GeneratePaymoApiClient(), DependencyFactory.GenerateOptions());
Thread.Sleep(5000);
_paymoClientContactsApi = new PaymoClientContactsApi(new PaymoBaseApi<PaymoClientContactsResponse, PaymoClientContact>(DependencyFactory.GeneratePaymoApiClient(), DependencyFactory.GenerateOptions()));
_paymoPaymoClientsApi = new PaymoPaymoClientsApi(new PaymoBaseApi<PaymoClientsResponse, PaymoClient>(DependencyFactory.GeneratePaymoApiClient(), DependencyFactory.GenerateOptions()));
}
[Fact]
public async void GetClientContacts()
{
@@ -54,16 +58,16 @@ namespace Novaloop.PaymoApi.Tests
public async void CreateClientContact()
{
// Arrange
var existingClientContact = (await _paymoClientsApi.GetClients()).First();
var existingClientContact = (await _paymoPaymoClientsApi.GetClients()).First();
// Act
_testClientContact.ClientId = existingClientContact.Id;
var createdClientContact = await _paymoClientContactsApi.CreateClientContact(_testClientContact);
_testPaymoClientContact.ClientId = existingClientContact.Id;
var createdClientContact = await _paymoClientContactsApi.CreateClientContact(_testPaymoClientContact);
var clientContact = await _paymoClientContactsApi.GetClientContact(createdClientContact.Id);
// Assert
Assert.Equal(_testClientContact.Name, clientContact.Name);
Assert.Equal(_testClientContact.Email, clientContact.Email);
Assert.Equal(_testPaymoClientContact.Name, clientContact.Name);
Assert.Equal(_testPaymoClientContact.Email, clientContact.Email);
// Cleanup
await _paymoClientContactsApi.DeleteClientContact(createdClientContact.Id);
@@ -73,9 +77,9 @@ namespace Novaloop.PaymoApi.Tests
public async void DeleteClientContact()
{
// Arrange
var existingClientContact = (await _paymoClientsApi.GetClients()).First();
_testClientContact.ClientId = existingClientContact.Id;
var createdClientContact = await _paymoClientContactsApi.CreateClientContact(_testClientContact);
var existingClientContact = (await _paymoPaymoClientsApi.GetClients()).First();
_testPaymoClientContact.ClientId = existingClientContact.Id;
var createdClientContact = await _paymoClientContactsApi.CreateClientContact(_testPaymoClientContact);
// Act
await _paymoClientContactsApi.DeleteClientContact(createdClientContact.Id);

105
tests/ClientsApiTests.cs Normal file
View File

@@ -0,0 +1,105 @@
using System.Dynamic;
using System.Linq;
using System.Threading;
using Novaloop.PaymoApi.Clients;
using Novaloop.PaymoApi.Clients.Models;
using Novaloop.PaymoApi.Shared;
using Xunit;
namespace Novaloop.PaymoApi.Tests
{
[Collection("IntegrationTests")]
public class ClientsApiTests
{
private readonly PaymoPaymoClientsApi _paymoPaymoClientsApi;
private readonly PaymoClient _testPaymoClient = new PaymoClient
{
Name = "Testclient",
Email = "test@client.de"
};
public ClientsApiTests()
{
Thread.Sleep(5000);
_paymoPaymoClientsApi =
new PaymoPaymoClientsApi(new PaymoBaseApi<PaymoClientsResponse, PaymoClient>(DependencyFactory.GeneratePaymoApiClient(),
DependencyFactory.GenerateOptions()));
}
[Fact]
public async void GetClients()
{
// Arrange
// Act
var clients = (await _paymoPaymoClientsApi.GetClients()).ToList();
// Assert
Assert.NotEmpty(clients);
}
[Fact]
public async void GetClient()
{
// Arrange
// Act
var clients = (await _paymoPaymoClientsApi.GetClients()).ToList();
var client = await _paymoPaymoClientsApi.GetClient(clients.First().Id);
// Assert
Assert.NotNull(client);
}
[Fact]
public async void CreateClient()
{
// Arrange
// Act
var createdClient = await _paymoPaymoClientsApi.CreateClient(_testPaymoClient);
var client = await _paymoPaymoClientsApi.GetClient(createdClient.Id);
// Assert
Assert.Equal(_testPaymoClient.Name, client.Name);
Assert.Equal(_testPaymoClient.Email, client.Email);
// Cleanup
await _paymoPaymoClientsApi.DeleteClient(createdClient.Id);
}
[Fact]
public async void DeleteClient()
{
// Arrange
var createdClient = await _paymoPaymoClientsApi.CreateClient(_testPaymoClient);
// Act
await _paymoPaymoClientsApi.DeleteClient(createdClient.Id);
var clients = (await _paymoPaymoClientsApi.GetClients()).ToList();
// Assert
Assert.Empty(clients.Where(c => c.Id == createdClient.Id));
}
[Fact]
public async void UpdateClient()
{
// Arrange
var createdClient = await _paymoPaymoClientsApi.CreateClient(_testPaymoClient);
dynamic clientUpdateInfo = new ExpandoObject();
clientUpdateInfo.Name = "Updated";
// Act
await _paymoPaymoClientsApi.UpdateClient(clientUpdateInfo, createdClient.Id);
var updatedClient = await _paymoPaymoClientsApi.GetClient(createdClient.Id);
// Assert
Assert.Equal(clientUpdateInfo.Name, updatedClient.Name);
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Net.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Novaloop.PaymoApi.Extensions;
using Novaloop.PaymoApi.Shared;
@@ -9,9 +10,9 @@ namespace Novaloop.PaymoApi.Tests
{
public static class DependencyFactory
{
public static PaymoApiClient GeneratePaymoApiClient()
public static PaymoPaymoIApiClient GeneratePaymoApiClient()
{
return new PaymoApiClient(new HttpClient {BaseAddress = new Uri("https://app.paymoapp.com/")});
return new PaymoPaymoIApiClient(new HttpClient(new PaymoLoggingHandler(new HttpClientHandler(), new NullLogger<PaymoLoggingHandler>())) {BaseAddress = new Uri("https://app.paymoapp.com/")});
}
public static IOptions<PaymoApiOptions> GenerateOptions()

View File

@@ -1,81 +0,0 @@
using System.Linq;
using Novaloop.PaymoApi.Clients;
using Novaloop.PaymoApi.Clients.Models;
using Xunit;
namespace Novaloop.PaymoApi.Tests
{
public class PaymoClientsApiTests
{
private readonly PaymoClientsApi _paymoClientsApi;
private readonly PaymoClient _testClient = new PaymoClient
{
Name = "Testclient",
Email = "test@client.de"
};
public PaymoClientsApiTests()
{
_paymoClientsApi = new PaymoClientsApi(DependencyFactory.GeneratePaymoApiClient(), DependencyFactory.GenerateOptions());
}
[Fact]
public async void GetClients()
{
// Arrange
// Act
var clients = (await _paymoClientsApi.GetClients()).ToList();
// Assert
Assert.NotEmpty(clients);
}
[Fact]
public async void GetClient()
{
// Arrange
// Act
var clients = (await _paymoClientsApi.GetClients()).ToList();
var client = await _paymoClientsApi.GetClient(clients.First().Id);
// Assert
Assert.NotNull(client);
}
[Fact]
public async void CreateClient()
{
// Arrange
// Act
var createdClient = await _paymoClientsApi.CreateClient(_testClient);
var client = await _paymoClientsApi.GetClient(createdClient.Id);
// Assert
Assert.Equal(_testClient.Name, client.Name);
Assert.Equal(_testClient.Email, client.Email);
// Cleanup
await _paymoClientsApi.DeleteClient(createdClient.Id);
}
[Fact]
public async void DeleteClient()
{
// Arrange
var createdClient = await _paymoClientsApi.CreateClient(_testClient);
// Act
await _paymoClientsApi.DeleteClient(createdClient.Id);
var clients = (await _paymoClientsApi.GetClients()).ToList();
// Assert
Assert.Empty(clients.Where(c => c.Id == createdClient.Id));
}
}
}

View File

@@ -1,4 +1,7 @@
using System.Dynamic;
using System.Linq;
using System.Threading;
using Novaloop.PaymoApi.Shared;
using Novaloop.PaymoApi.Tasks;
using Novaloop.PaymoApi.Tasks.Models;
using Xunit;
@@ -6,17 +9,22 @@ using Xunit.Abstractions;
namespace Novaloop.PaymoApi.Tests
{
public class PaymoTasksApiTests
[Collection("IntegrationTests")]
public class TasksApiTests
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly PaymoTasksApi _paymoTasksApi;
private readonly PaymoTask _testTask;
private readonly PaymoTask _testPaymoTask;
public PaymoTasksApiTests(ITestOutputHelper testOutputHelper)
public TasksApiTests(ITestOutputHelper testOutputHelper)
{
Thread.Sleep(5000);
_testOutputHelper = testOutputHelper;
_paymoTasksApi = new PaymoTasksApi(DependencyFactory.GeneratePaymoApiClient(), DependencyFactory.GenerateOptions());
_testTask = new PaymoTask
_paymoTasksApi =
new PaymoTasksApi(new PaymoBaseApi<PaymoTasksResponse, PaymoTask>(DependencyFactory.GeneratePaymoApiClient(),
DependencyFactory.GenerateOptions()));
_testPaymoTask = new PaymoTask
{
Name = "Testtask",
Description = "Just a little task"
@@ -57,13 +65,13 @@ namespace Novaloop.PaymoApi.Tests
var existingTaskListId = (await _paymoTasksApi.GetTasks()).First().TasklistId;
// Act
_testTask.TasklistId = existingTaskListId;
var createdTask = await _paymoTasksApi.CreateTask(_testTask);
_testPaymoTask.TasklistId = existingTaskListId;
var createdTask = await _paymoTasksApi.CreateTask(_testPaymoTask);
var task = await _paymoTasksApi.GetTask(createdTask.Id);
// Assert
Assert.Equal(_testTask.Name, task.Name);
Assert.Equal(_testTask.Description, task.Description);
Assert.Equal(_testPaymoTask.Name, task.Name);
Assert.Equal(_testPaymoTask.Description, task.Description);
// Cleanup
await _paymoTasksApi.DeleteTask(createdTask.Id);
@@ -74,8 +82,8 @@ namespace Novaloop.PaymoApi.Tests
{
// Arrange
var existingTaskListId = (await _paymoTasksApi.GetTasks()).First().TasklistId;
_testTask.TasklistId = existingTaskListId;
var createdTask = await _paymoTasksApi.CreateTask(_testTask);
_testPaymoTask.TasklistId = existingTaskListId;
var createdTask = await _paymoTasksApi.CreateTask(_testPaymoTask);
// Act
await _paymoTasksApi.DeleteTask(createdTask.Id);
@@ -85,5 +93,25 @@ namespace Novaloop.PaymoApi.Tests
// Assert
Assert.Empty(tasks.Where(c => c.Id == createdTask.Id));
}
[Fact]
public async void UpdateTask()
{
// Arrange
var existingTaskListId = (await _paymoTasksApi.GetTasks()).First().TasklistId;
_testPaymoTask.TasklistId = existingTaskListId;
var createdTask = await _paymoTasksApi.CreateTask(_testPaymoTask);
dynamic taskUpdateInfo = new ExpandoObject();
taskUpdateInfo.Name = "Updated";
taskUpdateInfo.TaskListId = _testPaymoTask.TasklistId;
// Act
await _paymoTasksApi.UpdateTask(taskUpdateInfo, createdTask.Id);
var updatedTask = await _paymoTasksApi.GetTask(createdTask.Id);
// Assert
Assert.Equal(taskUpdateInfo.Name, updatedTask.Name);
}
}
}