Search in sources :

Example 1 with OkHttp3ForestResponseFactory

use of com.dtflys.forest.backend.okhttp3.response.OkHttp3ForestResponseFactory 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);
// }
}
Also used : Response(okhttp3.Response) ForestResponse(com.dtflys.forest.http.ForestResponse) Call(okhttp3.Call) ForestResponse(com.dtflys.forest.http.ForestResponse) OkHttpClient(okhttp3.OkHttpClient) ForestRequest(com.dtflys.forest.http.ForestRequest) Request(okhttp3.Request) Date(java.util.Date) ForestRetryException(com.dtflys.forest.exceptions.ForestRetryException) URLBuilder(com.dtflys.forest.backend.url.URLBuilder) QueryableURLBuilder(com.dtflys.forest.backend.url.QueryableURLBuilder) OkHttp3ForestResponseFactory(com.dtflys.forest.backend.okhttp3.response.OkHttp3ForestResponseFactory)

Aggregations

OkHttp3ForestResponseFactory (com.dtflys.forest.backend.okhttp3.response.OkHttp3ForestResponseFactory)1 QueryableURLBuilder (com.dtflys.forest.backend.url.QueryableURLBuilder)1 URLBuilder (com.dtflys.forest.backend.url.URLBuilder)1 ForestRetryException (com.dtflys.forest.exceptions.ForestRetryException)1 ForestRequest (com.dtflys.forest.http.ForestRequest)1 ForestResponse (com.dtflys.forest.http.ForestResponse)1 Date (java.util.Date)1 Call (okhttp3.Call)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1