diff --git a/source/VMelnalksnis.PaperlessDotNet/DocumentTypes/DocumentType.cs b/source/VMelnalksnis.PaperlessDotNet/DocumentTypes/DocumentType.cs new file mode 100644 index 0000000..f00ae02 --- /dev/null +++ b/source/VMelnalksnis.PaperlessDotNet/DocumentTypes/DocumentType.cs @@ -0,0 +1,34 @@ +// Copyright 2022 Valters Melnalksnis +// Licensed under the Apache License 2.0. +// See LICENSE file in the project root for full license information. + +using NodaTime; + +using VMelnalksnis.PaperlessDotNet.Correspondents; + +namespace VMelnalksnis.PaperlessDotNet.DocumentTypes; + +/// A type of document with whom documents were exchanged with. +public sealed class DocumentType +{ + /// Gets or sets the id of the document type. + public int Id { get; set; } + + /// Gets or sets the normalized - lowercased and with whitespace replaced with '-'. + public string Slug { get; set; } = null!; + + /// Gets or sets the name of the document type. + public string Name { get; set; } = null!; + + /// Gets or sets the pattern by which to match the document type to documents. + public string MatchingPattern { get; set; } = null!; + + /// Gets or sets the id of the matching algorithm used to match the document type to documents. + public MatchingAlgorithm MatchingAlgorithm { get; set; } = null!; + + /// Gets or sets a value indicating whether to ignore case when matching the document type to documents. + public bool IsInsensitive { get; set; } + + /// Gets or sets the number of documents with the document type. + public int DocumentCount { get; set; } +} diff --git a/source/VMelnalksnis.PaperlessDotNet/DocumentTypes/DocumentTypeClient.cs b/source/VMelnalksnis.PaperlessDotNet/DocumentTypes/DocumentTypeClient.cs new file mode 100644 index 0000000..2e11b0b --- /dev/null +++ b/source/VMelnalksnis.PaperlessDotNet/DocumentTypes/DocumentTypeClient.cs @@ -0,0 +1,77 @@ +// Copyright 2022 Valters Melnalksnis +// Licensed under the Apache License 2.0. +// See LICENSE file in the project root for full license information. + +using System; +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.DocumentTypes; + +/// +public sealed class DocumentTypeClient : IDocumentTypeClient +{ + private readonly HttpClient _httpClient; + private readonly JsonSerializerOptions _options; + + /// Initializes a new instance of the class. + /// Http client configured for making requests to the Paperless API. + /// Paperless specific instance of . + public DocumentTypeClient(HttpClient httpClient, PaperlessJsonSerializerOptions serializerOptions) + { + _httpClient = httpClient; + _options = serializerOptions.Options; + } + + /// + public IAsyncEnumerable GetAll(CancellationToken cancellationToken = default) + { + return GetAllCore(Routes.DocumentTypes.Uri, cancellationToken); + } + + /// + public IAsyncEnumerable GetAll(int pageSize, CancellationToken cancellationToken = default) + { + return GetAllCore(Routes.DocumentTypes.PagedUri(pageSize), cancellationToken); + } + + /// + public Task Get(int id, CancellationToken cancellationToken = default) + { + return _httpClient.GetFromJsonAsync( + Routes.DocumentTypes.IdUri(id), + _options.GetTypeInfo(), + cancellationToken); + } + + /// + public Task Create(DocumentTypeCreation documentType) + { + return _httpClient.PostAsJsonAsync( + Routes.DocumentTypes.Uri, + documentType, + _options.GetTypeInfo(), + _options.GetTypeInfo()); + } + + /// + public async Task Delete(int id) + { + using var response = await _httpClient.DeleteAsync(Routes.DocumentTypes.IdUri(id)).ConfigureAwait(false); + await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false); + } + + private IAsyncEnumerable GetAllCore(Uri requestUri, CancellationToken cancellationToken) + { + return _httpClient.GetPaginated( + requestUri, + _options.GetTypeInfo>(), + cancellationToken); + } +} diff --git a/source/VMelnalksnis.PaperlessDotNet/DocumentTypes/DocumentTypeCreation.cs b/source/VMelnalksnis.PaperlessDotNet/DocumentTypes/DocumentTypeCreation.cs new file mode 100644 index 0000000..e251a70 --- /dev/null +++ b/source/VMelnalksnis.PaperlessDotNet/DocumentTypes/DocumentTypeCreation.cs @@ -0,0 +1,33 @@ +// Copyright 2022 Valters Melnalksnis +// Licensed under the Apache License 2.0. +// See LICENSE file in the project root for full license information. + +using VMelnalksnis.PaperlessDotNet.Correspondents; + +namespace VMelnalksnis.PaperlessDotNet.DocumentTypes; + +/// Information needed to create a new . +public sealed class DocumentTypeCreation +{ + /// Initializes a new instance of the class. + /// The name of the document type. + public DocumentTypeCreation(string name) + { + Name = name; + } + + /// + public string? Slug { get; set; } + + /// + public string Name { get; set; } + + /// + public string? Match { get; set; } + + /// + public MatchingAlgorithm? MatchingAlgorithm { get; set; } + + /// + public bool? IsInsensitive { get; set; } +} diff --git a/source/VMelnalksnis.PaperlessDotNet/DocumentTypes/IDocumentTypeClient.cs b/source/VMelnalksnis.PaperlessDotNet/DocumentTypes/IDocumentTypeClient.cs new file mode 100644 index 0000000..e9df543 --- /dev/null +++ b/source/VMelnalksnis.PaperlessDotNet/DocumentTypes/IDocumentTypeClient.cs @@ -0,0 +1,40 @@ +// 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.DocumentTypes; + +/// Paperless API client for working with correspondents. +public interface IDocumentTypeClient +{ + /// Gets all document types. + /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. + /// A enumerable which will asynchronously iterate over all available pages of document types. + IAsyncEnumerable GetAll(CancellationToken cancellationToken = default); + + /// Gets all document types. + /// The number of document types to get in a single request. + /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. + /// A enumerable which will asynchronously iterate over all available pages of document types. + IAsyncEnumerable GetAll(int pageSize, CancellationToken cancellationToken = default); + + /// Gets the document type with the specified id. + /// The id of the document type to get. + /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. + /// The document type with the specified id if it exists; otherwise . + Task Get(int id, CancellationToken cancellationToken = default); + + /// Creates a new document type. + /// The correspondent to create. + /// The created correspondent. + Task Create(DocumentTypeCreation documentType); + + /// Deletes a document type. + /// The id of the document type to delete. + /// A representing the asynchronous operation. + Task Delete(int id); +} diff --git a/source/VMelnalksnis.PaperlessDotNet/Documents/DocumentClient.cs b/source/VMelnalksnis.PaperlessDotNet/Documents/DocumentClient.cs index 575055e..eadcc50 100644 --- a/source/VMelnalksnis.PaperlessDotNet/Documents/DocumentClient.cs +++ b/source/VMelnalksnis.PaperlessDotNet/Documents/DocumentClient.cs @@ -90,6 +90,17 @@ public async IAsyncEnumerable> GetAll(int pageSize, [ } } + /// + /// Gets all documents tagged with a certain tag Id. + /// + /// Id of the tag for which to retrieve all documents. + /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. + /// An enumerable which will asynchronously iterate over all retrieved documents. + public IAsyncEnumerable GetAllByTagId(int tagId, CancellationToken cancellationToken = default) + { + return GetAllCore(Routes.Documents.ByTagIdUri(tagId), cancellationToken); + } + /// public Task Get(int id, CancellationToken cancellationToken = default) { diff --git a/source/VMelnalksnis.PaperlessDotNet/Routes.cs b/source/VMelnalksnis.PaperlessDotNet/Routes.cs index b6ddf45..4ff657a 100644 --- a/source/VMelnalksnis.PaperlessDotNet/Routes.cs +++ b/source/VMelnalksnis.PaperlessDotNet/Routes.cs @@ -13,6 +13,8 @@ internal static class Routes private const string _correspondents = "/api/correspondents/"; private const string _customFields = "/api/custom_fields/"; private const string _documents = "/api/documents/"; + private const string _documenttypes = "/api/document_types/"; + private const string _storagepaths = "/api/storage_paths/"; private const string _tags = "/api/tags/"; private const string _tasks = "/api/tasks/"; @@ -27,6 +29,15 @@ internal static class Correspondents internal static Uri PagedUri(int pageSize) => new($"{_correspondents}?{_pageSize}={pageSize}", Relative); } + internal static class DocumentTypes + { + internal static readonly Uri Uri = new(_documenttypes, Relative); + + internal static Uri IdUri(int id) => new($"{_documenttypes}{id}/", Relative); + + internal static Uri PagedUri(int pageSize) => new($"{_documenttypes}?{_pageSize}={pageSize}", Relative); + } + internal static class CustomFields { internal static readonly Uri Uri = new(_customFields, Relative); @@ -39,6 +50,8 @@ internal static class Documents internal static readonly Uri Uri = new(_documents, Relative); internal static readonly Uri CreateUri = new($"{_documents}post_document/", Relative); + internal static Uri ByTagIdUri(int id) => new($"{_documents}?tags__id__all={id}", Relative); + internal static Uri IdUri(int id) => new($"{_documents}{id}/", Relative); internal static Uri MetadataUri(int id) => new($"{_documents}{id}/metadata/", Relative); @@ -56,6 +69,15 @@ internal static class Documents internal static Uri PagedUri(int pageSize) => new($"{_documents}?{_pageSize}={pageSize}", Relative); } + internal static class StoragePaths + { + internal static readonly Uri Uri = new(_storagepaths, Relative); + + internal static Uri IdUri(int id) => new($"{_storagepaths}{id}/", Relative); + + internal static Uri PagedUri(int pageSize) => new($"{_storagepaths}?{_pageSize}={pageSize}", Relative); + } + internal static class Tags { internal static readonly Uri Uri = new(_tags, Relative); diff --git a/source/VMelnalksnis.PaperlessDotNet/Serialization/PaperlessJsonSerializerContext.cs b/source/VMelnalksnis.PaperlessDotNet/Serialization/PaperlessJsonSerializerContext.cs index de46454..0e85bec 100644 --- a/source/VMelnalksnis.PaperlessDotNet/Serialization/PaperlessJsonSerializerContext.cs +++ b/source/VMelnalksnis.PaperlessDotNet/Serialization/PaperlessJsonSerializerContext.cs @@ -8,6 +8,8 @@ using VMelnalksnis.PaperlessDotNet.Correspondents; using VMelnalksnis.PaperlessDotNet.Documents; +using VMelnalksnis.PaperlessDotNet.DocumentTypes; +using VMelnalksnis.PaperlessDotNet.StoragePaths; using VMelnalksnis.PaperlessDotNet.Tags; using VMelnalksnis.PaperlessDotNet.Tasks; @@ -20,6 +22,8 @@ namespace VMelnalksnis.PaperlessDotNet.Serialization; [JsonSerializable(typeof(CorrespondentCreation))] [JsonSerializable(typeof(PaginatedList))] [JsonSerializable(typeof(Document))] +[JsonSerializable(typeof(DocumentType))] +[JsonSerializable(typeof(StoragePath))] [JsonSerializable(typeof(List))] [JsonSerializable(typeof(CustomFieldCreation))] [JsonSerializable(typeof(List))] diff --git a/source/VMelnalksnis.PaperlessDotNet/StoragePaths/IStoragePathClient.cs b/source/VMelnalksnis.PaperlessDotNet/StoragePaths/IStoragePathClient.cs new file mode 100644 index 0000000..a148f9f --- /dev/null +++ b/source/VMelnalksnis.PaperlessDotNet/StoragePaths/IStoragePathClient.cs @@ -0,0 +1,40 @@ +// 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.StoragePaths; + +/// Paperless API client for working with correspondents. +public interface IStoragePathClient +{ + /// Gets all storage paths. + /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. + /// A enumerable which will asynchronously iterate over all available pages of storage paths. + IAsyncEnumerable GetAll(CancellationToken cancellationToken = default); + + /// Gets all storage paths. + /// The number of storage paths to get in a single request. + /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. + /// A enumerable which will asynchronously iterate over all available pages of storage paths. + IAsyncEnumerable GetAll(int pageSize, CancellationToken cancellationToken = default); + + /// Gets the storage path with the specified id. + /// The id of the storage path to get. + /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. + /// The storage path with the specified id if it exists; otherwise . + Task Get(int id, CancellationToken cancellationToken = default); + + /// Creates a new storage path. + /// The correspondent to create. + /// The created correspondent. + Task Create(StoragePathCreation storagePath); + + /// Deletes a storage path. + /// The id of the storage path to delete. + /// A representing the asynchronous operation. + Task Delete(int id); +} diff --git a/source/VMelnalksnis.PaperlessDotNet/StoragePaths/StoragePath.cs b/source/VMelnalksnis.PaperlessDotNet/StoragePaths/StoragePath.cs new file mode 100644 index 0000000..789ff56 --- /dev/null +++ b/source/VMelnalksnis.PaperlessDotNet/StoragePaths/StoragePath.cs @@ -0,0 +1,37 @@ +// Copyright 2022 Valters Melnalksnis +// Licensed under the Apache License 2.0. +// See LICENSE file in the project root for full license information. + +using NodaTime; + +using VMelnalksnis.PaperlessDotNet.Correspondents; + +namespace VMelnalksnis.PaperlessDotNet.StoragePaths; + +/// A type of document with whom documents were exchanged with. +public sealed class StoragePath +{ + /// Gets or sets the id of the storage path. + public int Id { get; set; } + + /// Gets or sets the normalized - lowercased and with whitespace replaced with '-'. + public string Slug { get; set; } = null!; + + /// Gets or sets the name of the storage path. + public string Name { get; set; } = null!; + + /// Gets or sets the path of the storage path. + public string Path { get; set; } = null!; + + /// Gets or sets the pattern by which to match the storage path to documents. + public string MatchingPattern { get; set; } = null!; + + /// Gets or sets the id of the matching algorithm used to match the storage path to documents. + public MatchingAlgorithm MatchingAlgorithm { get; set; } = null!; + + /// Gets or sets a value indicating whether to ignore case when matching the storage path to documents. + public bool IsInsensitive { get; set; } + + /// Gets or sets the number of documents with the storage path. + public int DocumentCount { get; set; } +} diff --git a/source/VMelnalksnis.PaperlessDotNet/StoragePaths/StoragePathClient.cs b/source/VMelnalksnis.PaperlessDotNet/StoragePaths/StoragePathClient.cs new file mode 100644 index 0000000..12950c7 --- /dev/null +++ b/source/VMelnalksnis.PaperlessDotNet/StoragePaths/StoragePathClient.cs @@ -0,0 +1,78 @@ +// Copyright 2022 Valters Melnalksnis +// Licensed under the Apache License 2.0. +// See LICENSE file in the project root for full license information. + +using System; +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; +using VMelnalksnis.PaperlessDotNet.StoragePaths; + +namespace VMelnalksnis.PaperlessDotNet.StoragePaths; + +/// +public sealed class StoragePathClient : IStoragePathClient +{ + private readonly HttpClient _httpClient; + private readonly JsonSerializerOptions _options; + + /// Initializes a new instance of the class. + /// Http client configured for making requests to the Paperless API. + /// Paperless specific instance of . + public StoragePathClient(HttpClient httpClient, PaperlessJsonSerializerOptions serializerOptions) + { + _httpClient = httpClient; + _options = serializerOptions.Options; + } + + /// + public IAsyncEnumerable GetAll(CancellationToken cancellationToken = default) + { + return GetAllCore(Routes.StoragePaths.Uri, cancellationToken); + } + + /// + public IAsyncEnumerable GetAll(int pageSize, CancellationToken cancellationToken = default) + { + return GetAllCore(Routes.StoragePaths.PagedUri(pageSize), cancellationToken); + } + + /// + public Task Get(int id, CancellationToken cancellationToken = default) + { + return _httpClient.GetFromJsonAsync( + Routes.StoragePaths.IdUri(id), + _options.GetTypeInfo(), + cancellationToken); + } + + /// + public Task Create(StoragePathCreation storagePath) + { + return _httpClient.PostAsJsonAsync( + Routes.StoragePaths.Uri, + storagePath, + _options.GetTypeInfo(), + _options.GetTypeInfo()); + } + + /// + public async Task Delete(int id) + { + using var response = await _httpClient.DeleteAsync(Routes.StoragePaths.IdUri(id)).ConfigureAwait(false); + await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false); + } + + private IAsyncEnumerable GetAllCore(Uri requestUri, CancellationToken cancellationToken) + { + return _httpClient.GetPaginated( + requestUri, + _options.GetTypeInfo>(), + cancellationToken); + } +} diff --git a/source/VMelnalksnis.PaperlessDotNet/StoragePaths/StoragePathCreation.cs b/source/VMelnalksnis.PaperlessDotNet/StoragePaths/StoragePathCreation.cs new file mode 100644 index 0000000..a35ba3a --- /dev/null +++ b/source/VMelnalksnis.PaperlessDotNet/StoragePaths/StoragePathCreation.cs @@ -0,0 +1,38 @@ +// Copyright 2022 Valters Melnalksnis +// Licensed under the Apache License 2.0. +// See LICENSE file in the project root for full license information. + +using VMelnalksnis.PaperlessDotNet.Correspondents; + +namespace VMelnalksnis.PaperlessDotNet.StoragePaths; + +/// Information needed to create a new . +public sealed class StoragePathCreation +{ + /// Initializes a new instance of the class. + /// The name of the document type. + /// The path of the storage path. + public StoragePathCreation(string name, string path) + { + Name = name; + Path = path; + } + + /// + public string? Slug { get; set; } + + /// + public string Name { get; set; } + + /// + public string Path { get; set; } + + /// + public string? Match { get; set; } + + /// + public MatchingAlgorithm? MatchingAlgorithm { get; set; } + + /// + public bool? IsInsensitive { get; set; } +} diff --git a/source/VMelnalksnis.PaperlessDotNet/VMelnalksnis.PaperlessDotNet.csproj b/source/VMelnalksnis.PaperlessDotNet/VMelnalksnis.PaperlessDotNet.csproj index 9160c16..753b322 100644 --- a/source/VMelnalksnis.PaperlessDotNet/VMelnalksnis.PaperlessDotNet.csproj +++ b/source/VMelnalksnis.PaperlessDotNet/VMelnalksnis.PaperlessDotNet.csproj @@ -33,4 +33,8 @@ + + + +