Skip to content

Commit 277fb28

Browse files
committed
Support product name of ApiCallDetails
1 parent 300d322 commit 277fb28

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed

src/Elasticsearch.Net/Responses/ElasticsearchResponse.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Collections.ObjectModel;
88
using System.Net.NetworkInformation;
9+
using System.Text;
910
using Elasticsearch.Net.Diagnostics;
1011

1112
namespace Elasticsearch.Net
@@ -45,20 +46,31 @@ public ReadOnlyDictionary<string, ThreadPoolStatistics> ThreadPoolStats
4546

4647
/// <inheritdoc cref="IApiCallDetails.DeprecationWarnings"/>
4748
public IEnumerable<string> DeprecationWarnings => ApiCall.DeprecationWarnings;
49+
50+
/// <inheritdoc cref="IApiCallDetails.ProductName"/>
51+
public string ProductName => ApiCall.ProductName;
52+
4853
/// <inheritdoc cref="IApiCallDetails.SuccessOrKnownError"/>
4954
public bool SuccessOrKnownError => ApiCall.SuccessOrKnownError;
55+
5056
/// <inheritdoc cref="IApiCallDetails.HttpStatusCode"/>
5157
public int? HttpStatusCode => ApiCall.HttpStatusCode;
5258

5359
/// <inheritdoc cref="IApiCallDetails.Success"/>
5460
public bool Success => ApiCall.Success;
61+
5562
/// <inheritdoc cref="IApiCallDetails.OriginalException"/>
5663
public Exception OriginalException => ApiCall.OriginalException;
64+
5765
/// <inheritdoc cref="IApiCallDetails.ResponseMimeType"/>
5866
public string ResponseMimeType => ApiCall.ResponseMimeType;
67+
5968
/// <inheritdoc cref="IApiCallDetails.Uri"/>
6069
public Uri Uri => ApiCall.Uri;
6170

71+
/// <inheritdoc cref="IApiCallDetails.BuildDebugInformationPrefix"/>
72+
public Action<StringBuilder> BuildDebugInformationPrefix => ApiCall.BuildDebugInformationPrefix;
73+
6274
/// <inheritdoc cref="IApiCallDetails.ConnectionConfiguration"/>
6375
public IConnectionConfigurationValues ConnectionConfiguration => ApiCall.ConnectionConfiguration;
6476

src/Elasticsearch.Net/Responses/HttpDetails/ApiCallDetails.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ public string DebugInformation
2828

2929
var sb = new StringBuilder();
3030
sb.AppendLine(ToString());
31+
3132
_debugInformation = ResponseStatics.DebugInformationBuilder(this, sb);
3233

3334
return _debugInformation;
3435
}
3536
}
3637

3738
public IEnumerable<string> DeprecationWarnings { get; set; }
39+
public string ProductName { get; set; }
3840
public HttpMethod HttpMethod { get; set; }
3941
public int? HttpStatusCode { get; set; }
4042
public Exception OriginalException { get; set; }
@@ -50,6 +52,8 @@ public string DebugInformation
5052
&& HttpStatusCode != 502;
5153

5254
public Uri Uri { get; set; }
55+
56+
public Action<StringBuilder> BuildDebugInformationPrefix { get; set; }
5357

5458
public IConnectionConfigurationValues ConnectionConfiguration { get; set; }
5559

src/Elasticsearch.Net/Responses/HttpDetails/IApiCallDetails.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.ObjectModel;
88
using System.Diagnostics;
99
using System.Net.NetworkInformation;
10+
using System.Text;
1011
using Elasticsearch.Net.Diagnostics;
1112

1213
namespace Elasticsearch.Net
@@ -50,6 +51,11 @@ public interface IApiCallDetails
5051
/// </summary>
5152
IEnumerable<string> DeprecationWarnings { get; }
5253

54+
/// <summary>
55+
/// The product name, if present in the 'X-elastic-product' response header.
56+
/// </summary>
57+
string ProductName { get; }
58+
5359
/// <summary>
5460
/// The HTTP method used by the request
5561
/// </summary>
@@ -98,5 +104,10 @@ public interface IApiCallDetails
98104
/// The url as requested
99105
/// </summary>
100106
Uri Uri { get; }
107+
108+
/// <summary>
109+
/// A delegate which may render a prefix to the debug information.
110+
/// </summary>
111+
Action<StringBuilder> BuildDebugInformationPrefix { get; }
101112
}
102113
}

