Skip to content

Commit 772c00a

Browse files
authored
Allow setting a custom EventListener in ApiClientBuilder (#1621)
Hi Team, Thank you for your great work on developing the SDK for LINE Bot in Java. I noticed that you have already implemented `HttpInterceptor` interface, which allows the SDK users to add a custom `Interceptor` to the underlying OKHttpClient. (https://square.github.io/okhttp/features/interceptors/) With this support, we can easily add our own custom `HttpInterceptor` or reuse existing `OKHttpClient's Interceptor` by wrapping it accordingly. I was wondering if it would be possible to also allow setting a custom `EventListener` on the underlying `OkHttpClient`.(https://square.github.io/okhttp/features/events/) Based on our discussion, rather than introducing a new interface like HttpInterceptor, it seems preferable to expose the EventListener directly, as it is easier for users to use and also leads to a simpler implementation. This pull request introduces a proof of concept for adding such support. The implementation closely mirrors that of the custom interceptor mechanism , although `EventListener` is a bit more involved than `Interceptor`. With this feature, SDK users will be able to set a custom EventListener to suit their needs when creating the API client. For Example: ```java MessagingApiClient.builder("your access token") .apiEndPoint(URI.create(lineApiUrl)) .addInterceptor(new MyHttpInterceptor) .setEventListener(new MyEventListener()) .build(); ``` My use case for `OKHttpClient's EventListener`. In our system, we also use `OkHttpClient` along with `Retrofit`. To enhance the observability of API calls in our system, we leverage the `OkHttpMetricsEventListener` provided by `Micrometer` to record HTTP request metrics. (https://docs.micrometer.io/micrometer/reference/reference/okhttpclient.html). We would like to attach this metrics to LINE Bot SDK's client by adapting a `Micrometer`'s `OkHttpMetricsEventListener`, like this: ```java MessagingApiClient.builder("your access token") .apiEndPoint(URI.create(lineApiUrl)) .addInterceptor(new MyHttpInterceptor) .setEventListener(new OkHttpMetricsEventListener()) .build(); ``` Thank you for considering this suggestion. I look forward to your feedback!
1 parent 03e345f commit 772c00a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

clients/line-bot-client-base/src/main/java/com/linecorp/bot/client/base/ApiClientBuilder.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.linecorp.bot.jackson.ModelObjectMapper;
3838

3939
import okhttp3.Dispatcher;
40+
import okhttp3.EventListener;
4041
import okhttp3.Interceptor;
4142
import okhttp3.OkHttpClient;
4243
import okhttp3.Request;
@@ -89,6 +90,13 @@ public ApiClientBuilder(URI apiEndPoint, Class<T> clientClass, ExceptionBuilder
8990
*/
9091
private List<Interceptor> additionalInterceptors = new ArrayList<>();
9192

93+
/**
94+
* Custom EventListener
95+
*
96+
* <p>You can add your own EventListener.
97+
*/
98+
private EventListener eventListener;
99+
92100
private Proxy proxy;
93101

94102
private HttpAuthenticator proxyAuthenticator;
@@ -141,6 +149,11 @@ public ApiClientBuilder<T> addInterceptor(HttpInterceptor interceptor) {
141149
return this;
142150
}
143151

152+
public ApiClientBuilder<T> setEventListener(EventListener eventListener) {
153+
this.eventListener = eventListener;
154+
return this;
155+
}
156+
144157
/**
145158
* The maximum number of requests to execute concurrently.
146159
* Default: 64
@@ -230,6 +243,10 @@ public T build() {
230243
}
231244
});
232245

246+
if (this.eventListener != null) {
247+
okHttpClientBuilder.eventListener(this.eventListener);
248+
}
249+
233250
if (this.proxy != null) {
234251
okHttpClientBuilder.proxy(this.proxy);
235252
}
@@ -262,6 +279,7 @@ public String toString() {
262279
+ ", readTimeout=" + readTimeout
263280
+ ", writeTimeout=" + writeTimeout
264281
+ ", additionalInterceptors=" + additionalInterceptors
282+
+ ", eventListener=" + eventListener
265283
+ ", maxRequests=" + maxRequests
266284
+ ", maxRequestsPerHost=" + maxRequestsPerHost
267285
+ '}';

0 commit comments

Comments
 (0)