-
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
Merged
eucyt
merged 4 commits into
line:master
from
kabigon-sung:feature/allow-setting-http-event-listener
Jun 5, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e46f642
Allow setting a custom EventListener in ApiClientBuilder
kabigon-sung b738c8d
Merge branch 'master' into feature/allow-setting-http-event-listener
kabigon-sung 196053d
Directly expose okhttp's EventListener class
kabigon-sung 08a4917
Update line-openapi submodule
kabigon-sung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
...bot-client-base/src/main/java/com/linecorp/bot/client/base/http/EventListenerAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* | ||
* Copyright 2025 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.bot.client.base.http; | ||
|
||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.net.Proxy; | ||
import java.util.List; | ||
|
||
import okhttp3.Call; | ||
import okhttp3.Connection; | ||
import okhttp3.EventListener; | ||
import okhttp3.Handshake; | ||
import okhttp3.HttpUrl; | ||
import okhttp3.Protocol; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
public class EventListenerAdapter extends EventListener { | ||
private final HttpEventListener httpEventListener; | ||
|
||
public EventListenerAdapter(HttpEventListener httpEventListener) { | ||
this.httpEventListener = httpEventListener; | ||
} | ||
|
||
@Override | ||
public void callStart(Call call) { | ||
httpEventListener.callStart(new HttpCall(call)); | ||
} | ||
|
||
@Override | ||
public void proxySelectStart(Call call, HttpUrl url) { | ||
httpEventListener.proxySelectStart(new HttpCall(call), new HttpEndpoint(url)); | ||
} | ||
|
||
@Override | ||
public void proxySelectEnd(Call call, HttpUrl url, List<Proxy> proxies) { | ||
httpEventListener.proxySelectEnd(new HttpCall(call), new HttpEndpoint(url), proxies); | ||
} | ||
|
||
@Override | ||
public void dnsStart(Call call, String domainName) { | ||
httpEventListener.dnsStart(new HttpCall(call), domainName); | ||
} | ||
|
||
@Override | ||
public void dnsEnd(Call call, String domainName, List<InetAddress> inetAddressList) { | ||
httpEventListener.dnsEnd(new HttpCall(call), domainName, inetAddressList); | ||
} | ||
|
||
@Override | ||
public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy proxy) { | ||
httpEventListener.connectStart(new HttpCall(call), inetSocketAddress, proxy); | ||
} | ||
|
||
@Override | ||
public void secureConnectStart(Call call) { | ||
httpEventListener.secureConnectStart(new HttpCall(call)); | ||
} | ||
|
||
@Override | ||
public void secureConnectEnd(Call call, Handshake handshake) { | ||
httpEventListener.secureConnectEnd(new HttpCall(call), handshake != null ? new TlsHandshake(handshake) : null); | ||
} | ||
|
||
@Override | ||
public void connectEnd(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol) { | ||
httpEventListener.connectEnd(new HttpCall(call), inetSocketAddress, proxy, protocol != null ? new HttpProtocol(protocol) : null); | ||
} | ||
|
||
@Override | ||
public void connectFailed(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol, IOException ioe) { | ||
httpEventListener.connectFailed(new HttpCall(call), inetSocketAddress, proxy, protocol != null ? new HttpProtocol(protocol) : null, ioe); | ||
} | ||
|
||
@Override | ||
public void connectionAcquired(Call call, Connection connection) { | ||
httpEventListener.connectionAcquired(new HttpCall(call), new HttpConnection(connection)); | ||
} | ||
|
||
@Override | ||
public void connectionReleased(Call call, Connection connection) { | ||
httpEventListener.connectionReleased(new HttpCall(call), new HttpConnection(connection)); | ||
} | ||
|
||
@Override | ||
public void requestHeadersStart(Call call) { | ||
httpEventListener.requestHeadersStart(new HttpCall(call)); | ||
} | ||
|
||
@Override | ||
public void requestHeadersEnd(Call call, Request request) { | ||
httpEventListener.requestHeadersEnd(new HttpCall(call), new HttpRequest(request)); | ||
} | ||
|
||
@Override | ||
public void requestBodyStart(Call call) { | ||
httpEventListener.requestBodyStart(new HttpCall(call)); | ||
} | ||
|
||
@Override | ||
public void requestBodyEnd(Call call, long byteCount) { | ||
httpEventListener.requestBodyEnd(new HttpCall(call), byteCount); | ||
} | ||
|
||
@Override | ||
public void requestFailed(Call call, IOException ioe) { | ||
httpEventListener.requestFailed(new HttpCall(call), ioe); | ||
} | ||
|
||
@Override | ||
public void responseHeadersStart(Call call) { | ||
httpEventListener.responseHeadersStart(new HttpCall(call)); | ||
} | ||
|
||
@Override | ||
public void responseHeadersEnd(Call call, Response response) { | ||
httpEventListener.responseHeadersEnd(new HttpCall(call), new HttpResponse(response)); | ||
} | ||
|
||
@Override | ||
public void responseBodyStart(Call call) { | ||
httpEventListener.responseBodyStart(new HttpCall(call)); | ||
} | ||
|
||
@Override | ||
public void responseBodyEnd(Call call, long byteCount) { | ||
httpEventListener.responseBodyEnd(new HttpCall(call), byteCount); | ||
} | ||
|
||
@Override | ||
public void responseFailed(Call call, IOException ioe) { | ||
httpEventListener.responseFailed(new HttpCall(call), ioe); | ||
} | ||
|
||
@Override | ||
public void callEnd(Call call) { | ||
httpEventListener.callEnd(new HttpCall(call)); | ||
} | ||
|
||
@Override | ||
public void callFailed(Call call, IOException ioe) { | ||
httpEventListener.callFailed(new HttpCall(call), ioe); | ||
} | ||
|
||
@Override | ||
public void canceled(Call call) { | ||
httpEventListener.canceled(new HttpCall(call)); | ||
} | ||
|
||
@Override | ||
public void satisfactionFailure(Call call, Response response) { | ||
httpEventListener.satisfactionFailure(new HttpCall(call), new HttpResponse(response)); | ||
} | ||
|
||
@Override | ||
public void cacheHit(Call call, Response response) { | ||
httpEventListener.cacheHit(new HttpCall(call), new HttpResponse(response)); | ||
} | ||
|
||
@Override | ||
public void cacheMiss(Call call) { | ||
httpEventListener.cacheMiss(new HttpCall(call)); | ||
} | ||
|
||
@Override | ||
public void cacheConditionalHit(Call call, Response cachedResponse) { | ||
httpEventListener.cacheConditionalHit(new HttpCall(call), new HttpResponse(cachedResponse)); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
clients/line-bot-client-base/src/main/java/com/linecorp/bot/client/base/http/HttpCall.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2025 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.bot.client.base.http; | ||
|
||
import okhttp3.Call; | ||
|
||
public class HttpCall { | ||
private final Call call; | ||
|
||
public HttpCall(Call call) { | ||
this.call = call; | ||
} | ||
|
||
public HttpRequest request() { | ||
return new HttpRequest(call.request()); | ||
} | ||
|
||
public Call toOkHttpCall() { | ||
return call; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../line-bot-client-base/src/main/java/com/linecorp/bot/client/base/http/HttpConnection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2025 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.bot.client.base.http; | ||
|
||
import okhttp3.Connection; | ||
|
||
public class HttpConnection { | ||
private final Connection connection; | ||
|
||
public HttpConnection(Connection connection) { | ||
this.connection = connection; | ||
} | ||
|
||
public Connection toOkHttpConnection() { | ||
return connection; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ts/line-bot-client-base/src/main/java/com/linecorp/bot/client/base/http/HttpEndpoint.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2025 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.bot.client.base.http; | ||
|
||
import okhttp3.HttpUrl; | ||
|
||
public class HttpEndpoint { | ||
private final HttpUrl httpUrl; | ||
|
||
public HttpEndpoint(HttpUrl httpUrl) { | ||
this.httpUrl = httpUrl; | ||
} | ||
|
||
public HttpUrl toOkHttpUrl() { | ||
return httpUrl; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'sEventListener
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...? 🙏Alternatively, I think it would also be good to return a builder so that users can set
EventListener
and other components as they prefer.Uh oh!
There was an error while loading. Please reload this page.
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.