Skip to content

Commit 48acf7c

Browse files
committed
Add back the ability to register callbacks and renamed to OnRequestCompleted
Closes #1712
1 parent 8454c4a commit 48acf7c

File tree

5 files changed

+43
-23
lines changed

5 files changed

+43
-23
lines changed

Diff for: src/Elasticsearch.Net/Configuration/ConnectionConfiguration.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ public abstract class ConnectionConfiguration<T> : IConnectionConfigurationValue
123123
private bool _throwExceptions;
124124
bool IConnectionConfigurationValues.ThrowExceptions => _throwExceptions;
125125

126-
private static void DefaultApiCallHandler(IApiCallDetails status) { }
127-
Action<IApiCallDetails> _apiCallHandler = DefaultApiCallHandler;
128-
Action<IApiCallDetails> IConnectionConfigurationValues.ApiCallHandler => _apiCallHandler;
126+
private static void DefaultCompletedRequestHandler(IApiCallDetails response) { }
127+
Action<IApiCallDetails> _completedRequestHandler = DefaultCompletedRequestHandler;
128+
Action<IApiCallDetails> IConnectionConfigurationValues.OnRequestCompleted => _completedRequestHandler;
129129

130130
private readonly NameValueCollection _queryString = new NameValueCollection();
131131
NameValueCollection IConnectionConfigurationValues.QueryStringParameters => _queryString;
@@ -290,10 +290,10 @@ public T PrettyJson(bool b = true) => Assign(a =>
290290

291291
/// <summary>
292292
/// Global callback for every response that NEST receives, useful for custom logging.
293-
/// Calling this multiple times will register multiple listeners
293+
/// Calling this multiple times will register multiple listeners.
294294
/// </summary>
295-
public T ConnectionStatusHandler(Action<IApiCallDetails> handler) =>
296-
Assign(a => a._apiCallHandler += handler ?? DefaultApiCallHandler);
295+
public T OnRequestCompleted(Action<IApiCallDetails> handler) =>
296+
Assign(a => a._completedRequestHandler += handler ?? DefaultCompletedRequestHandler);
297297

298298
/// <summary>
299299
/// Basic access authentication credentials to specify with all requests.

Diff for: src/Elasticsearch.Net/Configuration/IConnectionConfigurationValues.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public interface IConnectionConfigurationValues : IDisposable
131131
/// <summary>
132132
/// Allows you to register a callback every time a an API call is returned
133133
/// </summary>
134-
Action<IApiCallDetails> ApiCallHandler { get; }
134+
Action<IApiCallDetails> OnRequestCompleted { get; }
135135

136136
/// <summary>
137137
/// Basic access authorization credentials to specify with all requests.

Diff for: src/Elasticsearch.Net/Transport/Transport.cs

+8
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ public ElasticsearchResponse<TReturn> Request<TReturn>(HttpMethod method, string
100100
}
101101
if (response == null || !response.Success)
102102
pipeline.BadResponse(ref response, requestData, seenExceptions);
103+
104+
if (this.Settings.OnRequestCompleted != null)
105+
this.Settings.OnRequestCompleted(response);
106+
103107
return response;
104108
}
105109
}
@@ -157,6 +161,10 @@ public async Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(HttpMeth
157161
}
158162
if (response == null || !response.Success)
159163
pipeline.BadResponse(ref response, requestData, seenExceptions);
164+
165+
if (this.Settings.OnRequestCompleted != null)
166+
this.Settings.OnRequestCompleted(response);
167+
160168
return response;
161169
}
162170
}

Diff for: src/Tests/ClientConcepts/LowLevel/Connecting.doc.cs

+27-15
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void InstantiatingAConnectionPoolClient()
5353
var config = new ConnectionConfiguration(connectionPool);
5454
var client = new ElasticsearchClient(config);
5555
}
56-
56+
5757
/**
5858
* Here instead of directly passing `node`, we pass a `SniffingConnectionPool` which will use our `node` to find out the rest of the available cluster nodes.
5959
* Be sure to read more about [Connection Pooling and Cluster Failover here](/elasticsearch-net/cluster-failover.html)
@@ -98,10 +98,10 @@ public void AvailableOptions()
9898
*/
9999

