diff --git a/source/VMelnalksnis.PaperlessDotNet/Correspondents/CorrespondentClient.cs b/source/VMelnalksnis.PaperlessDotNet/Correspondents/CorrespondentClient.cs
index 1550b91..00932bc 100644
--- a/source/VMelnalksnis.PaperlessDotNet/Correspondents/CorrespondentClient.cs
+++ b/source/VMelnalksnis.PaperlessDotNet/Correspondents/CorrespondentClient.cs
@@ -2,6 +2,7 @@
// 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;
@@ -31,26 +32,20 @@ public CorrespondentClient(HttpClient httpClient, PaperlessJsonSerializerOptions
///
public IAsyncEnumerable GetAll(CancellationToken cancellationToken = default)
{
- return _httpClient.GetPaginated(
- "/api/correspondents/",
- _options.GetTypeInfo>(),
- cancellationToken);
+ return GetAllCore(Routes.Correspondents.Uri, cancellationToken);
}
///
public IAsyncEnumerable GetAll(int pageSize, CancellationToken cancellationToken = default)
{
- return _httpClient.GetPaginated(
- $"/api/correspondents/?page_size={pageSize}",
- _options.GetTypeInfo>(),
- cancellationToken);
+ return GetAllCore(Routes.Correspondents.PagedUri(pageSize), cancellationToken);
}
///
public Task Get(int id, CancellationToken cancellationToken = default)
{
return _httpClient.GetFromJsonAsync(
- $"/api/correspondents/{id}/",
+ Routes.Correspondents.IdUri(id),
_options.GetTypeInfo(),
cancellationToken);
}
@@ -59,7 +54,7 @@ public IAsyncEnumerable GetAll(int pageSize, CancellationToken ca
public Task Create(CorrespondentCreation correspondent)
{
return _httpClient.PostAsJsonAsync(
- "/api/correspondents/",
+ Routes.Correspondents.Uri,
correspondent,
_options.GetTypeInfo(),
_options.GetTypeInfo());
@@ -68,7 +63,15 @@ public Task Create(CorrespondentCreation correspondent)
///
public async Task Delete(int id)
{
- using var response = await _httpClient.DeleteAsync($"/api/correspondents/{id}/").ConfigureAwait(false);
+ using var response = await _httpClient.DeleteAsync(Routes.Correspondents.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/Documents/DocumentClient.cs b/source/VMelnalksnis.PaperlessDotNet/Documents/DocumentClient.cs
index 9a51588..1d345d2 100644
--- a/source/VMelnalksnis.PaperlessDotNet/Documents/DocumentClient.cs
+++ b/source/VMelnalksnis.PaperlessDotNet/Documents/DocumentClient.cs
@@ -44,7 +44,7 @@ public DocumentClient(HttpClient httpClient, PaperlessJsonSerializerOptions seri
///
public IAsyncEnumerable GetAll(CancellationToken cancellationToken = default)
{
- return GetAllCore("/api/documents/", cancellationToken);
+ return GetAllCore(Routes.Documents.Uri, cancellationToken);
}
///
@@ -57,7 +57,7 @@ await foreach (var unused in GetCustomFields(cancellationToken).ConfigureAwait(f
}
}
- var documents = GetAllCore>("/api/documents/", cancellationToken);
+ var documents = GetAllCore>(Routes.Documents.Uri, cancellationToken);
await foreach (var document in documents.ConfigureAwait(false))
{
yield return document;
@@ -67,7 +67,7 @@ await foreach (var document in documents.ConfigureAwait(false))
///
public IAsyncEnumerable GetAll(int pageSize, CancellationToken cancellationToken = default)
{
- return GetAllCore($"/api/documents/?page_size={pageSize}", cancellationToken);
+ return GetAllCore(Routes.Documents.PagedUri(pageSize), cancellationToken);
}
///
@@ -80,7 +80,7 @@ await foreach (var unused in GetCustomFields(cancellationToken).ConfigureAwait(f
}
}
- var documents = GetAllCore>($"/api/documents/?page_size={pageSize}", cancellationToken);
+ var documents = GetAllCore>(Routes.Documents.PagedUri(pageSize), cancellationToken);
await foreach (var document in documents.ConfigureAwait(false))
{
yield return document;
@@ -147,7 +147,7 @@ public async Task Create(DocumentCreation document)
content.Add(new StringContent(archiveSerialNumber.ToString()), "archive_serial_number");
}
- using var response = await _httpClient.PostAsync("/api/documents/post_document/", content).ConfigureAwait(false);
+ using var response = await _httpClient.PostAsync(Routes.Documents.CreateUri, content).ConfigureAwait(false);
await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);
// Until v1.9.2 paperless did not return the document import task id,
@@ -203,20 +203,20 @@ await foreach (var unused in GetCustomFields().ConfigureAwait(false))
///
public IAsyncEnumerable GetCustomFields(CancellationToken cancellationToken = default)
{
- return GetCustomFieldsCore($"/api/custom_fields/", cancellationToken);
+ return GetCustomFieldsCore(Routes.CustomFields.Uri, cancellationToken);
}
///
public IAsyncEnumerable GetCustomFields(int pageSize, CancellationToken cancellationToken = default)
{
- return GetCustomFieldsCore($"/api/custom_fields/?page_size={pageSize}", cancellationToken);
+ return GetCustomFieldsCore(Routes.CustomFields.PagedUri(pageSize), cancellationToken);
}
///
public async Task CreateCustomField(CustomFieldCreation field)
{
using var response = await _httpClient
- .PostAsJsonAsync("/api/custom_fields/", field, _options.GetTypeInfo())
+ .PostAsJsonAsync(Routes.CustomFields.Uri, field, _options.GetTypeInfo())
.ConfigureAwait(false);
await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);
@@ -227,7 +227,7 @@ public async Task CreateCustomField(CustomFieldCreation field)
return createdField;
}
- private IAsyncEnumerable GetAllCore(string requestUri, CancellationToken cancellationToken)
+ private IAsyncEnumerable GetAllCore(Uri requestUri, CancellationToken cancellationToken)
where TDocument : Document
{
return _httpClient.GetPaginated(
@@ -240,7 +240,7 @@ private IAsyncEnumerable GetAllCore(string requestUri, Can
where TDocument : Document
{
return _httpClient.GetFromJsonAsync(
- $"/api/documents/{id}/",
+ Routes.Documents.IdUri(id),
_options.GetTypeInfo(),
cancellationToken);
}
@@ -250,7 +250,7 @@ private IAsyncEnumerable GetAllCore(string requestUri, Can
where TUpdate : DocumentUpdate
{
using var response = await _httpClient
- .PatchAsJsonAsync($"/api/documents/{id}/", update, _options.GetTypeInfo())
+ .PatchAsJsonAsync(Routes.Documents.IdUri(id), update, _options.GetTypeInfo())
.ConfigureAwait(false);
await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);
@@ -258,7 +258,7 @@ private IAsyncEnumerable GetAllCore(string requestUri, Can
return (await response.Content.ReadFromJsonAsync(_options.GetTypeInfo()).ConfigureAwait(false))!;
}
- private async IAsyncEnumerable GetCustomFieldsCore(string requestUri, [EnumeratorCancellation] CancellationToken cancellationToken)
+ private async IAsyncEnumerable GetCustomFieldsCore(Uri requestUri, [EnumeratorCancellation] CancellationToken cancellationToken)
{
var fields = _httpClient.GetPaginated(requestUri, _options.GetTypeInfo>(), cancellationToken);
diff --git a/source/VMelnalksnis.PaperlessDotNet/Routes.cs b/source/VMelnalksnis.PaperlessDotNet/Routes.cs
new file mode 100644
index 0000000..b969166
--- /dev/null
+++ b/source/VMelnalksnis.PaperlessDotNet/Routes.cs
@@ -0,0 +1,62 @@
+// 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 static System.UriKind;
+
+namespace VMelnalksnis.PaperlessDotNet;
+
+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 _tags = "/api/tags/";
+ private const string _tasks = "/api/tasks/";
+
+ private const string _pageSize = "taskId";
+
+ internal static class Correspondents
+ {
+ internal static readonly Uri Uri = new(_correspondents, Relative);
+
+ internal static Uri IdUri(int id) => new($"{_correspondents}{id}/", Relative);
+
+ internal static Uri PagedUri(int pageSize) => new($"{_correspondents}?{_pageSize}={pageSize}", Relative);
+ }
+
+ internal static class CustomFields
+ {
+ internal static readonly Uri Uri = new(_customFields, Relative);
+
+ internal static Uri PagedUri(int pageSize) => new($"{_customFields}?{_pageSize}={pageSize}", Relative);
+ }
+
+ 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 IdUri(int id) => new($"{_documents}{id}/", Relative);
+
+ internal static Uri PagedUri(int pageSize) => new($"{_documents}?{_pageSize}={pageSize}", Relative);
+ }
+
+ internal static class Tags
+ {
+ internal static readonly Uri Uri = new(_tags, Relative);
+
+ internal static Uri IdUri(int id) => new($"{_tags}{id}/", Relative);
+
+ internal static Uri PagedUri(int pageSize) => new($"{_tags}?{_pageSize}={pageSize}", Relative);
+ }
+
+ internal static class Tasks
+ {
+ internal static readonly Uri Uri = new(_tasks, Relative);
+
+ internal static Uri IdUri(Guid taskId) => new($"{_tasks}?task_id={taskId}", Relative);
+ }
+}
diff --git a/source/VMelnalksnis.PaperlessDotNet/Serialization/HttpClientExtensions.cs b/source/VMelnalksnis.PaperlessDotNet/Serialization/HttpClientExtensions.cs
index c72b1a4..5aa21a6 100644
--- a/source/VMelnalksnis.PaperlessDotNet/Serialization/HttpClientExtensions.cs
+++ b/source/VMelnalksnis.PaperlessDotNet/Serialization/HttpClientExtensions.cs
@@ -2,6 +2,7 @@
// 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;
@@ -18,12 +19,12 @@ internal static class HttpClientExtensions
{
internal static async IAsyncEnumerable GetPaginated(
this HttpClient httpClient,
- string requestUri,
+ Uri requestUri,
JsonTypeInfo> typeInfo,
[EnumeratorCancellation] CancellationToken cancellationToken)
where TResult : class
{
- var next = requestUri;
+ var next = requestUri.OriginalString;
while (next is not null && !cancellationToken.IsCancellationRequested)
{
var paginatedList = await httpClient.GetFromJsonAsync(next, typeInfo, cancellationToken).ConfigureAwait(false);
@@ -43,7 +44,7 @@ internal static class HttpClientExtensions
internal static async Task PostAsJsonAsync(
this HttpClient httpClient,
- string requestUri,
+ Uri requestUri,
TRequest request,
JsonTypeInfo requestTypeInfo,
JsonTypeInfo responseTypeInfo)
@@ -58,7 +59,7 @@ internal static class HttpClientExtensions
internal static Task PostAsJsonAsync(
this HttpClient httpClient,
- string requestUri,
+ Uri requestUri,
TValue value,
JsonTypeInfo typeInfo)
{
@@ -72,7 +73,7 @@ internal static class HttpClientExtensions
internal static Task PatchAsJsonAsync(
this HttpClient httpClient,
- string requestUri,
+ Uri requestUri,
TValue value,
JsonTypeInfo typeInfo)
{
diff --git a/source/VMelnalksnis.PaperlessDotNet/Tags/TagClient.cs b/source/VMelnalksnis.PaperlessDotNet/Tags/TagClient.cs
index 4550ef1..a539043 100644
--- a/source/VMelnalksnis.PaperlessDotNet/Tags/TagClient.cs
+++ b/source/VMelnalksnis.PaperlessDotNet/Tags/TagClient.cs
@@ -2,6 +2,7 @@
// 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;
@@ -31,20 +32,20 @@ public TagClient(HttpClient httpClient, PaperlessJsonSerializerOptions serialize
///
public IAsyncEnumerable GetAll(CancellationToken cancellationToken = default)
{
- return GetAllCore("/api/tags/", cancellationToken);
+ return GetAllCore(Routes.Tags.Uri, cancellationToken);
}
///
public IAsyncEnumerable GetAll(int pageSize, CancellationToken cancellationToken = default)
{
- return GetAllCore($"/api/tags/?page_size={pageSize}", cancellationToken);
+ return GetAllCore(Routes.Tags.PagedUri(pageSize), cancellationToken);
}
///
public Task Get(int id, CancellationToken cancellationToken = default)
{
return _httpClient.GetFromJsonAsync(
- $"/api/tags/{id}/",
+ Routes.Tags.IdUri(id),
_options.GetTypeInfo(),
cancellationToken);
}
@@ -53,7 +54,7 @@ public IAsyncEnumerable GetAll(int pageSize, CancellationToken cancellation
public Task Create(TagCreation tag)
{
return _httpClient.PostAsJsonAsync(
- "/api/tags/",
+ Routes.Tags.Uri,
tag,
_options.GetTypeInfo(),
_options.GetTypeInfo());
@@ -62,11 +63,11 @@ public Task Create(TagCreation tag)
///
public async Task Delete(int id)
{
- using var response = await _httpClient.DeleteAsync($"/api/tags/{id}/").ConfigureAwait(false);
+ using var response = await _httpClient.DeleteAsync(Routes.Tags.IdUri(id)).ConfigureAwait(false);
await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);
}
- private IAsyncEnumerable GetAllCore(string requestUri, CancellationToken cancellationToken)
+ private IAsyncEnumerable GetAllCore(Uri requestUri, CancellationToken cancellationToken)
{
return _httpClient.GetPaginated(
requestUri,
diff --git a/source/VMelnalksnis.PaperlessDotNet/Tasks/TaskClient.cs b/source/VMelnalksnis.PaperlessDotNet/Tasks/TaskClient.cs
index 06b35d4..98d8a82 100644
--- a/source/VMelnalksnis.PaperlessDotNet/Tasks/TaskClient.cs
+++ b/source/VMelnalksnis.PaperlessDotNet/Tasks/TaskClient.cs
@@ -33,14 +33,14 @@ public TaskClient(HttpClient httpClient, PaperlessJsonSerializerOptions serializ
///
public Task> GetAll(CancellationToken cancellationToken = default)
{
- return _httpClient.GetFromJsonAsync("/api/tasks/", _options.GetTypeInfo>(), cancellationToken)!;
+ return _httpClient.GetFromJsonAsync(Routes.Tasks.Uri, _options.GetTypeInfo>(), cancellationToken)!;
}
///
public async Task Get(Guid taskId, CancellationToken cancellationToken = default)
{
var tasks = await _httpClient
- .GetFromJsonAsync($"/api/tasks/?task_id={taskId}", _options.GetTypeInfo>(), cancellationToken)
+ .GetFromJsonAsync(Routes.Tasks.IdUri(taskId), _options.GetTypeInfo>(), cancellationToken)
.ConfigureAwait(false);
return tasks?.SingleOrDefault();