-
Notifications
You must be signed in to change notification settings - Fork 48
/
Index.cs
159 lines (141 loc) · 6.38 KB
/
Index.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading;
using System.Threading.Tasks;
using Meilisearch.Extensions;
namespace Meilisearch
{
/// <summary>
/// Meilisearch index to search and manage documents.
/// </summary>
public partial class Index
{
private HttpClient _http;
private TaskEndpoint _taskEndpoint;
/// <summary>
/// Initializes a new instance of the <see cref="Index"/> class.
/// Initializes with the UID (mandatory) and the primary key.
/// </summary>
/// <param name="uid">Unique index identifier.</param>
/// <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)
{
Uid = uid;
PrimaryKey = primaryKey;
CreatedAt = createdAt;
UpdatedAt = updatedAt;
_taskEndpoint = null;
}
/// <summary>
/// Gets unique identifier of the index.
/// </summary>
public string Uid { get; internal set; }
/// <summary>
/// Gets primary key of the documents.
/// </summary>
public string PrimaryKey { get; internal set; }
/// <summary>
/// Gets the latest update date of the index.
/// </summary>
public DateTimeOffset? UpdatedAt { get; internal set; }
/// <summary>
/// Gets the creation date of the index.
/// </summary>
public DateTimeOffset? CreatedAt { get; internal set; }
/// <summary>
/// Gets raw index call response.
/// </summary>
/// <param name="http">HTTP client to make the call.</param>
/// <param name="uid">Uid of the index to retrieve.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Call response.</returns>
public static async Task<HttpResponseMessage> GetRawAsync(HttpClient http, string uid, CancellationToken cancellationToken = default)
{
return await http.GetAsync($"indexes/{uid}", cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Fetch the info of the index.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>An instance of the index fetch.</returns>
public async Task<Index> FetchInfoAsync(CancellationToken cancellationToken = default)
{
var response = await GetRawAsync(_http, Uid, cancellationToken).ConfigureAwait(false);
var content = await response.Content.ReadFromJsonAsync<Index>(cancellationToken: cancellationToken).ConfigureAwait(false);
PrimaryKey = content.PrimaryKey;
CreatedAt = content.CreatedAt;
UpdatedAt = content.UpdatedAt;
return this;
}
/// <summary>
/// Fetch the primary key of the index.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Primary key of the fetched index.</returns>
public async Task<string> FetchPrimaryKey(CancellationToken cancellationToken = default)
{
return (await FetchInfoAsync(cancellationToken).ConfigureAwait(false)).PrimaryKey;
}
/// <summary>
/// Changes the primary key of the index.
/// </summary>
/// <param name="primarykeytoChange">Primary key set.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Index with the updated Primary Key.</returns>
public async Task<TaskInfo> UpdateAsync(string primarykeytoChange, CancellationToken cancellationToken = default)
{
var responseMessage =
await _http.PatchAsJsonAsync($"indexes/{Uid}", new { primaryKey = primarykeytoChange }, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Deletes the index.
/// It's not a recovery delete. You will also lose the documents within the index.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> DeleteAsync(CancellationToken cancellationToken = default)
{
var responseMessage = await _http.DeleteAsync($"indexes/{Uid}", cancellationToken).ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Get stats.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Return index stats.</returns>
public async Task<IndexStats> GetStatsAsync(CancellationToken cancellationToken = default)
{
return await _http.GetFromJsonAsync<IndexStats>($"indexes/{Uid}/stats", cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Initializes the Index with HTTP client. Only for internal usage.
/// </summary>
/// <param name="http">HttpRequest instance used.</param>
/// <returns>The same object with the initialization.</returns>
// internal Index WithHttpClient(HttpClient client)
internal Index WithHttpClient(HttpClient http)
{
_http = http;
return this;
}
/// <summary>
/// Create a local reference to a task, without doing an HTTP call.
/// </summary>
/// <returns>Returns a TaskEndpoint instance.</returns>
private TaskEndpoint TaskEndpoint()
{
if (_taskEndpoint == null)
{
_taskEndpoint = new TaskEndpoint();
_taskEndpoint.WithHttpClient(_http);
}
return _taskEndpoint;
}
}
}