-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ElasticLowLevelClient.cs
67 lines (55 loc) · 3.47 KB
/
ElasticLowLevelClient.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
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Elasticsearch.Net
{
/// <summary>
/// Low level client that exposes all of elasticsearch API endpoints but leaves you in charge of building request and handling the response
/// </summary>
public partial class ElasticLowLevelClient : IElasticLowLevelClient
{
private readonly UrlFormatProvider _formatter;
public IConnectionConfigurationValues Settings => this.Transport.Settings;
public IElasticsearchSerializer Serializer => this.Transport.Settings.Serializer;
protected ITransport<IConnectionConfigurationValues> Transport { get; set; }
/// <summary>Instantiate a new low level elasticsearch client to http://localhost:9200</summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
public ElasticLowLevelClient() : this(new Transport<IConnectionConfigurationValues>(new ConnectionConfiguration())) { }
/// <summary>Instantiate a new low level elasticsearch client using the specified settings</summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
public ElasticLowLevelClient(IConnectionConfigurationValues settings) : this(new Transport<IConnectionConfigurationValues>(settings ?? new ConnectionConfiguration())) { }
/// <summary>
/// Instantiate a new low level elasticsearch client explicitly specifying a custom transport setup
/// </summary>
public ElasticLowLevelClient(ITransport<IConnectionConfigurationValues> transport)
{
transport.ThrowIfNull(nameof(transport));
transport.Settings.ThrowIfNull(nameof(transport.Settings));
transport.Settings.Serializer.ThrowIfNull(nameof(transport.Settings.Serializer));
this.Transport = transport;
this._formatter = new UrlFormatProvider(this.Transport.Settings);
}
string Url(FormattableString formattable) => formattable.ToString(_formatter);
private TRequestParams _params<TRequestParams>(Func<TRequestParams, TRequestParams> requestParameters, bool allow404 = false, string contentType = null, string accept = null)
where TRequestParams : class, IRequestParameters, new()
{
var requestParams = requestParameters?.Invoke(new TRequestParams());
if (!allow404 && contentType.IsNullOrEmpty()) return requestParams;
requestParams = requestParams ?? new TRequestParams();
if (requestParams.RequestConfiguration == null) requestParams.RequestConfiguration = new RequestConfiguration();
if (allow404)
requestParams.RequestConfiguration.AllowedStatusCodes = new[] { 404 };
if (!contentType.IsNullOrEmpty() && requestParams.RequestConfiguration.ContentType.IsNullOrEmpty())
requestParams.RequestConfiguration.ContentType = contentType;
if (!accept.IsNullOrEmpty() && requestParams.RequestConfiguration.Accept.IsNullOrEmpty())
requestParams.RequestConfiguration.Accept = accept;
return requestParams;
}
public ElasticsearchResponse<T> DoRequest<T>(HttpMethod method, string path, PostData<object> data = null, IRequestParameters requestParameters = null)
where T : class =>
this.Transport.Request<T>(method, path, data, requestParameters);
public Task<ElasticsearchResponse<T>> DoRequestAsync<T>(HttpMethod method, string path, CancellationToken cancellationToken, PostData<object> data = null, IRequestParameters requestParameters = null)
where T : class =>
this.Transport.RequestAsync<T>(method, path, cancellationToken, data, requestParameters);
}
}