Skip to content

Commit 09f2e6a

Browse files
committed
Read product name from HTTP responses
1 parent 7cb2c70 commit 09f2e6a

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/Elasticsearch.Net/Connection/HttpConnection.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public virtual TResponse Request<TResponse>(RequestData requestData)
6565
HttpResponseMessage responseMessage;
6666
int? statusCode = null;
6767
IEnumerable<string> warnings = null;
68+
IEnumerable<string> productNames = null;
6869
Stream responseStream = null;
6970
Exception ex = null;
7071
string mimeType = null;
@@ -95,6 +96,7 @@ public virtual TResponse Request<TResponse>(RequestData requestData)
9596

9697
requestData.MadeItToResponse = true;
9798
responseMessage.Headers.TryGetValues("Warning", out warnings);
99+
responseMessage.Headers.TryGetValues("X-elastic-product", out productNames);
98100
mimeType = responseMessage.Content.Headers.ContentType?.ToString();
99101

100102
if (responseMessage.Content != null)
@@ -114,7 +116,7 @@ public virtual TResponse Request<TResponse>(RequestData requestData)
114116
using(receive)
115117
using (responseStream ??= Stream.Null)
116118
{
117-
var response = ResponseBuilder.ToResponse<TResponse>(requestData, ex, statusCode, warnings, responseStream, mimeType);
119+
var response = ResponseBuilder.ToResponse<TResponse>(requestData, ex, statusCode, warnings, responseStream, mimeType, productNames?.FirstOrDefault());
118120

119121
// set TCP and threadpool stats on the response here so that in the event the request fails after the point of
120122
// gathering stats, they are still exposed on the call details. Ideally these would be set inside ResponseBuilder.ToResponse,
@@ -132,6 +134,7 @@ public virtual async Task<TResponse> RequestAsync<TResponse>(RequestData request
132134
HttpResponseMessage responseMessage;
133135
int? statusCode = null;
134136
IEnumerable<string> warnings = null;
137+
IEnumerable<string> productNames = null;
135138
Stream responseStream = null;
136139
Exception ex = null;
137140
string mimeType = null;
@@ -164,6 +167,7 @@ public virtual async Task<TResponse> RequestAsync<TResponse>(RequestData request
164167
requestData.MadeItToResponse = true;
165168
mimeType = responseMessage.Content.Headers.ContentType?.ToString();
166169
responseMessage.Headers.TryGetValues("Warning", out warnings);
170+
responseMessage.Headers.TryGetValues("X-elastic-product", out productNames);
167171

168172
if (responseMessage.Content != null)
169173
{
@@ -180,13 +184,13 @@ public virtual async Task<TResponse> RequestAsync<TResponse>(RequestData request
180184
ex = e;
181185
}
182186
using (receive)
183-
using (responseStream = responseStream ?? Stream.Null)
187+
using (responseStream ??= Stream.Null)
184188
{
185189
var response = await ResponseBuilder.ToResponseAsync<TResponse>
186-
(requestData, ex, statusCode, warnings, responseStream, mimeType, cancellationToken)
190+
(requestData, ex, statusCode, warnings, responseStream, mimeType, productNames?.FirstOrDefault(), cancellationToken)
187191
.ConfigureAwait(false);
188192

189-
// set TCP and threadpool stats on the response here so that in the event the request fails after the point of
193+
// set TCP and thread pool stats on the response here so that in the event the request fails after the point of
190194
// gathering stats, they are still exposed on the call details. Ideally these would be set inside ResponseBuilder.ToResponse,
191195
// but doing so would be a breaking change in 7.x
192196
response.ApiCall.TcpStats = tcpStats;

src/Elasticsearch.Net/Connection/HttpWebRequestConnection.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public virtual TResponse Request<TResponse>(RequestData requestData)
3636
{
3737
int? statusCode = null;
3838
IEnumerable<string> warnings = null;
39+
IEnumerable<string> productNames = null;
3940
Stream responseStream = null;
4041
Exception ex = null;
4142
string mimeType = null;
@@ -76,6 +77,10 @@ public virtual TResponse Request<TResponse>(RequestData requestData)
7677
//response.Headers.HasKeys() can return false even if response.Headers.AllKeys has values.
7778
if (httpWebResponse.SupportsHeaders && httpWebResponse.Headers.Count > 0 && httpWebResponse.Headers.AllKeys.Contains("Warning"))
7879
warnings = httpWebResponse.Headers.GetValues("Warning");
80+
81+
//response.Headers.HasKeys() can return false even if response.Headers.AllKeys has values.
82+
if (httpWebResponse.SupportsHeaders && httpWebResponse.Headers.Count > 0 && httpWebResponse.Headers.AllKeys.Contains("X-elastic-product"))
83+
productNames = httpWebResponse.Headers.GetValues("X-elastic-product");
7984
}
8085
catch (WebException e)
8186
{
@@ -85,7 +90,7 @@ public virtual TResponse Request<TResponse>(RequestData requestData)
8590
}
8691

8792
responseStream ??= Stream.Null;
88-
var response = ResponseBuilder.ToResponse<TResponse>(requestData, ex, statusCode, warnings, responseStream, mimeType);
93+
var response = ResponseBuilder.ToResponse<TResponse>(requestData, ex, statusCode, warnings, responseStream, mimeType, productNames.FirstOrDefault());
8994

9095
// set TCP and threadpool stats on the response here so that in the event the request fails after the point of
9196
// gathering stats, they are still exposed on the call details. Ideally these would be set inside ResponseBuilder.ToResponse,
@@ -103,6 +108,7 @@ CancellationToken cancellationToken
103108
Action unregisterWaitHandle = null;
104109
int? statusCode = null;
105110
IEnumerable<string> warnings = null;
111+
IEnumerable<string> productNames = null;
106112
Stream responseStream = null;
107113
Exception ex = null;
108114
string mimeType = null;
@@ -151,6 +157,10 @@ CancellationToken cancellationToken
151157
HandleResponse(httpWebResponse, out statusCode, out responseStream, out mimeType);
152158
if (httpWebResponse.SupportsHeaders && httpWebResponse.Headers.HasKeys() && httpWebResponse.Headers.AllKeys.Contains("Warning"))
153159
warnings = httpWebResponse.Headers.GetValues("Warning");
160+
161+
//response.Headers.HasKeys() can return false even if response.Headers.AllKeys has values.
162+
if (httpWebResponse.SupportsHeaders && httpWebResponse.Headers.Count > 0 && httpWebResponse.Headers.AllKeys.Contains("X-elastic-product"))
163+
productNames = httpWebResponse.Headers.GetValues("X-elastic-product");
154164
}
155165
}
156166
catch (WebException e)
@@ -165,10 +175,10 @@ CancellationToken cancellationToken
165175
}
166176
responseStream ??= Stream.Null;
167177
var response = await ResponseBuilder.ToResponseAsync<TResponse>
168-
(requestData, ex, statusCode, warnings, responseStream, mimeType, cancellationToken)
178+
(requestData, ex, statusCode, warnings, responseStream, mimeType, productNames.FirstOrDefault(), cancellationToken)
169179
.ConfigureAwait(false);
170180

171-
// set TCP and threadpool stats on the response here so that in the event the request fails after the point of
181+
// set TCP and thread pool stats on the response here so that in the event the request fails after the point of
172182
// gathering stats, they are still exposed on the call details. Ideally these would be set inside ResponseBuilder.ToResponse,
173183
// but doing so would be a breaking change in 7.x
174184
response.ApiCall.TcpStats = tcpStats;

0 commit comments

Comments
 (0)