Skip to content

Commit 1b2568f

Browse files
russcamStuart Cam
authored andcommitted
Add response mime type to response (#3198)
This commit is a backport of #3188 that adds the response mime type to ElasticsearchResponse.
1 parent 773875f commit 1b2568f

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

src/Elasticsearch.Net/Connection/HttpConnection-CoreFx.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public class HttpConnection : IConnection
3636
protected readonly ConcurrentDictionary<int, HttpClient> Clients = new ConcurrentDictionary<int, HttpClient>();
3737

3838
private static readonly string CanNotUseStreamResponsesWithCurlHandler =
39-
"Using Stream as TReturn does not work as expected on .NET core linux, because we can no longer guarantee this works it will be removed from the client in our 6.0 release"
39+
"Using Stream as TReturn does not work as expected on .NET core linux. " +
40+
"Because we can no longer guarantee this works it will be removed from the client in our 6.0 release"
4041
;
4142

4243
private HttpClient GetClient(RequestData requestData)
4344
{
4445
var key = GetClientKey(requestData);
45-
HttpClient client;
46-
if (this.Clients.TryGetValue(key, out client)) return client;
46+
if (this.Clients.TryGetValue(key, out var client)) return client;
4747
lock (_lock)
4848
{
4949
client = this.Clients.GetOrAdd(key, h =>
@@ -64,8 +64,6 @@ private HttpClient GetClient(RequestData requestData)
6464

6565
public virtual ElasticsearchResponse<TReturn> Request<TReturn>(RequestData requestData) where TReturn : class
6666
{
67-
//TODO remove Stream response support in 6.0, closing the stream is sufficient on desktop/mono
68-
//but not on .NET core on linux HttpClient which proxies to curl.
6967
if (typeof(TReturn) == typeof(Stream) && ConnectionConfiguration.IsCurlHandler)
7068
throw new Exception(CanNotUseStreamResponsesWithCurlHandler);
7169

@@ -78,10 +76,11 @@ public virtual ElasticsearchResponse<TReturn> Request<TReturn>(RequestData reque
7876
responseMessage = client.SendAsync(requestMessage).GetAwaiter().GetResult();
7977
requestData.MadeItToResponse = true;
8078
builder.StatusCode = (int) responseMessage.StatusCode;
81-
IEnumerable<string> warnings;
82-
if (responseMessage.Headers.TryGetValues("Warning", out warnings))
79+
if (responseMessage.Headers.TryGetValues("Warning", out var warnings))
8380
builder.DeprecationWarnings = warnings;
8481

82+
builder.ResponseMimeType = responseMessage.Content.Headers.ContentType?.MediaType;
83+
8584
if (responseMessage.Content != null)
8685
builder.Stream = responseMessage.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
8786
// https://github.com/elastic/elasticsearch-net/issues/2311
@@ -107,8 +106,6 @@ public virtual ElasticsearchResponse<TReturn> Request<TReturn>(RequestData reque
107106
public virtual async Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(RequestData requestData,
108107
CancellationToken cancellationToken) where TReturn : class
109108
{
110-
//TODO remove Stream response support in 6.0, closing the stream is sufficient on desktop/mono
111-
//but not on .NET core on linux HttpClient which proxies to curl.
112109
if (typeof(TReturn) == typeof(Stream) && ConnectionConfiguration.IsCurlHandler)
113110
throw new Exception(CanNotUseStreamResponsesWithCurlHandler);
114111

@@ -121,10 +118,11 @@ public virtual async Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(
121118
responseMessage = await client.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false);
122119
requestData.MadeItToResponse = true;
123120
builder.StatusCode = (int) responseMessage.StatusCode;
124-
IEnumerable<string> warnings;
125-
if (responseMessage.Headers.TryGetValues("Warning", out warnings))
121+
if (responseMessage.Headers.TryGetValues("Warning", out var warnings))
126122
builder.DeprecationWarnings = warnings;
127123

124+
builder.ResponseMimeType = responseMessage.Content.Headers.ContentType?.MediaType;
125+
128126
if (responseMessage.Content != null)
129127
builder.Stream = await responseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);
130128
// https://github.com/elastic/elasticsearch-net/issues/2311
@@ -250,7 +248,7 @@ protected virtual HttpRequestMessage CreateRequestMessage(RequestData requestDat
250248
else
251249
{
252250
// Set content in order to set a Content-Type header.
253-
// Content gets diposed so can't be shared instance
251+
// Content gets disposed so can't be shared instance
254252
requestMessage.Content = new ByteArrayContent(new byte[0]);
255253
}
256254

src/Elasticsearch.Net/Responses/ElasticsearchResponse.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public class ElasticsearchResponse<T> : IApiCallDetails
7575

7676
public IEnumerable<string> DeprecationWarnings { get; internal set; } = Enumerable.Empty<string>();
7777

78+
public string ResponseMimeType { get; internal set; }
79+
7880
internal bool AllowAllStatusCodes { get; }
7981

8082
/// <summary>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class ResponseBuilder<TReturn>
2222
public Exception Exception { get; set; }
2323
public int? StatusCode { get; set; }
2424
public Stream Stream { get; set; }
25+
public string ResponseMimeType { get; set; } = RequestData.MimeType;
2526

2627
public IEnumerable<string> DeprecationWarnings { get; set; }
2728

@@ -61,6 +62,7 @@ private ElasticsearchResponse<TReturn> Initialize(int? statusCode, Exception exc
6162
response.HttpMethod = this._requestData.Method;
6263
response.OriginalException = exception;
6364
response.DeprecationWarnings = this.DeprecationWarnings ?? Enumerable.Empty<string>();
65+
response.ResponseMimeType = this.ResponseMimeType;
6466
return response;
6567
}
6668

0 commit comments

Comments
 (0)