Skip to content

Commit

Permalink
fix: Move all routes/query parameters to single file
Browse files Browse the repository at this point in the history
  • Loading branch information
VMelnalksnis committed Feb 9, 2024
1 parent a37dcf1 commit 70d418b
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 36 deletions.
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 System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
Expand Down Expand Up @@ -31,26 +32,20 @@ public CorrespondentClient(HttpClient httpClient, PaperlessJsonSerializerOptions
/// <inheritdoc />
public IAsyncEnumerable<Correspondent> GetAll(CancellationToken cancellationToken = default)
{
return _httpClient.GetPaginated(
"/api/correspondents/",
_options.GetTypeInfo<PaginatedList<Correspondent>>(),
cancellationToken);
return GetAllCore(Routes.Correspondents.Uri, cancellationToken);
}

/// <inheritdoc />
public IAsyncEnumerable<Correspondent> GetAll(int pageSize, CancellationToken cancellationToken = default)
{
return _httpClient.GetPaginated(
$"/api/correspondents/?page_size={pageSize}",
_options.GetTypeInfo<PaginatedList<Correspondent>>(),
cancellationToken);
return GetAllCore(Routes.Correspondents.PagedUri(pageSize), cancellationToken);
}

/// <inheritdoc />
public Task<Correspondent?> Get(int id, CancellationToken cancellationToken = default)
{
return _httpClient.GetFromJsonAsync(
$"/api/correspondents/{id}/",
Routes.Correspondents.IdUri(id),
_options.GetTypeInfo<Correspondent>(),
cancellationToken);
}
Expand All @@ -59,7 +54,7 @@ public IAsyncEnumerable<Correspondent> GetAll(int pageSize, CancellationToken ca
public Task<Correspondent> Create(CorrespondentCreation correspondent)
{
return _httpClient.PostAsJsonAsync(
"/api/correspondents/",
Routes.Correspondents.Uri,
correspondent,
_options.GetTypeInfo<CorrespondentCreation>(),
_options.GetTypeInfo<Correspondent>());
Expand All @@ -68,7 +63,15 @@ public Task<Correspondent> Create(CorrespondentCreation correspondent)
/// <inheritdoc />
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<Correspondent> GetAllCore(Uri requestUri, CancellationToken cancellationToken)
{
return _httpClient.GetPaginated(
requestUri,
_options.GetTypeInfo<PaginatedList<Correspondent>>(),
cancellationToken);
}
}
24 changes: 12 additions & 12 deletions source/VMelnalksnis.PaperlessDotNet/Documents/DocumentClient.cs
Expand Up @@ -44,7 +44,7 @@ public DocumentClient(HttpClient httpClient, PaperlessJsonSerializerOptions seri
/// <inheritdoc />
public IAsyncEnumerable<Document> GetAll(CancellationToken cancellationToken = default)
{
return GetAllCore<Document>("/api/documents/", cancellationToken);
return GetAllCore<Document>(Routes.Documents.Uri, cancellationToken);
}

/// <inheritdoc />
Expand All @@ -57,7 +57,7 @@ await foreach (var unused in GetCustomFields(cancellationToken).ConfigureAwait(f
}
}

var documents = GetAllCore<Document<TFields>>("/api/documents/", cancellationToken);
var documents = GetAllCore<Document<TFields>>(Routes.Documents.Uri, cancellationToken);
await foreach (var document in documents.ConfigureAwait(false))
{
yield return document;
Expand All @@ -67,7 +67,7 @@ await foreach (var document in documents.ConfigureAwait(false))
/// <inheritdoc />
public IAsyncEnumerable<Document> GetAll(int pageSize, CancellationToken cancellationToken = default)
{
return GetAllCore<Document>($"/api/documents/?page_size={pageSize}", cancellationToken);
return GetAllCore<Document>(Routes.Documents.PagedUri(pageSize), cancellationToken);
}

/// <inheritdoc />
Expand All @@ -80,7 +80,7 @@ await foreach (var unused in GetCustomFields(cancellationToken).ConfigureAwait(f
}
}

var documents = GetAllCore<Document<TFields>>($"/api/documents/?page_size={pageSize}", cancellationToken);
var documents = GetAllCore<Document<TFields>>(Routes.Documents.PagedUri(pageSize), cancellationToken);

Check warning on line 83 in source/VMelnalksnis.PaperlessDotNet/Documents/DocumentClient.cs

View check run for this annotation

Codecov / codecov/patch

source/VMelnalksnis.PaperlessDotNet/Documents/DocumentClient.cs#L83

Added line #L83 was not covered by tests
await foreach (var document in documents.ConfigureAwait(false))
{
yield return document;
Expand Down Expand Up @@ -147,7 +147,7 @@ public async Task<DocumentCreationResult> 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,
Expand Down Expand Up @@ -203,20 +203,20 @@ await foreach (var unused in GetCustomFields().ConfigureAwait(false))
/// <inheritdoc />
public IAsyncEnumerable<CustomField> GetCustomFields(CancellationToken cancellationToken = default)
{
return GetCustomFieldsCore($"/api/custom_fields/", cancellationToken);
return GetCustomFieldsCore(Routes.CustomFields.Uri, cancellationToken);
}

/// <inheritdoc />
public IAsyncEnumerable<CustomField> GetCustomFields(int pageSize, CancellationToken cancellationToken = default)
{
return GetCustomFieldsCore($"/api/custom_fields/?page_size={pageSize}", cancellationToken);
return GetCustomFieldsCore(Routes.CustomFields.PagedUri(pageSize), cancellationToken);
}

/// <inheritdoc />
public async Task<CustomField> CreateCustomField(CustomFieldCreation field)
{
using var response = await _httpClient
.PostAsJsonAsync("/api/custom_fields/", field, _options.GetTypeInfo<CustomFieldCreation>())
.PostAsJsonAsync(Routes.CustomFields.Uri, field, _options.GetTypeInfo<CustomFieldCreation>())
.ConfigureAwait(false);

await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);
Expand All @@ -227,7 +227,7 @@ public async Task<CustomField> CreateCustomField(CustomFieldCreation field)
return createdField;
}

private IAsyncEnumerable<TDocument> GetAllCore<TDocument>(string requestUri, CancellationToken cancellationToken)
private IAsyncEnumerable<TDocument> GetAllCore<TDocument>(Uri requestUri, CancellationToken cancellationToken)
where TDocument : Document
{
return _httpClient.GetPaginated(
Expand All @@ -240,7 +240,7 @@ private IAsyncEnumerable<TDocument> GetAllCore<TDocument>(string requestUri, Can
where TDocument : Document
{
return _httpClient.GetFromJsonAsync(
$"/api/documents/{id}/",
Routes.Documents.IdUri(id),
_options.GetTypeInfo<TDocument>(),
cancellationToken);
}
Expand All @@ -250,15 +250,15 @@ private IAsyncEnumerable<TDocument> GetAllCore<TDocument>(string requestUri, Can
where TUpdate : DocumentUpdate
{
using var response = await _httpClient
.PatchAsJsonAsync($"/api/documents/{id}/", update, _options.GetTypeInfo<TUpdate>())
.PatchAsJsonAsync(Routes.Documents.IdUri(id), update, _options.GetTypeInfo<TUpdate>())
.ConfigureAwait(false);

await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);

return (await response.Content.ReadFromJsonAsync(_options.GetTypeInfo<TDocument>()).ConfigureAwait(false))!;
}

private async IAsyncEnumerable<CustomField> GetCustomFieldsCore(string requestUri, [EnumeratorCancellation] CancellationToken cancellationToken)
private async IAsyncEnumerable<CustomField> GetCustomFieldsCore(Uri requestUri, [EnumeratorCancellation] CancellationToken cancellationToken)
{
var fields = _httpClient.GetPaginated(requestUri, _options.GetTypeInfo<PaginatedList<CustomField>>(), cancellationToken);

Expand Down
62 changes: 62 additions & 0 deletions 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);

Check warning on line 58 in source/VMelnalksnis.PaperlessDotNet/Routes.cs

View check run for this annotation

Codecov / codecov/patch

source/VMelnalksnis.PaperlessDotNet/Routes.cs#L58

Added line #L58 was not covered by tests

internal static Uri IdUri(Guid taskId) => new($"{_tasks}?task_id={taskId}", Relative);
}
}
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 System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
Expand All @@ -18,12 +19,12 @@ internal static class HttpClientExtensions
{
internal static async IAsyncEnumerable<TResult> GetPaginated<TResult>(
this HttpClient httpClient,
string requestUri,
Uri requestUri,
JsonTypeInfo<PaginatedList<TResult>> 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);
Expand All @@ -43,7 +44,7 @@ internal static class HttpClientExtensions

internal static async Task<TResponse> PostAsJsonAsync<TRequest, TResponse>(
this HttpClient httpClient,
string requestUri,
Uri requestUri,
TRequest request,
JsonTypeInfo<TRequest> requestTypeInfo,
JsonTypeInfo<TResponse> responseTypeInfo)
Expand All @@ -58,7 +59,7 @@ internal static class HttpClientExtensions

internal static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(
this HttpClient httpClient,
string requestUri,
Uri requestUri,
TValue value,
JsonTypeInfo<TValue> typeInfo)
{
Expand All @@ -72,7 +73,7 @@ internal static class HttpClientExtensions

internal static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(
this HttpClient httpClient,
string requestUri,
Uri requestUri,
TValue value,
JsonTypeInfo<TValue> typeInfo)
{
Expand Down
13 changes: 7 additions & 6 deletions source/VMelnalksnis.PaperlessDotNet/Tags/TagClient.cs
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 System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
Expand Down Expand Up @@ -31,20 +32,20 @@ public TagClient(HttpClient httpClient, PaperlessJsonSerializerOptions serialize
/// <inheritdoc />
public IAsyncEnumerable<Tag> GetAll(CancellationToken cancellationToken = default)
{
return GetAllCore("/api/tags/", cancellationToken);
return GetAllCore(Routes.Tags.Uri, cancellationToken);
}

/// <inheritdoc />
public IAsyncEnumerable<Tag> GetAll(int pageSize, CancellationToken cancellationToken = default)
{
return GetAllCore($"/api/tags/?page_size={pageSize}", cancellationToken);
return GetAllCore(Routes.Tags.PagedUri(pageSize), cancellationToken);
}

/// <inheritdoc />
public Task<Tag?> Get(int id, CancellationToken cancellationToken = default)
{
return _httpClient.GetFromJsonAsync(
$"/api/tags/{id}/",
Routes.Tags.IdUri(id),
_options.GetTypeInfo<Tag>(),
cancellationToken);
}
Expand All @@ -53,7 +54,7 @@ public IAsyncEnumerable<Tag> GetAll(int pageSize, CancellationToken cancellation
public Task<Tag> Create(TagCreation tag)
{
return _httpClient.PostAsJsonAsync(
"/api/tags/",
Routes.Tags.Uri,
tag,
_options.GetTypeInfo<TagCreation>(),
_options.GetTypeInfo<Tag>());
Expand All @@ -62,11 +63,11 @@ public Task<Tag> Create(TagCreation tag)
/// <inheritdoc />
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<Tag> GetAllCore(string requestUri, CancellationToken cancellationToken)
private IAsyncEnumerable<Tag> GetAllCore(Uri requestUri, CancellationToken cancellationToken)
{
return _httpClient.GetPaginated(
requestUri,
Expand Down
4 changes: 2 additions & 2 deletions source/VMelnalksnis.PaperlessDotNet/Tasks/TaskClient.cs
Expand Up @@ -33,14 +33,14 @@ public TaskClient(HttpClient httpClient, PaperlessJsonSerializerOptions serializ
/// <inheritdoc />
public Task<List<PaperlessTask>> GetAll(CancellationToken cancellationToken = default)
{
return _httpClient.GetFromJsonAsync("/api/tasks/", _options.GetTypeInfo<List<PaperlessTask>>(), cancellationToken)!;
return _httpClient.GetFromJsonAsync(Routes.Tasks.Uri, _options.GetTypeInfo<List<PaperlessTask>>(), cancellationToken)!;

Check warning on line 36 in source/VMelnalksnis.PaperlessDotNet/Tasks/TaskClient.cs

View check run for this annotation

Codecov / codecov/patch

source/VMelnalksnis.PaperlessDotNet/Tasks/TaskClient.cs#L36

Added line #L36 was not covered by tests
}

/// <inheritdoc />
public async Task<PaperlessTask?> Get(Guid taskId, CancellationToken cancellationToken = default)
{
var tasks = await _httpClient
.GetFromJsonAsync($"/api/tasks/?task_id={taskId}", _options.GetTypeInfo<List<PaperlessTask>>(), cancellationToken)
.GetFromJsonAsync(Routes.Tasks.IdUri(taskId), _options.GetTypeInfo<List<PaperlessTask>>(), cancellationToken)
.ConfigureAwait(false);

return tasks?.SingleOrDefault();
Expand Down

0 comments on commit 70d418b

Please sign in to comment.