100100
.DisableDirectStreaming()
101-
/**
102-
* By default responses are deserialized off stream to the object you tell it to.
103-
* For debugging purposes it can be very useful to keep a copy of the raw response on the result object.
104-
*/;
101+
/**
102+
* By default responses are deserialized off stream to the object you tell it to.
103+
* For debugging purposes it can be very useful to keep a copy of the raw response on the result object.
104+
*/;
105105

106106
var result = client.Search<SearchResponse<object>>(new { size = 12 });
107107
var raw = result.ResponseBodyInBytes;
@@ -116,12 +116,6 @@ public void AvailableOptions()
116116
//hide
117117
config = config
118118
//endhide
119-
.ConnectionStatusHandler(s => { })
120-
/**
121-
* Allows you to pass a `Action&lt;IElasticsearchResponse&gt;` that can eaves drop every time a response (good or bad) is created. If you have complex logging needs
122-
* this is a good place to add that in.
123-
*/
124-
125119
.GlobalQueryStringParameters(new NameValueCollection())
126120
/**
127121
* Allows you to set querystring parameters that have to be added to every request. For instance, if you use a hosted elasticserch provider, and you need need to pass an `apiKey` parameter onto every request.
@@ -179,6 +173,23 @@ public void AvailableOptions()
179173
*/
180174
}
181175

176+
/**
177+
* You can pass a callback of type `Action&lt;IApiCallDetails&gt;` that can eaves drop every time a response (good or bad) is created.
178+
* If you have complex logging needs this is a good place to add that in.
179+
*/
180+
[U]public void OnRequestCompletedIsCalled()
181+
{
182+
var counter = 0;
183+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
184+
var settings = new ConnectionSettings(connectionPool, new InMemoryConnection())
185+
.OnRequestCompleted(r => counter++);
186+
var client = new ElasticClient(settings);
187+
client.RootNodeInfo();
188+
counter.Should().Be(1);
189+
client.RootNodeInfoAsync();
190+
counter.Should().Be(2);
191+
}
192+
182193
public void ConfiguringSSL()
183194
{
184195
/**
@@ -209,7 +220,7 @@ public void ConfiguringSSL()
209220
public class MyJsonNetSerializer : JsonNetSerializer
210221
{
211222
public MyJsonNetSerializer(IConnectionSettingsValues settings) : base(settings) { }
212-
223+
213224

214225
/**
215226
* Override ModifyJsonSerializerSettings if you need access to `JsonSerializerSettings`
@@ -223,15 +234,16 @@ public MyJsonNetSerializer(IConnectionSettingsValues settings) : base(settings)
223234
*/
224235
protected override IList<Func<Type, JsonConverter>> ContractConverters => new List<Func<Type, JsonConverter>>();
225236
}
226-
237+
227238
/**
228239
* You can then register a factory on ConnectionSettings to create an instance of your subclass instead.
229240
* This is called once per instance of ConnectionSettings.
230241
*/
231-
[U] public void ModifyJsonSerializerSettingsIsCalled()
242+
[U]
243+
public void ModifyJsonSerializerSettingsIsCalled()
232244
{
233245
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
234-
var settings = new ConnectionSettings(connectionPool, new InMemoryConnection(),s => new MyJsonNetSerializer(s));
246+
var settings = new ConnectionSettings(connectionPool, new InMemoryConnection(), s => new MyJsonNetSerializer(s));
235247
var client = new ElasticClient(settings);
236248
client.RootNodeInfo();
237249
client.RootNodeInfo();

Diff for: src/Tests/tests.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# mode either u (unit test), i (integration test) or m (mixed mode)
2-
mode: i
2+
mode: u
33
# the elasticsearch version that should be started
44
elasticsearch_version: 2.0.1
55
# whether we want to forcefully reseed on the node, if you are starting the tests with a node already running

0 commit comments

Comments
 (0)