Skip to content

Commit

Permalink
Merge branch 'main' into meilisearch#462/total-tasks-in-task-route
Browse files Browse the repository at this point in the history
  • Loading branch information
kashifsoofi authored Nov 28, 2023
2 parents 9b436dd + 4622ff5 commit 9d3304d
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 77 deletions.
4 changes: 4 additions & 0 deletions .github/release-draft-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ categories:
label: 'bug'
- title: '🔒 Security'
label: 'security'
- title: '⚙️ Maintenance/misc'
label:
- 'maintenance'
- 'documentation'
template: |
$CHANGES
Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@

**Meilisearch** is an open-source search engine. [Learn more about Meilisearch.](https://github.com/meilisearch/meilisearch)

---

### 🔥 On November 2nd, we are hosting our first-ever live demo and product updates for [Meilisearch Cloud](https://www.meilisearch.com/cloud?utm_campaign=oss&utm_source=github&utm_medium=meilisearch). Make sure to [register here](https://us06web.zoom.us/meeting/register/tZMlc-mqrjIsH912-HTRe-AaT-pp41bDe81a#/registration) and bring your questions for live Q&A!

---

## Table of Contents <!-- omit in toc -->
## Table of Contents <!-- omit in TOC -->

- [📖 Documentation](#-documentation)
- [⚡ Supercharge your Meilisearch experience](#-supercharge-your-meilisearch-experience)
Expand Down
59 changes: 37 additions & 22 deletions src/Meilisearch/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,58 @@ internal static IDictionary<string, string> AsDictionary(this object source, Bin
/// </summary>
/// <param name="source">Object to transform.</param>
/// <param name="bindingAttr">Binding flags.</param>
/// <param name="uri">Uri to prepend before query string.</param>
/// <returns>Returns an url encoded query string.</returns>
internal static string ToQueryString(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
internal static string ToQueryString(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance, string uri = "")
{
var values = new List<string>();
foreach (var field in source.GetType().GetProperties(bindingAttr))
if (source != null)
{
var value = field.GetValue(source, null);
var key = Uri.EscapeDataString(char.ToLowerInvariant(field.Name[0]) + field.Name.Substring(1));

if (value != null)
foreach (var field in source.GetType().GetProperties(bindingAttr))
{
var type = value.GetType();
var value = field.GetValue(source, null);
var key = Uri.EscapeDataString(char.ToLowerInvariant(field.Name[0]) + field.Name.Substring(1));

if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
if (value != null)
{
var itemType = type.GetGenericArguments()[0];
if (itemType == typeof(string))
var type = value.GetType();

if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
{
values.Add(key + "=" + string.Join(",", (List<string>)value));
var itemType = type.GetGenericArguments()[0];
if (itemType == typeof(string))
{
values.Add(key + "=" + string.Join(",", (List<string>)value));
}
else if (itemType == typeof(int))
{
values.Add(key + "=" + string.Join(",", (List<int>)value));
}
}
else if (itemType == typeof(int))
else if (value is DateTime)
{
values.Add(key + "=" + string.Join(",", (List<int>)value));
values.Add(key + "=" + Uri.EscapeDataString(((DateTime)value).ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz")));
}
else
{
values.Add(key + "=" + Uri.EscapeDataString(value.ToString()));
}
}
else if (value is DateTime)
{
values.Add(key + "=" + Uri.EscapeDataString(((DateTime)value).ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz")));
}
else
{
values.Add(key + "=" + Uri.EscapeDataString(value.ToString()));
}
}
}

return string.Join("&", values);
var queryString = string.Join("&", values);
if (string.IsNullOrWhiteSpace(uri))
{
return queryString;
}

if (string.IsNullOrEmpty(queryString))
{
return uri;
}

return $"{uri}?{queryString}";
}
}
}
6 changes: 1 addition & 5 deletions src/Meilisearch/Index.Documents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,7 @@ public async Task<ResourceResults<IEnumerable<T>>> GetDocumentsAsync<T>(Document
}
else
{
var uri = $"indexes/{Uid}/documents";
if (query != null)
{
uri = $"{uri}?{query.ToQueryString()}";
}
var uri = query.ToQueryString(uri: $"indexes/{Uid}/documents");

return await _http
.GetFromJsonAsync<ResourceResults<IEnumerable<T>>>(uri, cancellationToken: cancellationToken)
Expand Down
4 changes: 2 additions & 2 deletions src/Meilisearch/Meilisearch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<OutputType>Library</OutputType>
<PackageId>MeiliSearch</PackageId>
<Version>0.14.5</Version>
<Version>0.14.6</Version>
<Description>.NET wrapper for Meilisearch, an open-source search engine</Description>
<RepositoryUrl>https://github.com/meilisearch/meilisearch-dotnet</RepositoryUrl>
<PackageTags>meilisearch;dotnet;sdk;search-engine;search;instant-search</PackageTags>
Expand All @@ -21,7 +21,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.2" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.3" />
<PackageReference Include="System.Net.Http.Json" Version="6.0.0" />
</ItemGroup>

Expand Down
25 changes: 4 additions & 21 deletions src/Meilisearch/MeilisearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@ public class MeilisearchClient
/// </summary>
/// <param name="url">URL corresponding to Meilisearch server.</param>
/// <param name="apiKey">API Key to connect to the Meilisearch server.</param>
public MeilisearchClient(string url, string apiKey = default)
public MeilisearchClient(string url, string apiKey = default) : this(new HttpClient(new MeilisearchMessageHandler(new HttpClientHandler())) { BaseAddress = url.ToSafeUri() }, apiKey)
{
_http = new HttpClient(new MeilisearchMessageHandler(new HttpClientHandler())) { BaseAddress = url.ToSafeUri() };
_http.AddApiKeyToHeader(apiKey);
_http.AddDefaultUserAgent();
_taskEndpoint = null;
ApiKey = apiKey;
}

/// <summary>
Expand Down Expand Up @@ -147,11 +142,7 @@ public async Task<TaskInfo> DeleteIndexAsync(string uid, CancellationToken cance
/// <returns>An IEnumerable of indexes in JsonElement format.</returns>
public async Task<JsonDocument> GetAllRawIndexesAsync(IndexesQuery query = default, CancellationToken cancellationToken = default)
{
var uri = $"indexes";
if (query != null)
{
uri = $"{uri}?{query.ToQueryString()}";
}
var uri = query.ToQueryString(uri: "indexes");
var response = await _http.GetAsync(uri, cancellationToken).ConfigureAwait(false);

var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Expand All @@ -166,11 +157,7 @@ public async Task<JsonDocument> GetAllRawIndexesAsync(IndexesQuery query = defau
/// <returns>Return Enumerable of Index.</returns>
public async Task<ResourceResults<IEnumerable<Index>>> GetAllIndexesAsync(IndexesQuery query = default, CancellationToken cancellationToken = default)
{
var uri = $"indexes";
if (query != null)
{
uri = $"{uri}?{query.ToQueryString()}";
}
var uri = query.ToQueryString(uri: "indexes");
var response = await _http.GetAsync(uri, cancellationToken).ConfigureAwait(false);

var content = await response.Content.ReadFromJsonAsync<ResourceResults<IEnumerable<Index>>>(cancellationToken: cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -305,11 +292,7 @@ public async Task<TaskInfo> CreateDumpAsync(CancellationToken cancellationToken
/// <returns>Returns a list of the API keys.</returns>
public async Task<ResourceResults<IEnumerable<Key>>> GetKeysAsync(KeysQuery query = default, CancellationToken cancellationToken = default)
{
var uri = $"keys";
if (query != null)
{
uri = $"{uri}?{query.ToQueryString()}";
}
var uri = query.ToQueryString(uri: "keys");
return await _http.GetFromJsonAsync<ResourceResults<IEnumerable<Key>>>(uri, cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Meilisearch/MeilisearchMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ namespace Meilisearch
/// </summary>
public class MeilisearchMessageHandler : DelegatingHandler
{

/// <summary>
/// Initializes a new instance of the <see cref="MeilisearchMessageHandler"/> class.
/// Default message handler for Meilisearch API.
/// </summary>
public MeilisearchMessageHandler()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MeilisearchMessageHandler"/> class.
/// Default message handler for Meilisearch API.
Expand Down
4 changes: 2 additions & 2 deletions src/Meilisearch/Stats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Meilisearch
/// </summary>
public class Stats
{
public Stats(int databaseSize, DateTime? lastUpdate, IReadOnlyDictionary<string, IndexStats> indexes)
public Stats(long databaseSize, DateTime? lastUpdate, IReadOnlyDictionary<string, IndexStats> indexes)
{
DatabaseSize = databaseSize;
LastUpdate = lastUpdate;
Expand All @@ -20,7 +20,7 @@ public Stats(int databaseSize, DateTime? lastUpdate, IReadOnlyDictionary<string,
/// Gets database size.
/// </summary>
[JsonPropertyName("databaseSize")]
public int DatabaseSize { get; }
public long DatabaseSize { get; }

/// <summary>
/// Gets last update timestamp.
Expand Down
18 changes: 3 additions & 15 deletions src/Meilisearch/TaskEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ public class TaskEndpoint
/// <returns>Returns a list of the tasks.</returns>
public async Task<TasksResults<IEnumerable<TaskResource>>> GetTasksAsync(TasksQuery query = default, CancellationToken cancellationToken = default)
{
var uri = $"tasks";
if (query != null)
{
uri = $"{uri}?{query.ToQueryString()}";
}
var uri = query.ToQueryString(uri: "tasks");
return await _http.GetFromJsonAsync<TasksResults<IEnumerable<TaskResource>>>(uri, cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
Expand All @@ -43,11 +39,7 @@ public async Task<TasksResults<IEnumerable<TaskResource>>> GetTasksAsync(TasksQu
/// <returns>Returns a list of the tasks.</returns>
public async Task<TaskInfo> CancelTasksAsync(CancelTasksQuery query, CancellationToken cancellationToken = default)
{
var uri = "tasks/cancel";
if (query != null)
{
uri = $"{uri}?{query.ToQueryString()}";
}
var uri = query.ToQueryString(uri: "tasks/cancel");

var response = await _http.PostAsync(uri, null, cancellationToken: cancellationToken).ConfigureAwait(false);

Expand All @@ -62,11 +54,7 @@ public async Task<TaskInfo> CancelTasksAsync(CancelTasksQuery query, Cancellatio
/// <returns>Returns a list of the tasks.</returns>
public async Task<TaskInfo> DeleteTasksAsync(DeleteTasksQuery query, CancellationToken cancellationToken = default)
{
var uri = "tasks";
if (query != null)
{
uri = $"{uri}?{query.ToQueryString()}";
}
var uri = query.ToQueryString(uri: "tasks");

var response = await _http.DeleteAsync(uri, cancellationToken).ConfigureAwait(false);

Expand Down
2 changes: 1 addition & 1 deletion tests/Meilisearch.Tests/Meilisearch.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
Expand Down
50 changes: 48 additions & 2 deletions tests/Meilisearch.Tests/ObjectExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;

using Meilisearch.Extensions;
using Meilisearch.QueryParameters;
Expand All @@ -16,6 +17,7 @@ public class FakeQuery
public string FakeString { get; set; }
public int? FakeInteger { get; set; }
public List<string> FakeStringList { get; set; }
public string Path { get; set; }
}

public class ObjectExtensionsTests
Expand Down Expand Up @@ -56,7 +58,51 @@ public void QueryStringsAreEqualsForPrimaryKey(string key)
var o = new { primaryKey = key };

var expected = QueryHelpers.AddQueryString(uri, o.AsDictionary());
var actual = $"{uri}?{o.ToQueryString()}";
var actual = o.ToQueryString(uri: uri);
Assert.Equal(expected, actual);
}

[Theory]
[InlineData("simple")]
public void QueryStringOnlyQueryStringParameters(string key)
{
var o = new { primaryKey = key };

var expected = "primaryKey=simple";
var actual = o.ToQueryString();
Assert.Equal(expected, actual);
}

[Theory]
[InlineData("simple")]
public void QueryStringPrependsUri(string key)
{
var uri = "indexes/myindex/documents";
var o = new { primaryKey = key };

var expected = QueryHelpers.AddQueryString(uri, o.AsDictionary());
var actual = o.ToQueryString(uri: uri);
Assert.Equal(expected, actual);
}

[Fact]
public void QueryStringReturnsEmptyForNullObject()
{
object o = null;

var expected = "";
var actual = o.ToQueryString();
Assert.Equal(expected, actual);
}

[Fact]
public void QueryStringReturnsUriForNullObject()
{
var uri = "indexes/myindex/documents";
object o = null;

var expected = uri;
var actual = o.ToQueryString(uri: uri);
Assert.Equal(expected, actual);
}

Expand All @@ -75,7 +121,7 @@ public void QueryStringsWithListAreEqualsForDocumentsQuery(int? offset, int? lim
{
var uri = "indexes/myindex/documents";
var dq = new DocumentsQuery { Offset = offset, Limit = limit, Fields = fields != null ? new List<string>(fields) : null };
var actualQuery = $"{uri}?{dq.ToQueryString()}";
var actualQuery = dq.ToQueryString(uri: uri);

Assert.NotEmpty(actualQuery);
Assert.NotNull(actualQuery);
Expand Down

0 comments on commit 9d3304d

Please sign in to comment.