Skip to content

Commit 63617cf

Browse files
Add code generated Task API (#6177) (#6179)
Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent f794e41 commit 63617cf

20 files changed

+1072
-624
lines changed

src/Elastic.Clients.Elasticsearch/Common/Infer/TaskId/TaskId.cs

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,88 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5-
namespace Elastic.Clients.Elasticsearch
5+
using System;
6+
using System.Diagnostics;
7+
using System.Globalization;
8+
using System.Text.Json;
9+
using System.Text.Json.Serialization;
10+
using Elastic.Transport;
11+
12+
namespace Elastic.Clients.Elasticsearch;
13+
14+
[JsonConverter(typeof(TaskIdConverter))]
15+
[DebuggerDisplay("{DebugDisplay,nq}")]
16+
public partial class TaskId : IUrlParameter, IEquatable<TaskId>
617
{
7-
//[DebuggerDisplay("{DebugDisplay,nq}")]
8-
//public partial class TaskId : IUrlParameter, IEquatable<TaskId>
9-
//{
10-
// /// <summary>
11-
// /// A task id exists in the form [node_id]:[task_id]
12-
// /// </summary>
13-
// /// <param name="taskId">the task identifier</param>
14-
// public TaskId(string taskId)
15-
// {
16-
// if (string.IsNullOrWhiteSpace(taskId))
17-
// throw new ArgumentException("TaskId can not be an empty string", nameof(taskId));
18+
/// <summary>
19+
/// A task id exists in the form [node_id]:[task_id]
20+
/// </summary>
21+
/// <param name="taskId">the task identifier</param>
22+
public TaskId(string taskId)
23+
{
24+
if (string.IsNullOrWhiteSpace(taskId))
25+
throw new ArgumentException("TaskId can not be an empty string", nameof(taskId));
1826

19-
// var tokens = taskId.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
20-
// if (tokens.Length != 2)
21-
// throw new ArgumentException($"TaskId:{taskId} not in expected format of <node_id>:<task_id>", nameof(taskId));
27+
var tokens = taskId.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
28+
if (tokens.Length != 2)
29+
throw new ArgumentException($"TaskId:{taskId} not in expected format of <node_id>:<task_id>", nameof(taskId));
2230

23-
// NodeId = tokens[0];
24-
// FullyQualifiedId = taskId;
31+
NodeId = tokens[0];
32+
FullyQualifiedId = taskId;
2533

26-
// if (!long.TryParse(tokens[1].Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out var t) || t < -1 || t == 0)
27-
// throw new ArgumentException($"TaskId task component:{tokens[1]} could not be parsed to long or is out of range", nameof(taskId));
34+
if (!long.TryParse(tokens[1].Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out var t) || t < -1 || t == 0)
35+
throw new ArgumentException($"TaskId task component:{tokens[1]} could not be parsed to long or is out of range", nameof(taskId));
2836

29-
// TaskNumber = t;
30-
// }
37+
TaskNumber = t;
38+
}
3139

32-
// public string FullyQualifiedId { get; }
33-
// public string NodeId { get; }
34-
// public long TaskNumber { get; }
40+
public string FullyQualifiedId { get; }
41+
public string NodeId { get; }
42+
public long TaskNumber { get; }
3543

36-
// private string DebugDisplay => FullyQualifiedId;
44+
private string DebugDisplay => FullyQualifiedId;
3745

38-
// public bool Equals(TaskId other) => EqualsString(other?.FullyQualifiedId);
46+
public bool Equals(TaskId other) => EqualsString(other?.FullyQualifiedId);
3947

40-
// public string GetString(ITransportConfiguration settings) => FullyQualifiedId;
48+
public string GetString(ITransportConfiguration settings) => FullyQualifiedId;
4149

42-
// public override string ToString() => FullyQualifiedId;
50+
public override string ToString() => FullyQualifiedId;
4351

44-
// public static implicit operator TaskId(string taskId) => taskId.IsNullOrEmpty() ? null : new TaskId(taskId);
52+
public static implicit operator TaskId(string taskId) => taskId.IsNullOrEmpty() ? null : new TaskId(taskId);
4553

46-
// public static bool operator ==(TaskId left, TaskId right) => Equals(left, right);
54+
public static bool operator ==(TaskId left, TaskId right) => Equals(left, right);
4755

48-
// public static bool operator !=(TaskId left, TaskId right) => !Equals(left, right);
56+
public static bool operator !=(TaskId left, TaskId right) => !Equals(left, right);
4957

50-
// public override bool Equals(object obj) =>
51-
// obj != null && obj is string s ? EqualsString(s) : obj is TaskId i && EqualsString(i.FullyQualifiedId);
58+
public override bool Equals(object obj) =>
59+
obj != null && obj is string s ? EqualsString(s) : obj is TaskId i && EqualsString(i.FullyQualifiedId);
5260

53-
// private bool EqualsString(string other) => !other.IsNullOrEmpty() && other == FullyQualifiedId;
61+
private bool EqualsString(string other) => !other.IsNullOrEmpty() && other == FullyQualifiedId;
5462

55-
// public override int GetHashCode()
56-
// {
57-
// unchecked
58-
// {
59-
// return (NodeId.GetHashCode() * 397) ^ TaskNumber.GetHashCode();
60-
// }
61-
// }
62-
//}
63+
public override int GetHashCode()
64+
{
65+
unchecked
66+
{
67+
return (NodeId.GetHashCode() * 397) ^ TaskNumber.GetHashCode();
68+
}
69+
}
70+
}
71+
72+
internal sealed class TaskIdConverter : JsonConverter<TaskId>
73+
{
74+
public override TaskId? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
75+
{
76+
if (reader.TokenType == JsonTokenType.Null)
77+
return null;
78+
79+
if (reader.TokenType == JsonTokenType.String)
80+
{
81+
var taskId = reader.GetString();
82+
return new TaskId(taskId);
83+
}
84+
85+
throw new JsonException("Unexpected JSON token");
86+
}
6387

88+
public override void Write(Utf8JsonWriter writer, TaskId value, JsonSerializerOptions options) => throw new NotImplementedException();
6489
}

src/Elastic.Clients.Elasticsearch/Serialization/DictionaryConverter.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Runtime.Serialization;
99
using System.Text.Json;
1010
using System.Text.Json.Serialization;
11+
using Elastic.Clients.Elasticsearch.Tasks;
1112

1213
namespace Elastic.Clients.Elasticsearch
1314
{
@@ -73,33 +74,43 @@ public override Dictionary<TKey, TValue> Read(
7374
{
7475
if (reader.TokenType == JsonTokenType.EndObject)
7576
return dictionary;
77+
7678
// Get the key.
7779
if (reader.TokenType != JsonTokenType.PropertyName)
7880
throw new JsonException();
79-
var propertyName = reader.GetString();
8081

81-
if (propertyName is null)
82-
throw new SerializationException("Oh no!"); // TODO handle this better
82+
var keyValue = reader.GetString();
83+
84+
if (keyValue is null)
85+
throw new JsonException("Key was null.");
8386

8487
// TODO: This is all very basic
8588
TKey key;
89+
8690
if (typeof(TKey) == typeof(string))
87-
key = (TKey)Activator.CreateInstance(typeof(string), propertyName.ToCharArray());
91+
{
92+
key = (TKey)Activator.CreateInstance(typeof(string), keyValue.ToCharArray());
93+
}
8894
else if (typeof(TKey) == typeof(IndexName))
8995
{
90-
key = IndexName.Parse(propertyName) as TKey;
96+
key = IndexName.Parse(keyValue) as TKey;
9197
}
9298
else if (typeof(TKey) == typeof(Field))
9399
{
94-
key = new Field(propertyName) as TKey;
100+
key = new Field(keyValue) as TKey;
101+
}
102+
else if (typeof(TKey) == typeof(TaskId))
103+
{
104+
key = new TaskId(keyValue) as TKey;
95105
}
96106
else
97107
{
98-
key = (TKey)Activator.CreateInstance(typeof(TKey),
99-
BindingFlags.Instance,
100-
null,
101-
new object[] { propertyName },
102-
null);
108+
throw new JsonException("Unsupported dictionary key");
109+
//key = (TKey)Activator.CreateInstance(typeof(TKey),
110+
// BindingFlags.Instance,
111+
// null,
112+
// new object[] { propertyName },
113+
// null);
103114
}
104115

105116
// Get the value.

src/Elastic.Clients.Elasticsearch/_Generated/Api/ApiUrlsLookup.g.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ internal static class ApiUrlsLookups
5151
internal static ApiUrls SqlGetAsync = new ApiUrls(new[] { "/_sql/async/{id}" });
5252
internal static ApiUrls SqlGetAsyncStatus = new ApiUrls(new[] { "/_sql/async/status/{id}" });
5353
internal static ApiUrls SqlQuery = new ApiUrls(new[] { "/_sql" });
54+
internal static ApiUrls TasksGet = new ApiUrls(new[] { "/_tasks/{task_id}" });
55+
internal static ApiUrls TasksCancel = new ApiUrls(new[] { "/_tasks/_cancel", "/_tasks/{task_id}/_cancel" });
56+
internal static ApiUrls TasksList = new ApiUrls(new[] { "/_tasks" });
5457
internal static ApiUrls NoNamespaceUpdate = new ApiUrls(new[] { "/{index}/_update/{id}" });
5558
}
5659
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
//
5+
// ███╗ ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
6+
// ████╗ ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
7+
// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗
8+
// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝
9+
// ██║ ╚████║╚██████╔╝ ██║ ██║╚██████╗███████╗
10+
// ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝
11+
// ------------------------------------------------
12+
//
13+
// This file is automatically generated.
14+
// Please do not edit these files manually.
15+
//
16+
// ------------------------------------------------
17+
18+
using Elastic.Transport;
19+
using System;
20+
using System.Collections.Generic;
21+
using System.Linq.Expressions;
22+
using System.Text.Json;
23+
using System.Text.Json.Serialization;
24+
25+
#nullable restore
26+
namespace Elastic.Clients.Elasticsearch.Tasks
27+
{
28+
public class GetTasksRequestParameters : RequestParameters<GetTasksRequestParameters>
29+
{
30+
[JsonIgnore]
31+
public Elastic.Clients.Elasticsearch.Time? Timeout { get => Q<Elastic.Clients.Elasticsearch.Time?>("timeout"); set => Q("timeout", value); }
32+
33+
[JsonIgnore]
34+
public bool? WaitForCompletion { get => Q<bool?>("wait_for_completion"); set => Q("wait_for_completion", value); }
35+
}
36+
37+
public partial class GetTasksRequest : PlainRequestBase<GetTasksRequestParameters>
38+
{
39+
public GetTasksRequest(Elastic.Clients.Elasticsearch.Id task_id) : base(r => r.Required("task_id", task_id))
40+
{
41+
}
42+
43+
internal override ApiUrls ApiUrls => ApiUrlsLookups.TasksGet;
44+
protected override HttpMethod HttpMethod => HttpMethod.GET;
45+
protected override bool SupportsBody => false;
46+
[JsonIgnore]
47+
public Elastic.Clients.Elasticsearch.Time? Timeout { get => Q<Elastic.Clients.Elasticsearch.Time?>("timeout"); set => Q("timeout", value); }
48+
49+
[JsonIgnore]
50+
public bool? WaitForCompletion { get => Q<bool?>("wait_for_completion"); set => Q("wait_for_completion", value); }
51+
}
52+
53+
public sealed partial class GetTasksRequestDescriptor : RequestDescriptorBase<GetTasksRequestDescriptor, GetTasksRequestParameters>
54+
{
55+
internal GetTasksRequestDescriptor(Action<GetTasksRequestDescriptor> configure) => configure.Invoke(this);
56+
public GetTasksRequestDescriptor(Elastic.Clients.Elasticsearch.Id task_id) : base(r => r.Required("task_id", task_id))
57+
{
58+
}
59+
60+
internal GetTasksRequestDescriptor()
61+
{
62+
}
63+
64+
internal override ApiUrls ApiUrls => ApiUrlsLookups.TasksGet;
65+
protected override HttpMethod HttpMethod => HttpMethod.GET;
66+
protected override bool SupportsBody => false;
67+
public GetTasksRequestDescriptor Timeout(Elastic.Clients.Elasticsearch.Time? timeout) => Qs("timeout", timeout);
68+
public GetTasksRequestDescriptor WaitForCompletion(bool? waitForCompletion = true) => Qs("wait_for_completion", waitForCompletion);
69+
public GetTasksRequestDescriptor TaskId(Elastic.Clients.Elasticsearch.Id task_id)
70+
{
71+
RouteValues.Required("task_id", task_id);
72+
return Self;
73+
}
74+
75+
protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
76+
{
77+
}
78+
}
79+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
//
5+
// ███╗ ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
6+
// ████╗ ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
7+
// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗
8+
// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝
9+
// ██║ ╚████║╚██████╔╝ ██║ ██║╚██████╗███████╗
10+
// ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝
11+
// ------------------------------------------------
12+
//
13+
// This file is automatically generated.
14+
// Please do not edit these files manually.
15+
//
16+
// ------------------------------------------------
17+
18+
using Elastic.Transport.Products.Elasticsearch;
19+
using System.Collections.Generic;
20+
using System.Text.Json.Serialization;
21+
22+
#nullable restore
23+
namespace Elastic.Clients.Elasticsearch.Tasks
24+
{
25+
public partial class GetTasksResponse : ResponseBase
26+
{
27+
[JsonInclude]
28+
[JsonPropertyName("completed")]
29+
public bool Completed { get; init; }
30+
31+
[JsonInclude]
32+
[JsonPropertyName("response")]
33+
public Elastic.Clients.Elasticsearch.Tasks.TaskStatus? Response { get; init; }
34+
35+
[JsonInclude]
36+
[JsonPropertyName("task")]
37+
public Elastic.Clients.Elasticsearch.Tasks.TaskInfo Task { get; init; }
38+
}
39+
}

0 commit comments

Comments
 (0)