Skip to content

Commit

Permalink
First take on adding an EventListener to the API (square#6)
Browse files Browse the repository at this point in the history
extend the builder and OkHttpClient to take either an EventListener or an EventListenerFactory
  • Loading branch information
Michael Benedict authored and GitHub Enterprise committed Mar 28, 2017
1 parent c8d1243 commit b1cf5bb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
28 changes: 28 additions & 0 deletions okhttp/src/main/java/okhttp3/OkHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ public void apply(ConnectionSpec tlsConfiguration, SSLSocket sslSocket, boolean
final int readTimeout;
final int writeTimeout;
final int pingInterval;
final EventListener eventListener;
final EventListener.Factory eventListenerFactory;

public OkHttpClient() {
this(new Builder());
Expand All @@ -233,6 +235,8 @@ public OkHttpClient() {
this.cache = builder.cache;
this.internalCache = builder.internalCache;
this.socketFactory = builder.socketFactory;
this.eventListener = builder.eventListener;
this.eventListenerFactory = builder.eventListenerFactory;

boolean isTLS = false;
for (ConnectionSpec spec : connectionSpecs) {
Expand Down Expand Up @@ -404,6 +408,14 @@ public List<Interceptor> networkInterceptors() {
return networkInterceptors;
}

public EventListener eventListener() {
return eventListener;
}

public EventListener.Factory eventListenerFactory() {
return eventListenerFactory;
}

/**
* Prepares the {@code request} to be executed at some point in the future.
*/
Expand Down Expand Up @@ -451,6 +463,8 @@ public static final class Builder {
int readTimeout;
int writeTimeout;
int pingInterval;
EventListener eventListener;
EventListener.Factory eventListenerFactory;

public Builder() {
dispatcher = new Dispatcher();
Expand All @@ -472,6 +486,8 @@ public Builder() {
readTimeout = 10_000;
writeTimeout = 10_000;
pingInterval = 0;
eventListener = EventListener.NULL_EVENT_LISTENER;
eventListenerFactory = null;
}

Builder(OkHttpClient okHttpClient) {
Expand Down Expand Up @@ -501,6 +517,8 @@ public Builder() {
this.readTimeout = okHttpClient.readTimeout;
this.writeTimeout = okHttpClient.writeTimeout;
this.pingInterval = okHttpClient.pingInterval;
this.eventListener = okHttpClient.eventListener;
this.eventListenerFactory = okHttpClient.eventListenerFactory;
}

/**
Expand Down Expand Up @@ -878,6 +896,16 @@ public Builder addNetworkInterceptor(Interceptor interceptor) {
return this;
}

public Builder addEventListener(EventListener listener) {
eventListener = listener;
return this;
}

public Builder addEventListenerFactory(EventListener.Factory listenerFactory) {
eventListenerFactory = listenerFactory;
return this;
}

public OkHttpClient build() {
return new OkHttpClient(this);
}
Expand Down
8 changes: 8 additions & 0 deletions okhttp/src/main/java/okhttp3/RealCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
final class RealCall implements Call {
final OkHttpClient client;
final RetryAndFollowUpInterceptor retryAndFollowUpInterceptor;
final EventListener eventListener;

/** The application's original request unadulterated by redirects or auth headers. */
final Request originalRequest;
Expand All @@ -42,10 +43,17 @@ final class RealCall implements Call {
private boolean executed;

RealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
final EventListener.Factory eventListenerFactory = client.eventListenerFactory();

this.client = client;
this.originalRequest = originalRequest;
this.forWebSocket = forWebSocket;
this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(client, forWebSocket);
if (null == eventListenerFactory) {
this.eventListener = client.eventListener();
} else {
this.eventListener = eventListenerFactory.create(this);
}
}

@Override public Request request() {
Expand Down

0 comments on commit b1cf5bb

Please sign in to comment.