use of com.dtflys.forest.backend.url.URLBuilder in project forest by dromara.
the class OkHttp3Executor method execute.
public void execute(final LifeCycleHandler lifeCycleHandler, int retryCount) {
OkHttpClient okHttpClient = getClient(request, lifeCycleHandler);
URLBuilder urlBuilder = URL_BUILDER;
String url = urlBuilder.buildUrl(request);
Request.Builder builder = new Request.Builder().url(url);
prepareHeaders(builder);
prepareMethodAndBody(builder, lifeCycleHandler);
final Request okRequest = builder.build();
Call call = okHttpClient.newCall(okRequest);
final OkHttp3ForestResponseFactory factory = new OkHttp3ForestResponseFactory();
logRequest(retryCount, okRequest, okHttpClient);
Date startDate = new Date();
Response okResponse = null;
ForestResponse response = null;
try {
okResponse = call.execute();
} catch (Throwable e) {
response = factory.createResponse(request, null, lifeCycleHandler, e, startDate);
ForestRetryException retryException = new ForestRetryException(e, request, request.getMaxRetryCount(), retryCount);
try {
request.canRetry(response, retryException);
} catch (Throwable throwable) {
response = factory.createResponse(request, null, lifeCycleHandler, throwable, startDate);
logResponse(response);
lifeCycleHandler.handleSyncWithException(request, response, throwable);
return;
}
response = factory.createResponse(request, null, lifeCycleHandler, e, startDate);
logResponse(response);
execute(lifeCycleHandler, retryCount + 1);
return;
} finally {
if (response == null) {
response = factory.createResponse(request, okResponse, lifeCycleHandler, null, startDate);
}
logResponse(response);
}
// 是否重试
ForestRetryException retryEx = request.canRetry(response);
if (retryEx != null && retryEx.isNeedRetry() && !retryEx.isMaxRetryCountReached()) {
execute(lifeCycleHandler, retryCount + 1);
return;
}
// 验证响应
if (response.isError()) {
retryOrDoError(response, okResponse, null, lifeCycleHandler, retryCount);
return;
}
okHttp3ResponseHandler.handleSync(okResponse, response);
// }
}
Aggregations