From b8f0fe37174183d93e25d1305ceefa352b05608f Mon Sep 17 00:00:00 2001 From: alallema Date: Tue, 12 Jul 2022 18:59:23 +0200 Subject: [PATCH 1/4] Add PatchAsync PatchAsJsonAsync methods --- src/Meilisearch/Extensions/HttpExtensions.cs | 40 +++++++++++++++++++- src/Meilisearch/Index.Documents.cs | 32 ++++++++-------- src/Meilisearch/Index.cs | 2 +- src/Meilisearch/Meilisearch.csproj | 2 + src/Meilisearch/MeilisearchClient.cs | 6 +-- 5 files changed, 61 insertions(+), 21 deletions(-) diff --git a/src/Meilisearch/Extensions/HttpExtensions.cs b/src/Meilisearch/Extensions/HttpExtensions.cs index 1a5d1b39..3101f371 100644 --- a/src/Meilisearch/Extensions/HttpExtensions.cs +++ b/src/Meilisearch/Extensions/HttpExtensions.cs @@ -1,5 +1,7 @@ +using System; using System.Net.Http; using System.Net.Http.Headers; +using System.Net.Http.Json; using System.Text; using System.Text.Json; using System.Threading; @@ -81,7 +83,7 @@ internal static void AddDefaultUserAgent(this HttpClient client) client.DefaultRequestHeaders.Add("User-Agent", version.GetQualifiedVersion()); } - private static StringContent PrepareJsonPayload(T body, JsonSerializerOptions options = null) + private static StringContent PrepareJsonPayload(T body, JsonSerializerOptions? options = null) { options = options ?? Constants.JsonSerializerOptionsWriteNulls; var payload = new StringContent(JsonSerializer.Serialize(body, options), Encoding.UTF8, "application/json"); @@ -89,5 +91,41 @@ private static StringContent PrepareJsonPayload(T body, JsonSerializerOptions return payload; } + + private static Task PatchAsync(this HttpClient client, string? requestUri, HttpContent content, CancellationToken cancellationToken) + { + var uri = string.IsNullOrEmpty(requestUri) ? null : new Uri(requestUri, UriKind.RelativeOrAbsolute); + return client.PatchAsync(uri, content, cancellationToken); + } + + private static Task PatchAsync(this HttpClient client, Uri? requestUri, HttpContent content, CancellationToken cancellationToken) + { + // HttpClient.PatchAsync is not available in .NET standard and NET462 + var method = new HttpMethod("PATCH"); + var request = new HttpRequestMessage(method, requestUri) { Content = content }; + return client.SendAsync(request, cancellationToken); + } + + internal static Task PatchAsJsonAsync(this HttpClient client, string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) + { + if (client == null) + { + throw new ArgumentNullException(nameof(client)); + } + + var content = JsonContent.Create(value, mediaType: null, options); + return client.PatchAsync(requestUri, content, cancellationToken); + } + + internal static Task PatchAsJsonAsync(this HttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) + { + if (client == null) + { + throw new ArgumentNullException(nameof(client)); + } + + var content = JsonContent.Create(value, mediaType: null, options); + return client.PatchAsync(requestUri, content, cancellationToken); + } } } diff --git a/src/Meilisearch/Index.Documents.cs b/src/Meilisearch/Index.Documents.cs index d7031334..d32ce22b 100644 --- a/src/Meilisearch/Index.Documents.cs +++ b/src/Meilisearch/Index.Documents.cs @@ -20,7 +20,7 @@ public partial class Index /// The cancellation token for this call. /// Type of the document. Even though documents are schemaless in Meilisearch, making it typed helps in compile time. /// Returns the task info. - public async Task AddDocumentsAsync(IEnumerable documents, string primaryKey = default, + public async Task AddDocumentsAsync(IEnumerable documents, string? primaryKey = default, CancellationToken cancellationToken = default) { HttpResponseMessage responseMessage; @@ -44,7 +44,7 @@ public async Task AddDocumentsAsync(IEnumerable documents, strin /// Primary key for the documents. /// The cancellation token for this call. /// Returns the task info. - public async Task AddDocumentsJsonAsync(string documents, string primaryKey = default, + public async Task AddDocumentsJsonAsync(string documents, string? primaryKey = default, CancellationToken cancellationToken = default) { var uri = $"indexes/{Uid}/documents"; @@ -67,7 +67,7 @@ public async Task AddDocumentsJsonAsync(string documents, string prima /// Primary key for the documents. /// The cancellation token for this call. /// Returns the task info. - public async Task AddDocumentsCsvAsync(string documents, string primaryKey = default, + public async Task AddDocumentsCsvAsync(string documents, string? primaryKey = default, CancellationToken cancellationToken = default) { var uri = $"indexes/{Uid}/documents"; @@ -90,7 +90,7 @@ public async Task AddDocumentsCsvAsync(string documents, string primar /// Primary key for the documents. /// The cancellation token for this call. /// Returns the task info. - public async Task AddDocumentsNdjsonAsync(string documents, string primaryKey = default, + public async Task AddDocumentsNdjsonAsync(string documents, string? primaryKey = default, CancellationToken cancellationToken = default) { var uri = $"indexes/{Uid}/documents"; @@ -116,7 +116,7 @@ public async Task AddDocumentsNdjsonAsync(string documents, string pri /// Type of the document. Even though documents are schemaless in Meilisearch, making it typed helps in compile time. /// Returns the task list. public async Task> AddDocumentsInBatchesAsync(IEnumerable documents, - int batchSize = 1000, string primaryKey = default, CancellationToken cancellationToken = default) + int batchSize = 1000, string? primaryKey = default, CancellationToken cancellationToken = default) { var tasks = new List(); foreach (var chunk in documents.GetChunks(batchSize)) @@ -136,7 +136,7 @@ public async Task> AddDocumentsInBatchesAsync(IEnumerab /// The cancellation token for this call. /// Returns the task list. public async Task> AddDocumentsCsvInBatchesAsync(string documents, - int batchSize = 1000, string primaryKey = default, CancellationToken cancellationToken = default) + int batchSize = 1000, string? primaryKey = default, CancellationToken cancellationToken = default) { var tasks = new List(); foreach (var chunk in documents.GetCsvChunks(batchSize)) @@ -156,7 +156,7 @@ public async Task> AddDocumentsCsvInBatchesAsync(string do /// The cancellation token for this call. /// Returns the task list. public async Task> AddDocumentsNdjsonInBatchesAsync(string documents, - int batchSize = 1000, string primaryKey = default, CancellationToken cancellationToken = default) + int batchSize = 1000, string? primaryKey = default, CancellationToken cancellationToken = default) { var tasks = new List(); foreach (var chunk in documents.GetNdjsonChunks(batchSize)) @@ -175,7 +175,7 @@ public async Task> AddDocumentsNdjsonInBatchesAsync(string /// The cancellation token for this call. /// Type of document. Even though documents are schemaless in Meilisearch, making it typed helps in compile time. /// Returns the task list. - public async Task UpdateDocumentsAsync(IEnumerable documents, string primaryKey = default, + public async Task UpdateDocumentsAsync(IEnumerable documents, string? primaryKey = default, CancellationToken cancellationToken = default) { HttpResponseMessage responseMessage; @@ -199,7 +199,7 @@ public async Task UpdateDocumentsAsync(IEnumerable documents, st /// Primary key for the documents. /// The cancellation token for this call. /// Returns the task info. - public async Task UpdateDocumentsJsonAsync(string documents, string primaryKey = default, + public async Task UpdateDocumentsJsonAsync(string documents, string? primaryKey = default, CancellationToken cancellationToken = default) { var uri = $"indexes/{Uid}/documents"; @@ -222,7 +222,7 @@ public async Task UpdateDocumentsJsonAsync(string documents, string pr /// Primary key for the documents. /// The cancellation token for this call. /// Returns the task info. - public async Task UpdateDocumentsCsvAsync(string documents, string primaryKey = default, + public async Task UpdateDocumentsCsvAsync(string documents, string? primaryKey = default, CancellationToken cancellationToken = default) { var uri = $"indexes/{Uid}/documents"; @@ -245,7 +245,7 @@ public async Task UpdateDocumentsCsvAsync(string documents, string pri /// Primary key for the documents. /// The cancellation token for this call. /// Returns the task info. - public async Task UpdateDocumentsNdjsonAsync(string documents, string primaryKey = default, + public async Task UpdateDocumentsNdjsonAsync(string documents, string? primaryKey = default, CancellationToken cancellationToken = default) { var uri = $"indexes/{Uid}/documents"; @@ -271,7 +271,7 @@ public async Task UpdateDocumentsNdjsonAsync(string documents, string /// Type of the document. Even though documents are schemaless in Meilisearch, making it typed helps in compile time. /// Returns the task list. public async Task> UpdateDocumentsInBatchesAsync(IEnumerable documents, - int batchSize = 1000, string primaryKey = default, CancellationToken cancellationToken = default) + int batchSize = 1000, string? primaryKey = default, CancellationToken cancellationToken = default) { var tasks = new List(); foreach (var chunk in documents.GetChunks(batchSize)) @@ -291,7 +291,7 @@ public async Task> UpdateDocumentsInBatchesAsync(IEnume /// The cancellation token for this call. /// Returns the task list. public async Task> UpdateDocumentsCsvInBatchesAsync(string documents, - int batchSize = 1000, string primaryKey = default, CancellationToken cancellationToken = default) + int batchSize = 1000, string? primaryKey = default, CancellationToken cancellationToken = default) { var tasks = new List(); foreach (var chunk in documents.GetCsvChunks(batchSize)) @@ -311,7 +311,7 @@ public async Task> UpdateDocumentsCsvInBatchesAsync(string /// The cancellation token for this call. /// Returns the task list. public async Task> UpdateDocumentsNdjsonInBatchesAsync(string documents, - int batchSize = 1000, string primaryKey = default, CancellationToken cancellationToken = default) + int batchSize = 1000, string? primaryKey = default, CancellationToken cancellationToken = default) { var tasks = new List(); foreach (var chunk in documents.GetNdjsonChunks(batchSize)) @@ -355,7 +355,7 @@ public async Task GetDocumentAsync(int documentId, CancellationToken cance /// The cancellation token for this call. /// Type of the document. /// Returns the list of documents. - public async Task> GetDocumentsAsync(DocumentQuery query = default, + public async Task> GetDocumentsAsync(DocumentQuery? query = default, CancellationToken cancellationToken = default) { var uri = $"indexes/{Uid}/documents"; @@ -447,7 +447,7 @@ public async Task DeleteAllDocumentsAsync(CancellationToken cancellati /// Type parameter to return. /// Returns Enumerable of items. public async Task> SearchAsync(string query, - SearchQuery searchAttributes = default(SearchQuery), CancellationToken cancellationToken = default) + SearchQuery? searchAttributes = default(SearchQuery), CancellationToken cancellationToken = default) { SearchQuery body; if (searchAttributes == null) diff --git a/src/Meilisearch/Index.cs b/src/Meilisearch/Index.cs index d2370930..19172ba3 100644 --- a/src/Meilisearch/Index.cs +++ b/src/Meilisearch/Index.cs @@ -23,7 +23,7 @@ public partial class Index /// Documents primary key. /// The creation date of the index. /// The latest update of the index. - public Index(string uid, string primaryKey = default, DateTimeOffset? createdAt = default, DateTimeOffset? updatedAt = default) + public Index(string uid, string? primaryKey = default, DateTimeOffset? createdAt = default, DateTimeOffset? updatedAt = default) { Uid = uid; PrimaryKey = primaryKey; diff --git a/src/Meilisearch/Meilisearch.csproj b/src/Meilisearch/Meilisearch.csproj index c7dcc9d4..59e77b66 100644 --- a/src/Meilisearch/Meilisearch.csproj +++ b/src/Meilisearch/Meilisearch.csproj @@ -12,6 +12,8 @@ https://meilisearch.com https://raw.githubusercontent.com/meilisearch/integration-guides/main/assets/logos/meilisearch_dotnet.png MIT + enable + 8.0 diff --git a/src/Meilisearch/MeilisearchClient.cs b/src/Meilisearch/MeilisearchClient.cs index 348afde8..32c62a20 100644 --- a/src/Meilisearch/MeilisearchClient.cs +++ b/src/Meilisearch/MeilisearchClient.cs @@ -84,7 +84,7 @@ public Index Index(string uid) /// Primary key for documents. /// The cancellation token for this call. /// Returns the associated task. - public async Task CreateIndexAsync(string uid, string primaryKey = default, CancellationToken cancellationToken = default) + public async Task CreateIndexAsync(string uid, string? primaryKey = default, CancellationToken cancellationToken = default) { var index = new Index(uid, primaryKey); var responseMessage = await _http.PostJsonCustomAsync("indexes", index, Constants.JsonSerializerOptionsRemoveNulls, cancellationToken: cancellationToken) @@ -340,9 +340,9 @@ public async Task DeleteKeyAsync(string keyUid, CancellationToken cancella /// When there is no defined in the client or as argument. /// When the sent param is in the past /// Returns a generated tenant token. - public string GenerateTenantToken(TenantTokenRules searchRules, string apiKey = null, DateTime? expiresAt = null) + public string GenerateTenantToken(string apiKeyUid, TenantTokenRules searchRules, string? apiKey = null, DateTime? expiresAt = null) { - return TenantToken.GenerateToken(searchRules, apiKey ?? ApiKey, expiresAt); + return TenantToken.GenerateToken(apiKeyUid, searchRules, apiKey ?? ApiKey, expiresAt); } /// From 15b77a5fe98ffa855e3359b56a55f4f0bc16b01d Mon Sep 17 00:00:00 2001 From: alallema Date: Wed, 13 Jul 2022 12:34:49 +0200 Subject: [PATCH 2/4] Changes after review --- src/Meilisearch/Extensions/HttpExtensions.cs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Meilisearch/Extensions/HttpExtensions.cs b/src/Meilisearch/Extensions/HttpExtensions.cs index 3101f371..deae4dfc 100644 --- a/src/Meilisearch/Extensions/HttpExtensions.cs +++ b/src/Meilisearch/Extensions/HttpExtensions.cs @@ -108,22 +108,12 @@ private static Task PatchAsync(this HttpClient client, Uri? internal static Task PatchAsJsonAsync(this HttpClient client, string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) { - if (client == null) - { - throw new ArgumentNullException(nameof(client)); - } - var content = JsonContent.Create(value, mediaType: null, options); return client.PatchAsync(requestUri, content, cancellationToken); } internal static Task PatchAsJsonAsync(this HttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) { - if (client == null) - { - throw new ArgumentNullException(nameof(client)); - } - var content = JsonContent.Create(value, mediaType: null, options); return client.PatchAsync(requestUri, content, cancellationToken); } From 880575e991e432de6ecddc54061d0dd786b2162a Mon Sep 17 00:00:00 2001 From: alallema Date: Wed, 13 Jul 2022 15:04:19 +0200 Subject: [PATCH 3/4] Remove useless overload method --- src/Meilisearch/Extensions/HttpExtensions.cs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/Meilisearch/Extensions/HttpExtensions.cs b/src/Meilisearch/Extensions/HttpExtensions.cs index deae4dfc..b9cfb81c 100644 --- a/src/Meilisearch/Extensions/HttpExtensions.cs +++ b/src/Meilisearch/Extensions/HttpExtensions.cs @@ -98,24 +98,10 @@ private static Task PatchAsync(this HttpClient client, stri return client.PatchAsync(uri, content, cancellationToken); } - private static Task PatchAsync(this HttpClient client, Uri? requestUri, HttpContent content, CancellationToken cancellationToken) - { - // HttpClient.PatchAsync is not available in .NET standard and NET462 - var method = new HttpMethod("PATCH"); - var request = new HttpRequestMessage(method, requestUri) { Content = content }; - return client.SendAsync(request, cancellationToken); - } - internal static Task PatchAsJsonAsync(this HttpClient client, string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) { var content = JsonContent.Create(value, mediaType: null, options); return client.PatchAsync(requestUri, content, cancellationToken); } - - internal static Task PatchAsJsonAsync(this HttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) - { - var content = JsonContent.Create(value, mediaType: null, options); - return client.PatchAsync(requestUri, content, cancellationToken); - } } } From 7b7486ede93843e73e34bc1a759783093bc28cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Am=C3=A9lie?= Date: Wed, 13 Jul 2022 15:05:43 +0200 Subject: [PATCH 4/4] Update src/Meilisearch/Extensions/HttpExtensions.cs Co-authored-by: Bruno Casali --- src/Meilisearch/Extensions/HttpExtensions.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Meilisearch/Extensions/HttpExtensions.cs b/src/Meilisearch/Extensions/HttpExtensions.cs index b9cfb81c..426e42e9 100644 --- a/src/Meilisearch/Extensions/HttpExtensions.cs +++ b/src/Meilisearch/Extensions/HttpExtensions.cs @@ -92,10 +92,9 @@ private static StringContent PrepareJsonPayload(T body, JsonSerializerOptions return payload; } - private static Task PatchAsync(this HttpClient client, string? requestUri, HttpContent content, CancellationToken cancellationToken) + private static Task PatchAsync(this HttpClient client, string requestUri, HttpContent content, CancellationToken cancellationToken) { - var uri = string.IsNullOrEmpty(requestUri) ? null : new Uri(requestUri, UriKind.RelativeOrAbsolute); - return client.PatchAsync(uri, content, cancellationToken); + return client.PatchAsync(requestUri, content, cancellationToken); } internal static Task PatchAsJsonAsync(this HttpClient client, string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)