-
Notifications
You must be signed in to change notification settings - Fork 947
Allow setting a custom EventListener in ApiClientBuilder #1621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow setting a custom EventListener in ApiClientBuilder #1621
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I apologize for the delayed response 🙇
public ApiClientBuilder<T> setEventListener(HttpEventListener httpEventListener) { | ||
this.eventListener = new EventListenerAdapter(httpEventListener); | ||
return this; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you implemented HttpEventListener
by wrapping okHttp's EventListener
with reference to the Interceptor implementation, considering consistency with existing code.
However, with this approach, sdk users need to implement a class that inherits from the wrapped HttpEventListener
class each time, which is cumbersome. Therefore, how about directly acceptting okHttp's EventListener as follows...? 🙏
public ApiClientBuilder<T> setEventListener(HttpEventListener httpEventListener) { | |
this.eventListener = new EventListenerAdapter(httpEventListener); | |
return this; | |
} | |
public ApiClientBuilder<T> setEventListener(EventListener eventListener) { | |
this.eventListener = eventListener; | |
return this; | |
} |
Alternatively, I think it would also be good to return a builder so that users can set EventListener
and other components as they prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your suggestion.
If directly accepting an HttpEventListener is not a concern from your perspective, I think it’s a simple and convenient approach.
I also noticed that you mentioned another approach — returning a builder so that users can set the EventListener and other components. Could you explain this approach in more detail? (It seems that the OkHttpClientBuilder is created in build method and pass to the Retrofit.Builder )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your confirmation!
I was imagining a structure that wouldn't require additional implementation when adding not just EventListener but other options as well. Specifically, I was thinking about making the OkHttpClientBuilder directly accessible to users so they could handle the client themselves. However, as you mentioned, it would need to be passed to the Retrofit.Builder afterward, so it probably wouldn't result in a clean implementation.
It seems better to simply allow setting the EventListener directly. 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your detail explanation. I've modified the implementation to directly allow setting a EventListener. Kindly review the PR and let me know if there are any issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
I confirmed that it's possible to set okHttpMetricsEventListener in my local.
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 customInterceptor
to the underlying OKHttpClient. (https://square.github.io/okhttp/features/interceptors/) With this support, we can easily add our own customHttpInterceptor
or reuse existingOKHttpClient's Interceptor
by wrapping it accordingly.I was wondering if it would be possible to also allow setting a custom
EventListener
on the underlyingOkHttpClient
.(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 thanInterceptor
.With this feature, SDK users will be able to set a custom EventListener to suit their needs when creating the API client. For Example:
My use case for
OKHttpClient's EventListener
.In our system, we also use
OkHttpClient
along withRetrofit
. To enhance the observability of API calls in our system, we leverage theOkHttpMetricsEventListener
provided byMicrometer
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
'sOkHttpMetricsEventListener
, like this:Thank you for considering this suggestion. I look forward to your feedback!