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 PatchAsync PatchAsJsonAsync methods #287

Merged
merged 4 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 29 additions & 1 deletion src/Meilisearch/Extensions/HttpExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -81,13 +83,39 @@ internal static void AddDefaultUserAgent(this HttpClient client)
client.DefaultRequestHeaders.Add("User-Agent", version.GetQualifiedVersion());
}

private static StringContent PrepareJsonPayload<T>(T body, JsonSerializerOptions options = null)
private static StringContent PrepareJsonPayload<T>(T body, JsonSerializerOptions? options = null)
{
options = options ?? Constants.JsonSerializerOptionsWriteNulls;
var payload = new StringContent(JsonSerializer.Serialize(body, options), Encoding.UTF8, "application/json");
payload.Headers.ContentType.CharSet = string.Empty;

return payload;
}

private static Task<HttpResponseMessage> 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);
}
alallema marked this conversation as resolved.
Show resolved Hide resolved

private static Task<HttpResponseMessage> 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<HttpResponseMessage> PatchAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
var content = JsonContent.Create(value, mediaType: null, options);
alallema marked this conversation as resolved.
Show resolved Hide resolved
return client.PatchAsync(requestUri, content, cancellationToken);
}

internal static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(this HttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
alallema marked this conversation as resolved.
Show resolved Hide resolved
{
var content = JsonContent.Create(value, mediaType: null, options);
return client.PatchAsync(requestUri, content, cancellationToken);
}
}
}
32 changes: 16 additions & 16 deletions src/Meilisearch/Index.Documents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public partial class Index
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <typeparam name="T">Type of the document. Even though documents are schemaless in Meilisearch, making it typed helps in compile time.</typeparam>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> AddDocumentsAsync<T>(IEnumerable<T> documents, string primaryKey = default,
public async Task<TaskInfo> AddDocumentsAsync<T>(IEnumerable<T> documents, string? primaryKey = default,
CancellationToken cancellationToken = default)
{
HttpResponseMessage responseMessage;
Expand All @@ -44,7 +44,7 @@ public async Task<TaskInfo> AddDocumentsAsync<T>(IEnumerable<T> documents, strin
/// <param name="primaryKey">Primary key for the documents.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> AddDocumentsJsonAsync(string documents, string primaryKey = default,
public async Task<TaskInfo> AddDocumentsJsonAsync(string documents, string? primaryKey = default,
CancellationToken cancellationToken = default)
{
var uri = $"indexes/{Uid}/documents";
Expand All @@ -67,7 +67,7 @@ public async Task<TaskInfo> AddDocumentsJsonAsync(string documents, string prima
/// <param name="primaryKey">Primary key for the documents.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> AddDocumentsCsvAsync(string documents, string primaryKey = default,
public async Task<TaskInfo> AddDocumentsCsvAsync(string documents, string? primaryKey = default,
CancellationToken cancellationToken = default)
{
var uri = $"indexes/{Uid}/documents";
Expand All @@ -90,7 +90,7 @@ public async Task<TaskInfo> AddDocumentsCsvAsync(string documents, string primar
/// <param name="primaryKey">Primary key for the documents.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> AddDocumentsNdjsonAsync(string documents, string primaryKey = default,
public async Task<TaskInfo> AddDocumentsNdjsonAsync(string documents, string? primaryKey = default,
CancellationToken cancellationToken = default)
{
var uri = $"indexes/{Uid}/documents";
Expand All @@ -116,7 +116,7 @@ public async Task<TaskInfo> AddDocumentsNdjsonAsync(string documents, string pri
/// <typeparam name="T">Type of the document. Even though documents are schemaless in Meilisearch, making it typed helps in compile time.</typeparam>
/// <returns>Returns the task list.</returns>
public async Task<IEnumerable<TaskInfo>> AddDocumentsInBatchesAsync<T>(IEnumerable<T> documents,
int batchSize = 1000, string primaryKey = default, CancellationToken cancellationToken = default)
int batchSize = 1000, string? primaryKey = default, CancellationToken cancellationToken = default)
{
var tasks = new List<TaskInfo>();
foreach (var chunk in documents.GetChunks(batchSize))
Expand All @@ -136,7 +136,7 @@ public async Task<IEnumerable<TaskInfo>> AddDocumentsInBatchesAsync<T>(IEnumerab
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task list.</returns>
public async Task<IEnumerable<TaskInfo>> 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<TaskInfo>();
foreach (var chunk in documents.GetCsvChunks(batchSize))
Expand All @@ -156,7 +156,7 @@ public async Task<IEnumerable<TaskInfo>> AddDocumentsCsvInBatchesAsync(string do
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task list.</returns>
public async Task<IEnumerable<TaskInfo>> 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<TaskInfo>();
foreach (var chunk in documents.GetNdjsonChunks(batchSize))
Expand All @@ -175,7 +175,7 @@ public async Task<IEnumerable<TaskInfo>> AddDocumentsNdjsonInBatchesAsync(string
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <typeparam name="T">Type of document. Even though documents are schemaless in Meilisearch, making it typed helps in compile time.</typeparam>
/// <returns>Returns the task list.</returns>
public async Task<TaskInfo> UpdateDocumentsAsync<T>(IEnumerable<T> documents, string primaryKey = default,
public async Task<TaskInfo> UpdateDocumentsAsync<T>(IEnumerable<T> documents, string? primaryKey = default,
CancellationToken cancellationToken = default)
{
HttpResponseMessage responseMessage;
Expand All @@ -199,7 +199,7 @@ public async Task<TaskInfo> UpdateDocumentsAsync<T>(IEnumerable<T> documents, st
/// <param name="primaryKey">Primary key for the documents.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> UpdateDocumentsJsonAsync(string documents, string primaryKey = default,
public async Task<TaskInfo> UpdateDocumentsJsonAsync(string documents, string? primaryKey = default,
CancellationToken cancellationToken = default)
{
var uri = $"indexes/{Uid}/documents";
Expand All @@ -222,7 +222,7 @@ public async Task<TaskInfo> UpdateDocumentsJsonAsync(string documents, string pr
/// <param name="primaryKey">Primary key for the documents.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> UpdateDocumentsCsvAsync(string documents, string primaryKey = default,
public async Task<TaskInfo> UpdateDocumentsCsvAsync(string documents, string? primaryKey = default,
CancellationToken cancellationToken = default)
{
var uri = $"indexes/{Uid}/documents";
Expand All @@ -245,7 +245,7 @@ public async Task<TaskInfo> UpdateDocumentsCsvAsync(string documents, string pri
/// <param name="primaryKey">Primary key for the documents.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> UpdateDocumentsNdjsonAsync(string documents, string primaryKey = default,
public async Task<TaskInfo> UpdateDocumentsNdjsonAsync(string documents, string? primaryKey = default,
CancellationToken cancellationToken = default)
{
var uri = $"indexes/{Uid}/documents";
Expand All @@ -271,7 +271,7 @@ public async Task<TaskInfo> UpdateDocumentsNdjsonAsync(string documents, string
/// <typeparam name="T">Type of the document. Even though documents are schemaless in Meilisearch, making it typed helps in compile time.</typeparam>
/// <returns>Returns the task list.</returns>
public async Task<IEnumerable<TaskInfo>> UpdateDocumentsInBatchesAsync<T>(IEnumerable<T> documents,
int batchSize = 1000, string primaryKey = default, CancellationToken cancellationToken = default)
int batchSize = 1000, string? primaryKey = default, CancellationToken cancellationToken = default)
{
var tasks = new List<TaskInfo>();
foreach (var chunk in documents.GetChunks(batchSize))
Expand All @@ -291,7 +291,7 @@ public async Task<IEnumerable<TaskInfo>> UpdateDocumentsInBatchesAsync<T>(IEnume
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task list.</returns>
public async Task<IEnumerable<TaskInfo>> 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<TaskInfo>();
foreach (var chunk in documents.GetCsvChunks(batchSize))
Expand All @@ -311,7 +311,7 @@ public async Task<IEnumerable<TaskInfo>> UpdateDocumentsCsvInBatchesAsync(string
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task list.</returns>
public async Task<IEnumerable<TaskInfo>> 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<TaskInfo>();
foreach (var chunk in documents.GetNdjsonChunks(batchSize))
Expand Down Expand Up @@ -355,7 +355,7 @@ public async Task<T> GetDocumentAsync<T>(int documentId, CancellationToken cance
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <typeparam name="T">Type of the document.</typeparam>
/// <returns>Returns the list of documents.</returns>
public async Task<IEnumerable<T>> GetDocumentsAsync<T>(DocumentQuery query = default,
public async Task<IEnumerable<T>> GetDocumentsAsync<T>(DocumentQuery? query = default,
CancellationToken cancellationToken = default)
{
var uri = $"indexes/{Uid}/documents";
Expand Down Expand Up @@ -447,7 +447,7 @@ public async Task<TaskInfo> DeleteAllDocumentsAsync(CancellationToken cancellati
/// <typeparam name="T">Type parameter to return.</typeparam>
/// <returns>Returns Enumerable of items.</returns>
public async Task<SearchResult<T>> SearchAsync<T>(string query,
SearchQuery searchAttributes = default(SearchQuery), CancellationToken cancellationToken = default)
SearchQuery? searchAttributes = default(SearchQuery), CancellationToken cancellationToken = default)
{
SearchQuery body;
if (searchAttributes == null)
Expand Down
2 changes: 1 addition & 1 deletion src/Meilisearch/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public partial class Index
/// <param name="primaryKey">Documents primary key.</param>
/// <param name="createdAt">The creation date of the index.</param>
/// <param name="updatedAt">The latest update of the index.</param>
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;
Expand Down
2 changes: 2 additions & 0 deletions src/Meilisearch/Meilisearch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<PackageProjectUrl>https://meilisearch.com</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/meilisearch/integration-guides/main/assets/logos/meilisearch_dotnet.png</PackageIconUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Nullable>enable</Nullable>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Meilisearch/MeilisearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Index Index(string uid)
/// <param name="primaryKey">Primary key for documents.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the associated task.</returns>
public async Task<TaskInfo> CreateIndexAsync(string uid, string primaryKey = default, CancellationToken cancellationToken = default)
public async Task<TaskInfo> 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)
Expand Down Expand Up @@ -340,9 +340,9 @@ public async Task<bool> DeleteKeyAsync(string keyUid, CancellationToken cancella
/// <exception cref="MeilisearchTenantTokenApiKeyInvalid">When there is no <paramref name="apiKey"/> defined in the client or as argument.</exception>
/// <exception cref="MeilisearchTenantTokenExpired">When the sent <paramref name="expiresAt"/> param is in the past</exception>
/// <returns>Returns a generated tenant token.</returns>
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);
}

/// <summary>
Expand Down