Skip to content

Commit

Permalink
Remove non existing include parameters from InvoiceClient (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viincenttt authored Sep 26, 2023
1 parent b1a98be commit 0fd5700
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/Mollie.Api/Client/Abstract/IInvoicesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

namespace Mollie.Api.Client.Abstract {
public interface IInvoicesClient : IDisposable {
Task<InvoiceResponse> GetInvoiceAsync(string invoiceId, bool includeLines = false, bool includeSettlements = false);
Task<InvoiceResponse> GetInvoiceAsync(string invoiceId);
Task<InvoiceResponse> GetInvoiceAsync(UrlObjectLink<InvoiceResponse> url);
Task<ListResponse<InvoiceResponse>> GetInvoiceListAsync(string reference = null, int? year = null,
string from = null, int? limit = null, bool includeLines = false, bool includeSettlements = false);
Task<ListResponse<InvoiceResponse>> GetInvoiceListAsync(
string reference = null, int? year = null, string from = null, int? limit = null);
Task<ListResponse<InvoiceResponse>> GetInvoiceListAsync(UrlObjectLink<ListResponse<InvoiceResponse>> url);
}
}
33 changes: 11 additions & 22 deletions src/Mollie.Api/Client/InvoicesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,33 @@ public class InvoicesClient : OauthBaseMollieClient, IInvoicesClient {
public InvoicesClient(string oauthAccessToken, HttpClient httpClient = null) : base(oauthAccessToken, httpClient) {
}

public async Task<InvoiceResponse> GetInvoiceAsync(string invoiceId, bool includeLines = false, bool includeSettlements = false) {
public async Task<InvoiceResponse> GetInvoiceAsync(string invoiceId) {
this.ValidateRequiredUrlParameter(nameof(invoiceId), invoiceId);
var includes = this.BuildIncludeParameter(includeLines, includeSettlements);
return await this.GetAsync<InvoiceResponse>($"invoices/{invoiceId}{includes.ToQueryString()}").ConfigureAwait(false);
return await this.GetAsync<InvoiceResponse>($"invoices/{invoiceId}").ConfigureAwait(false);
}

public async Task<InvoiceResponse> GetInvoiceAsync(UrlObjectLink<InvoiceResponse> url) {
return await this.GetAsync(url).ConfigureAwait(false);
}

public async Task<ListResponse<InvoiceResponse>> GetInvoiceListAsync(string reference = null, int? year = null, string from = null, int? limit = null, bool includeLines = false, bool includeSettlements = false) {
var parameters = this.BuildIncludeParameter(includeLines, includeSettlements);
public async Task<ListResponse<InvoiceResponse>> GetInvoiceListAsync(
string reference = null, int? year = null, string from = null, int? limit = null) {
var parameters = new Dictionary<string, string>();
parameters.AddValueIfNotNullOrEmpty(nameof(reference), reference);
parameters.AddValueIfNotNullOrEmpty(nameof(year), Convert.ToString(year));

return await this.GetListAsync<ListResponse<InvoiceResponse>>($"invoices", from, limit, parameters).ConfigureAwait(false);
return await this.GetListAsync<ListResponse<InvoiceResponse>>($"invoices", from, limit, parameters)
.ConfigureAwait(false);
}

public async Task<ListResponse<InvoiceResponse>> GetInvoiceListAsync(UrlObjectLink<ListResponse<InvoiceResponse>> url) {
return await this.GetAsync(url).ConfigureAwait(false);
}

private Dictionary<string, string> BuildIncludeParameter(bool includeLines = false, bool includeSettlements = false) {
private Dictionary<string, string> BuildQueryParameters(string profileId, bool testmode) {
var result = new Dictionary<string, string>();

var includeList = new List<string>();
if (includeLines) {
includeList.Add("lines");
}

if (includeSettlements) {
includeList.Add("settlements");
}

if (includeList.Any()) {
result.Add("include", string.Join(",", includeList));
}

result.AddValueIfNotNullOrEmpty("profileId", profileId);
result.AddValueIfTrue("testmode", testmode);
return result;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Mollie.Api/Mollie.Api.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>3.3.0.0</Version>
<Version>3.4.0.0</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Authors>Vincent Kok</Authors>
<Description>This is a wrapper for the Mollie REST webservice. All payment methods and webservice calls are supported.</Description>
<Company>Vincent Kok</Company>
<Product>Mollie Payment API</Product>
<PackageProjectUrl>https://github.com/Viincenttt/MollieApi</PackageProjectUrl>
<PackageTags>Mollie</PackageTags>
<AssemblyVersion>3.3.0.0</AssemblyVersion>
<FileVersion>3.3.0.0</FileVersion>
<PackageVersion>3.3.0.0</PackageVersion>
<AssemblyVersion>3.4.0.0</AssemblyVersion>
<FileVersion>3.4.0.0</FileVersion>
<PackageVersion>3.4.0.0</PackageVersion>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand Down
175 changes: 175 additions & 0 deletions tests/Mollie.Tests.Unit/Client/InvoiceClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using Mollie.Api.Client;
using RichardSzalay.MockHttp;
using Xunit;

namespace Mollie.Tests.Unit.Client;

public class InvoiceClientTests : BaseClientTests
{
[Fact]
public async Task GetInvoiceAsync_DefaultBehaviour_ResponseIsParsed()
{
// Given
const string invoiceId = "inv_xBEbP9rvAq";
var mockHttp = new MockHttpMessageHandler();
mockHttp.When($"{BaseMollieClient.ApiEndPoint}invoices/{invoiceId}")
.Respond("application/json", defaultInvoice);
HttpClient httpClient = mockHttp.ToHttpClient();
using InvoicesClient invoicesClient = new InvoicesClient("access_abcde", httpClient);

// When
var result = await invoicesClient.GetInvoiceAsync(invoiceId);

// Then
mockHttp.VerifyNoOutstandingExpectation();
result.Should().NotBeNull();
result.Resource.Should().Be("invoice");
result.Id.Should().Be(invoiceId);
result.Reference.Should().Be("2016.10000");
result.VatNumber.Should().Be("NL001234567B01");
result.Status.Should().Be("open");
}

[Theory]
[InlineData(null, null, null, null, "")]
[InlineData("my-reference", null, null, null, "?reference=my-reference")]
[InlineData("my-reference", 2023, null, null, "?reference=my-reference&year=2023")]
[InlineData("my-reference", 2023, "abcde", null, "?reference=my-reference&year=2023&from=abcde")]
[InlineData("my-reference", 2023, "abcde", 10, "?reference=my-reference&year=2023&from=abcde&limit=10")]
public async Task GetInvoiceListAsync_WithVariousParameters_QueryStringMatchesExpectedValue(
string reference, int? year, string from, int? limit, string expectedQueryString) {
// Given: We retrieve a list of customers
var mockHttp = new MockHttpMessageHandler();
mockHttp.When($"{BaseMollieClient.ApiEndPoint}invoices{expectedQueryString}")
.Respond("application/json", defaultInvoiceList);
HttpClient httpClient = mockHttp.ToHttpClient();
using InvoicesClient invoicesClient = new InvoicesClient("access_abcde", httpClient);

// When: We send the request
var result = await invoicesClient.GetInvoiceListAsync(
reference, year, from, limit);

// Then
mockHttp.VerifyNoOutstandingExpectation();
result.Should().NotBeNull();
}

private const string defaultInvoice = @"{
""resource"": ""invoice"",
""id"": ""inv_xBEbP9rvAq"",
""reference"": ""2016.10000"",
""vatNumber"": ""NL001234567B01"",
""status"": ""open"",
""issuedAt"": ""2016-08-31"",
""dueAt"": ""2016-09-14"",
""netAmount"": {
""value"": ""45.00"",
""currency"": ""EUR""
},
""vatAmount"": {
""value"": ""9.45"",
""currency"": ""EUR""
},
""grossAmount"": {
""value"": ""54.45"",
""currency"": ""EUR""
},
""lines"":[
{
""period"": ""2016-09"",
""description"": ""iDEAL transactiekosten"",
""count"": 100,
""vatPercentage"": 21,
""amount"": {
""value"": ""45.00"",
""currency"": ""EUR""
}
}
],
""_links"": {
""self"": {
""href"": ""https://api.mollie.com/v2/invoices/inv_xBEbP9rvAq"",
""type"": ""application/hal+json""
},
""pdf"": {
""href"": ""https://www.mollie.com/merchant/download/invoice/xBEbP9rvAq/2ab44d60b35b1d06090bba955fa2c602"",
""type"": ""application/pdf"",
""expiresAt"": ""2018-11-09T14:10:36+00:00""
}
}
}";

private const string defaultInvoiceList = @"{
""count"": 5,
""_embedded"": {
""invoices"": [
{
""resource"": ""invoice"",
""id"": ""inv_xBEbP9rvAq"",
""reference"": ""2016.10000"",
""vatNumber"": ""NL001234567B01"",
""status"": ""open"",
""issuedAt"": ""2016-08-31"",
""dueAt"": ""2016-09-14"",
""netAmount"": {
""value"": ""45.00"",
""currency"": ""EUR""
},
""vatAmount"": {
""value"": ""9.45"",
""currency"": ""EUR""
},
""grossAmount"": {
""value"": ""54.45"",
""currency"": ""EUR""
},
""lines"":[
{
""period"": ""2016-09"",
""description"": ""iDEAL transactiekosten"",
""count"": 100,
""vatPercentage"": 21,
""amount"": {
""value"": ""45.00"",
""currency"": ""EUR""
}
}
],
""_links"": {
""self"": {
""href"": ""https://api.mollie.com/v2/invoices/inv_xBEbP9rvAq"",
""type"": ""application/hal+json""
},
""pdf"": {
""href"": ""https://www.mollie.com/merchant/download/invoice/xBEbP9rvAq/2ab44d60b35955fa2c602"",
""type"": ""application/pdf"",
""expiresAt"": ""2018-11-09T14:10:36+00:00""
}
}
},
{ },
{ },
{ },
{ }
]
},
""_links"": {
""self"": {
""href"": ""https://api.mollie.nl/v2/invoices?limit=5"",
""type"": ""application/hal+json""
},
""previous"": null,
""next"": {
""href"": ""https://api.mollie.nl/v2/invoices?from=inv_xBEbP9rvAq&limit=5"",
""type"": ""application/hal+json""
},
""documentation"": {
""href"": ""https://docs.mollie.com/reference/v2/invoices-api/list-invoices"",
""type"": ""text/html""
}
}
}";
}

0 comments on commit 0fd5700

Please sign in to comment.