[Java] Added Play! WS filters support for retrofit2 client (#6499)

* added play! ws filters support

* samples updated
This commit is contained in:
lukoyanov 2017-09-20 07:32:52 +03:00 committed by wing328
parent 594b390e11
commit 7f6bccb4ed
2 changed files with 28 additions and 4 deletions

View File

@ -6,6 +6,7 @@ import okio.BufferedSource;
import play.libs.ws.WSClient;
import play.libs.ws.WSRequest;
import play.libs.ws.WSResponse;
import play.libs.ws.WSRequestFilter;
import java.io.IOException;
import java.net.MalformedURLException;
@ -31,11 +32,19 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
/** Extra query parameters to add to request */
private List<Pair> extraQueryParams = new ArrayList<>();
/** Filters (interceptors) */
private List<WSRequestFilter> filters = new ArrayList<>();
public Play25CallFactory(WSClient wsClient) {
this.wsClient = wsClient;
}
public Play25CallFactory(WSClient wsClient, List<WSRequestFilter> filters) {
this.wsClient = wsClient;
this.filters.addAll(filters);
}
public Play25CallFactory(WSClient wsClient, Map<String, String> extraHeaders,
List<Pair> extraQueryParams) {
this.wsClient = wsClient;
@ -74,7 +83,7 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
}
}
return new PlayWSCall(wsClient, rb.build());
return new PlayWSCall(wsClient, this.filters, rb.build());
}
/**
@ -84,12 +93,14 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
private final WSClient wsClient;
private WSRequest wsRequest;
private List<WSRequestFilter> filters;
private final Request request;
public PlayWSCall(WSClient wsClient, Request request) {
public PlayWSCall(WSClient wsClient, List<WSRequestFilter> filters, Request request) {
this.wsClient = wsClient;
this.request = request;
this.filters = filters;
}
@Override
@ -126,6 +137,7 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
if (request.body() != null) {
addBody(wsRequest);
}
filters.stream().forEach(f -> wsRequest.withRequestFilter(f));
return wsRequest.execute(request.method());
} catch (Exception e) {

View File

@ -6,6 +6,7 @@ import okio.BufferedSource;
import play.libs.ws.WSClient;
import play.libs.ws.WSRequest;
import play.libs.ws.WSResponse;
import play.libs.ws.WSRequestFilter;
import java.io.IOException;
import java.net.MalformedURLException;
@ -31,11 +32,19 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
/** Extra query parameters to add to request */
private List<Pair> extraQueryParams = new ArrayList<>();
/** Filters (interceptors) */
private List<WSRequestFilter> filters = new ArrayList<>();
public Play25CallFactory(WSClient wsClient) {
this.wsClient = wsClient;
}
public Play25CallFactory(WSClient wsClient, List<WSRequestFilter> filters) {
this.wsClient = wsClient;
this.filters.addAll(filters);
}
public Play25CallFactory(WSClient wsClient, Map<String, String> extraHeaders,
List<Pair> extraQueryParams) {
this.wsClient = wsClient;
@ -74,7 +83,7 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
}
}
return new PlayWSCall(wsClient, rb.build());
return new PlayWSCall(wsClient, this.filters, rb.build());
}
/**
@ -84,12 +93,14 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
private final WSClient wsClient;
private WSRequest wsRequest;
private List<WSRequestFilter> filters;
private final Request request;
public PlayWSCall(WSClient wsClient, Request request) {
public PlayWSCall(WSClient wsClient, List<WSRequestFilter> filters, Request request) {
this.wsClient = wsClient;
this.request = request;
this.filters = filters;
}
@Override
@ -126,6 +137,7 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
if (request.body() != null) {
addBody(wsRequest);
}
filters.stream().forEach(f -> wsRequest.withRequestFilter(f));
return wsRequest.execute(request.method());
} catch (Exception e) {