forked from loafle/openapi-generator-original
[Java] Added Play! WS filters support for retrofit2 client (#6499)
* added play! ws filters support * samples updated
This commit is contained in:
parent
594b390e11
commit
7f6bccb4ed
@ -6,6 +6,7 @@ import okio.BufferedSource;
|
|||||||
import play.libs.ws.WSClient;
|
import play.libs.ws.WSClient;
|
||||||
import play.libs.ws.WSRequest;
|
import play.libs.ws.WSRequest;
|
||||||
import play.libs.ws.WSResponse;
|
import play.libs.ws.WSResponse;
|
||||||
|
import play.libs.ws.WSRequestFilter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
@ -32,10 +33,18 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
|
|||||||
/** Extra query parameters to add to request */
|
/** Extra query parameters to add to request */
|
||||||
private List<Pair> extraQueryParams = new ArrayList<>();
|
private List<Pair> extraQueryParams = new ArrayList<>();
|
||||||
|
|
||||||
|
/** Filters (interceptors) */
|
||||||
|
private List<WSRequestFilter> filters = new ArrayList<>();
|
||||||
|
|
||||||
public Play25CallFactory(WSClient wsClient) {
|
public Play25CallFactory(WSClient wsClient) {
|
||||||
this.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,
|
public Play25CallFactory(WSClient wsClient, Map<String, String> extraHeaders,
|
||||||
List<Pair> extraQueryParams) {
|
List<Pair> extraQueryParams) {
|
||||||
this.wsClient = wsClient;
|
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 final WSClient wsClient;
|
||||||
private WSRequest wsRequest;
|
private WSRequest wsRequest;
|
||||||
|
private List<WSRequestFilter> filters;
|
||||||
|
|
||||||
private final Request request;
|
private final Request request;
|
||||||
|
|
||||||
public PlayWSCall(WSClient wsClient, Request request) {
|
public PlayWSCall(WSClient wsClient, List<WSRequestFilter> filters, Request request) {
|
||||||
this.wsClient = wsClient;
|
this.wsClient = wsClient;
|
||||||
this.request = request;
|
this.request = request;
|
||||||
|
this.filters = filters;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -126,6 +137,7 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
|
|||||||
if (request.body() != null) {
|
if (request.body() != null) {
|
||||||
addBody(wsRequest);
|
addBody(wsRequest);
|
||||||
}
|
}
|
||||||
|
filters.stream().forEach(f -> wsRequest.withRequestFilter(f));
|
||||||
|
|
||||||
return wsRequest.execute(request.method());
|
return wsRequest.execute(request.method());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -6,6 +6,7 @@ import okio.BufferedSource;
|
|||||||
import play.libs.ws.WSClient;
|
import play.libs.ws.WSClient;
|
||||||
import play.libs.ws.WSRequest;
|
import play.libs.ws.WSRequest;
|
||||||
import play.libs.ws.WSResponse;
|
import play.libs.ws.WSResponse;
|
||||||
|
import play.libs.ws.WSRequestFilter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
@ -32,10 +33,18 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
|
|||||||
/** Extra query parameters to add to request */
|
/** Extra query parameters to add to request */
|
||||||
private List<Pair> extraQueryParams = new ArrayList<>();
|
private List<Pair> extraQueryParams = new ArrayList<>();
|
||||||
|
|
||||||
|
/** Filters (interceptors) */
|
||||||
|
private List<WSRequestFilter> filters = new ArrayList<>();
|
||||||
|
|
||||||
public Play25CallFactory(WSClient wsClient) {
|
public Play25CallFactory(WSClient wsClient) {
|
||||||
this.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,
|
public Play25CallFactory(WSClient wsClient, Map<String, String> extraHeaders,
|
||||||
List<Pair> extraQueryParams) {
|
List<Pair> extraQueryParams) {
|
||||||
this.wsClient = wsClient;
|
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 final WSClient wsClient;
|
||||||
private WSRequest wsRequest;
|
private WSRequest wsRequest;
|
||||||
|
private List<WSRequestFilter> filters;
|
||||||
|
|
||||||
private final Request request;
|
private final Request request;
|
||||||
|
|
||||||
public PlayWSCall(WSClient wsClient, Request request) {
|
public PlayWSCall(WSClient wsClient, List<WSRequestFilter> filters, Request request) {
|
||||||
this.wsClient = wsClient;
|
this.wsClient = wsClient;
|
||||||
this.request = request;
|
this.request = request;
|
||||||
|
this.filters = filters;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -126,6 +137,7 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
|
|||||||
if (request.body() != null) {
|
if (request.body() != null) {
|
||||||
addBody(wsRequest);
|
addBody(wsRequest);
|
||||||
}
|
}
|
||||||
|
filters.stream().forEach(f -> wsRequest.withRequestFilter(f));
|
||||||
|
|
||||||
return wsRequest.execute(request.method());
|
return wsRequest.execute(request.method());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user