src/Elasticsearch.Net/Responses/ResponseStatics.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public static class ResponseStatics
2020

2121
public static string DebugInformationBuilder(IApiCallDetails r, StringBuilder sb)
2222
{
23+
r.BuildDebugInformationPrefix?.Invoke(sb);
24+
2325
if (r.DeprecationWarnings.HasAny())
2426
{
2527
sb.AppendLine($"# Server indicated deprecations:");
@@ -107,8 +109,10 @@ private static void AuditNodeUrl(StringBuilder sb, Audit audit)
107109

108110
if (!string.IsNullOrEmpty(uri.UserInfo))
109111
{
110-
var builder = new UriBuilder(uri);
111-
builder.Password = "redacted";
112+
var builder = new UriBuilder(uri)
113+
{
114+
Password = "redacted"
115+
};
112116
uri = builder.Uri;
113117
}
114118
sb.Append($" Node: {uri}");

src/Elasticsearch.Net/Transport/Pipeline/ResponseBuilder.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ public static TResponse ToResponse<TResponse>(
2525
int? statusCode,
2626
IEnumerable<string> warnings,
2727
Stream responseStream,
28-
string mimeType = null
28+
string mimeType = null,
29+
string productName = null
2930
)
3031
where TResponse : class, IElasticsearchResponse, new()
3132
{
3233
responseStream.ThrowIfNull(nameof(responseStream));
33-
var details = Initialize(requestData, ex, statusCode, warnings, mimeType);
34+
var details = Initialize(requestData, ex, statusCode, warnings, mimeType, productName);
3435
//TODO take ex and (responseStream == Stream.Null) into account might not need to flow to SetBody in that case
3536
var response = SetBody<TResponse>(details, requestData, responseStream, mimeType) ?? new TResponse();
3637
response.ApiCall = details;
@@ -44,21 +45,26 @@ public static async Task<TResponse> ToResponseAsync<TResponse>(
4445
IEnumerable<string> warnings,
4546
Stream responseStream,
4647
string mimeType = null,
48+
string productName = null,
4749
CancellationToken cancellationToken = default
4850
)
4951
where TResponse : class, IElasticsearchResponse, new()
5052
{
5153
responseStream.ThrowIfNull(nameof(responseStream));
52-
var details = Initialize(requestData, ex, statusCode, warnings, mimeType);
54+
var details = Initialize(requestData, ex, statusCode, warnings, mimeType, productName);
5355
var response = await SetBodyAsync<TResponse>(details, requestData, responseStream, mimeType, cancellationToken).ConfigureAwait(false)
5456
?? new TResponse();
5557
response.ApiCall = details;
5658
return response;
5759
}
5860

5961
private static ApiCallDetails Initialize(
60-
RequestData requestData, Exception exception, int? statusCode, IEnumerable<string> warnings, string mimeType
61-
)
62+
RequestData requestData,
63+
Exception exception,
64+
int? statusCode,
65+
IEnumerable<string> warnings,
66+
string mimeType,
67+
string productName)
6268
{
6369
var success = false;
6470
var allowedStatusCodes = requestData.AllowedStatusCodes;
@@ -83,7 +89,8 @@ private static ApiCallDetails Initialize(
8389
HttpMethod = requestData.Method,
8490
DeprecationWarnings = warnings ?? Enumerable.Empty<string>(),
8591
ResponseMimeType = mimeType,
86-
ConnectionConfiguration = requestData.ConnectionSettings
92+
ConnectionConfiguration = requestData.ConnectionSettings,
93+
ProductName = productName
8794
};
8895
return details;
8996
}
@@ -171,8 +178,10 @@ private static bool SetSpecialTypes<TResponse>(string mimeType, byte[] bytes, IM
171178
//if not json store the result under "body"
172179
if (!RequestData.IsJsonMimeType(mimeType))
173180
{
174-
var dictionary = new DynamicDictionary();
175-
dictionary["body"] = new DynamicValue(bytes.Utf8String());
181+
var dictionary = new DynamicDictionary
182+
{
183+
["body"] = new(bytes.Utf8String())
184+
};
176185
cs = new DynamicResponse(dictionary) as TResponse;
177186
}
178187
else

0 commit comments

Comments
 (0)