Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ICorrespondentClient #9

Merged
merged 1 commit into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

using VMelnalksnis.PaperlessDotNet.Correspondents;
using VMelnalksnis.PaperlessDotNet.Documents;
using VMelnalksnis.PaperlessDotNet.Serialization;

Expand Down Expand Up @@ -46,6 +47,12 @@ public static IHttpClientBuilder AddPaperlessDotNet(
return serviceCollection
.AddSingleton<PaperlessJsonSerializerOptions>()
.AddTransient<IPaperlessClient, PaperlessClient>()
.AddTransient<ICorrespondentClient, CorrespondentClient>(provider =>
{
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(PaperlessOptions.Name);
var options = provider.GetRequiredService<PaperlessJsonSerializerOptions>();
return new(httpClient, options);
})
.AddTransient<IDocumentClient, DocumentClient>(provider =>
{
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(PaperlessOptions.Name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2022 Valters Melnalksnis
// Licensed under the Apache License 2.0.
// See LICENSE file in the project root for full license information.

using NodaTime;

namespace VMelnalksnis.PaperlessDotNet.Correspondents;

/// <summary>Someone with whom documents were exchanged with.</summary>
public sealed class Correspondent
{
/// <summary>Gets or sets the id of the correspondent.</summary>
public int Id { get; set; }

/// <summary>Gets or sets the normalized <see cref="Name"/> - lowercased and with whitespace replaced with '-'.</summary>
public string Slug { get; set; } = null!;

/// <summary>Gets or sets the name of the correspondent.</summary>
public string Name { get; set; } = null!;

/// <summary>Gets or sets the pattern by which to match the correspondent to documents.</summary>
public string MatchingPattern { get; set; } = null!;

/// <summary>Gets or sets the id of the matching algorithm used to match the correspondent to documents.</summary>
public int MatchingAlgorithmId { get; set; }

/// <summary>Gets or sets a value indicating whether to ignore case when matching the correspondent to documents.</summary>
public bool CaseInsensitiveMatching { get; set; }

/// <summary>Gets or sets the number of documents with the correspondent.</summary>
public int DocumentCount { get; set; }

/// <summary>Gets or sets the instant when the last document with the correspondent was created.</summary>
public Instant? LastCorrespondence { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2022 Valters Melnalksnis
// Licensed under the Apache License 2.0.
// See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

using VMelnalksnis.PaperlessDotNet.Serialization;

namespace VMelnalksnis.PaperlessDotNet.Correspondents;

/// <inheritdoc />
public sealed class CorrespondentClient : ICorrespondentClient
{
private readonly HttpClient _httpClient;
private readonly PaperlessJsonSerializerContext _context;

/// <summary>Initializes a new instance of the <see cref="CorrespondentClient"/> class.</summary>
/// <param name="httpClient">Http client configured for making requests to the Paperless API.</param>
/// <param name="serializerOptions">Paperless specific instance of <see cref="JsonSerializerOptions"/>.</param>
public CorrespondentClient(HttpClient httpClient, PaperlessJsonSerializerOptions serializerOptions)
{
_httpClient = httpClient;
_context = serializerOptions.Context;
}

/// <inheritdoc />
public IAsyncEnumerable<Correspondent> GetAll(CancellationToken cancellationToken = default)
{
return _httpClient.GetPaginated(
"/api/correspondents/",
_context.PaginatedListCorrespondent,
cancellationToken);
}

/// <inheritdoc />
public IAsyncEnumerable<Correspondent> GetAll(int pageSize, CancellationToken cancellationToken = default)
{
return _httpClient.GetPaginated(
$"/api/correspondents/?page_size={pageSize}",
_context.PaginatedListCorrespondent,
cancellationToken);
}

/// <inheritdoc />
public Task<Correspondent?> Get(int id, CancellationToken cancellationToken = default)
{
return _httpClient.GetFromJsonAsync(
$"/api/correspondents/{id}/",
_context.Correspondent,
cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022 Valters Melnalksnis
// Licensed under the Apache License 2.0.
// See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace VMelnalksnis.PaperlessDotNet.Correspondents;

/// <summary>Paperless API client for working with correspondents.</summary>
public interface ICorrespondentClient
{
/// <summary>Gets all correspondents.</summary>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>A enumerable which will asynchronously iterate over all available pages of correspondents.</returns>
IAsyncEnumerable<Correspondent> GetAll(CancellationToken cancellationToken = default);

/// <summary>Gets all correspondents.</summary>
/// <param name="pageSize">The number of correspondents to get in a single request.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>A enumerable which will asynchronously iterate over all available pages of correspondents.</returns>
IAsyncEnumerable<Correspondent> GetAll(int pageSize, CancellationToken cancellationToken = default);

/// <summary>Gets the correspondent with the specified id.</summary>
/// <param name="id">The id of the correspondent to get.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>The correspondent with the specified id if it exists; otherwise <see langword="null"/>.</returns>
Task<Correspondent?> Get(int id, CancellationToken cancellationToken = default);
}
15 changes: 12 additions & 3 deletions source/VMelnalksnis.PaperlessDotNet/Documents/DocumentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,27 @@ public DocumentClient(HttpClient httpClient, PaperlessJsonSerializerOptions seri
/// <inheritdoc />
public IAsyncEnumerable<Document> GetAll(CancellationToken cancellationToken = default)
{
return _httpClient.GetPaginated("/api/documents/", _context.PaginatedListDocument, cancellationToken);
return _httpClient.GetPaginated(
"/api/documents/",
_context.PaginatedListDocument,
cancellationToken);
}

/// <inheritdoc />
public IAsyncEnumerable<Document> GetAll(int pageSize, CancellationToken cancellationToken = default)
{
return _httpClient.GetPaginated($"/api/documents/?page_size={pageSize}", _context.PaginatedListDocument, cancellationToken);
return _httpClient.GetPaginated(
$"/api/documents/?page_size={pageSize}",
_context.PaginatedListDocument,
cancellationToken);
}

/// <inheritdoc />
public Task<Document?> Get(int id, CancellationToken cancellationToken = default)
{
return _httpClient.GetFromJsonAsync($"/api/documents/{id}/", _context.Document, cancellationToken);
return _httpClient.GetFromJsonAsync(
$"/api/documents/{id}/",
_context.Document,
cancellationToken);
}
}
4 changes: 4 additions & 0 deletions source/VMelnalksnis.PaperlessDotNet/IPaperlessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
// Licensed under the Apache License 2.0.
// See LICENSE file in the project root for full license information.

using VMelnalksnis.PaperlessDotNet.Correspondents;
using VMelnalksnis.PaperlessDotNet.Documents;

namespace VMelnalksnis.PaperlessDotNet;

/// <summary>All available Paperless APIs.</summary>
public interface IPaperlessClient
{
/// <summary>Gets the documents API client.</summary>
ICorrespondentClient Correspondents { get; }

/// <summary>Gets the documents API client.</summary>
IDocumentClient Documents { get; }
}
8 changes: 7 additions & 1 deletion source/VMelnalksnis.PaperlessDotNet/PaperlessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License 2.0.
// See LICENSE file in the project root for full license information.

using VMelnalksnis.PaperlessDotNet.Correspondents;
using VMelnalksnis.PaperlessDotNet.Documents;

namespace VMelnalksnis.PaperlessDotNet;
Expand All @@ -10,12 +11,17 @@ namespace VMelnalksnis.PaperlessDotNet;
public sealed class PaperlessClient : IPaperlessClient
{
/// <summary>Initializes a new instance of the <see cref="PaperlessClient"/> class.</summary>
/// <param name="correspondents">Correspondents API client.</param>
/// <param name="documents">Documents API client.</param>
public PaperlessClient(IDocumentClient documents)
public PaperlessClient(ICorrespondentClient correspondents, IDocumentClient documents)
{
Correspondents = correspondents;
Documents = documents;
}

/// <inheritdoc />
public ICorrespondentClient Correspondents { get; }

/// <inheritdoc />
public IDocumentClient Documents { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

using System.Text.Json.Serialization;

using VMelnalksnis.PaperlessDotNet.Correspondents;
using VMelnalksnis.PaperlessDotNet.Documents;

namespace VMelnalksnis.PaperlessDotNet.Serialization;

/// <inheritdoc />
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(PaginatedList<Correspondent>))]
[JsonSerializable(typeof(Correspondent))]
[JsonSerializable(typeof(PaginatedList<Document>))]
[JsonSerializable(typeof(Document))]
internal partial class PaperlessJsonSerializerContext : JsonSerializerContext
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2022 Valters Melnalksnis
// Licensed under the Apache License 2.0.
// See LICENSE file in the project root for full license information.

using System.Linq;
using System.Threading.Tasks;

using Xunit.Abstractions;

namespace VMelnalksnis.PaperlessDotNet.Tests.Integration.Correspondents;

public sealed class CorrespondentClientTests : IClassFixture<ServiceProviderFixture>
{
private readonly IPaperlessClient _paperlessClient;

public CorrespondentClientTests(ITestOutputHelper testOutputHelper, ServiceProviderFixture serviceProviderFixture)
{
_paperlessClient = serviceProviderFixture.GetPaperlessClient(testOutputHelper);
}

[Fact(Skip = "Requires a running Paperless instance")]
public async Task GetAll_ShouldReturnExpected()
{
var correspondents = await _paperlessClient.Correspondents.GetAll().ToListAsync();

correspondents.Should().HaveCount(37);

var expectedCorrespondent = correspondents.First();

var correspondent = await _paperlessClient.Correspondents.Get(expectedCorrespondent.Id);

correspondent.Should().BeEquivalentTo(expectedCorrespondent);
}

[Fact(Skip = "Requires a running Paperless instance")]
public async Task GetAll_PageSizeShouldNotChangeResult()
{
var correspondents = await _paperlessClient.Correspondents.GetAll().ToListAsync();
var pageSizeCorrespondents = await _paperlessClient.Correspondents.GetAll(1).ToListAsync();

correspondents.Should().BeEquivalentTo(pageSizeCorrespondents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ public DocumentClientTests(ITestOutputHelper testOutputHelper, ServiceProviderFi
_paperlessClient = serviceProviderFixture.GetPaperlessClient(testOutputHelper);
}

[Fact]
public void Pass() => Assert.True(true);

[Fact(Skip = "Requires a running Paperless instance")]
public async Task GetAll_ShouldReturnExpected()
{
Expand All @@ -34,4 +31,13 @@ public async Task GetAll_ShouldReturnExpected()

document.Should().BeEquivalentTo(expectedDocument);
}

[Fact(Skip = "Requires a running Paperless instance")]
public async Task GetAll_PageSizeShouldNotChangeResult()
{
var documents = await _paperlessClient.Documents.GetAll().ToListAsync();
var pageSizeDocuments = await _paperlessClient.Documents.GetAll(1).ToListAsync();

documents.Should().BeEquivalentTo(pageSizeDocuments);
}
}