Skip to content

Commit 0682e4a

Browse files
committed
Merge pull request #1382 from elastic/feature/unformatted-requests
Added UsePrettyRequests() and PrettyJson() to connectionsettings
2 parents bcc2eb2 + d615281 commit 0682e4a

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

src/Elasticsearch.Net/Connection/Configuration/ConnectionConfiguration.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public class ConnectionConfiguration<T> : IConnectionConfigurationValues, IHideO
8787

8888
private bool _usePrettyResponses;
8989
bool IConnectionConfigurationValues.UsesPrettyResponses { get{ return _usePrettyResponses; } }
90+
91+
private bool _usePrettyRequests;
92+
bool IConnectionConfigurationValues.UsesPrettyRequests { get{ return _usePrettyRequests; } }
93+
9094
#if DEBUG
9195
private bool _keepRawResponse = true;
9296
#else
@@ -153,6 +157,7 @@ public ConnectionConfiguration(IConnectionPool connectionPool)
153157
this._connectionStatusHandler = this.ConnectionStatusDefaultHandler;
154158
this._maximumAsyncConnections = 0;
155159
this._connectionPool = connectionPool;
160+
this._usePrettyRequests = true;
156161
}
157162

158163
public ConnectionConfiguration(Uri uri = null)
@@ -357,6 +362,26 @@ public T SetProxy(Uri proxyAdress, string username, string password)
357362
return (T) this;
358363
}
359364

365+
/// <summary>
366+
/// Sets <see cref="UsePrettyRequests"/> and <see cref="UsePrettyResponses"/> in one go.
367+
/// Whether we want to send and recieve formatted json
368+
/// </summary>
369+
/// <param name="b"></param>
370+
/// <returns></returns>
371+
public T PrettyJson(bool b = true)
372+
{
373+
return this.UsePrettyRequests(b).UsePrettyResponses(b);
374+
}
375+
376+
/// <summary>
377+
/// Defaults to true, wether to send formatted json to elasticsearch or not
378+
/// </summary>
379+
public T UsePrettyRequests(bool b = true)
380+
{
381+
this._usePrettyRequests = b;
382+
return (T) this;
383+
}
384+
360385
/// <summary>
361386
/// Append ?pretty=true to requests, this helps to debug send and received json.
362387
/// </summary>

src/Elasticsearch.Net/Connection/Configuration/IConnectionConfigurationValues.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ public interface IConnectionConfigurationValues
9696
/// </summary>
9797
bool MetricsEnabled { get; }
9898

99+
/// <summary>
100+
/// Forces the client to pretty format the requests send to elasticsearch
101+
/// defaults to true;
102+
/// </summary>
103+
bool UsesPrettyRequests { get; }
104+
99105
/// <summary>
100106
/// Forces elasticsearch to send pretty json responses over the wire
101107
/// </summary>

src/Elasticsearch.Net/Connection/RequestHandlers/RequestHandlerBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ protected byte[] PostData(object data)
6060
if (ss != null) return (string.Join("\n", ss) + "\n").Utf8Bytes();
6161

6262
var so = data as IEnumerable<object>;
63-
if (so == null) return this._serializer.Serialize(data);
63+
var indent = this._settings.UsesPrettyRequests
64+
? SerializationFormatting.Indented
65+
: SerializationFormatting.None;
66+
if (so == null) return this._serializer.Serialize(data, indent);
6467
var joined = string.Join("\n", so
6568
.Select(soo => this._serializer.Serialize(soo, SerializationFormatting.None).Utf8String())) + "\n";
6669
return joined.Utf8Bytes();

src/Elasticsearch.Net/Serialization/ElasticsearchDefaultSerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public T Deserialize<T>(Stream stream)
2727
return SimpleJson.DeserializeObject<T>(buffer.Utf8String());
2828
}
2929
}
30+
3031
public Task<T> DeserializeAsync<T>(Stream stream)
3132
{
3233
var tcs = new TaskCompletionSource<T>();

0 commit comments

Comments
 (0)