Search in sources :

Example 1 with RequestResult

use of com.stanfy.enroscar.rest.RequestMethod.RequestResult in project enroscar by stanfy.

the class DirectRequestExecutor method performRequest.

@Override
public void performRequest(final RequestDescription description) {
    final RequestMethod requestMethod = config.getRequestMethod(description);
    // final ResponseModelConverter converter = config.getResponseModelConverter(description);
    ContentAnalyzer<?, ?> analyzer = null;
    final String analyzerBeanName = description.getContentAnalyzer();
    if (analyzerBeanName != null) {
        analyzer = BeansManager.get(context).getContainer().getBean(analyzerBeanName, ContentAnalyzer.class);
        if (analyzer == null) {
            throw new RuntimeException("ContentAnalyzer bean with name " + analyzerBeanName + " is not declared.");
        }
    }
    if (Utils.isDebugRest(context)) {
        Log.d(TAG, "Process request id " + description.getId());
    }
    hooks.beforeRequestProcessingStarted(description, requestMethod);
    boolean passedToAnalyzer = false;
    try {
        // execute request method
        final RequestResult res = requestMethod.perform(context, description);
        // check for cancel
        if (description.isCanceled()) {
            hooks.onRequestCancel(description, null);
            return;
        }
        // process results
        ResponseData<?> response = null;
        // check for cancel
        if (description.isCanceled()) {
            hooks.onRequestCancel(description, response);
            return;
        }
        // analyze
        passedToAnalyzer = true;
        if (analyzer != null) {
            response = analyze(context, analyzer, response, description);
            if (response == null) {
                throw new IllegalStateException("Analyzer " + analyzer + " returned null response");
            }
        }
        // report results
        if (response.isSuccessful()) {
            hooks.onRequestSuccess(description, response);
        } else {
            Log.e(TAG, "Server error: " + response.getErrorCode() + ", " + response.getMessage());
            hooks.onRequestError(description, response);
        }
    } catch (final Exception e) {
        // RequestMethodException e) {
        Log.e(TAG, "Request method error while processing " + description, e);
        // converter.toResponseData(description, e);
        ResponseData<?> data = null;
        if (analyzer != null && !passedToAnalyzer) {
        // try {
        // data = analyze(context, analyzer, data, description);
        // } catch (RequestMethodException analyzerException) {
        // Log.e(TAG, "Analyzer exception analyzerName=" + analyzerBeanName + " for " + description, analyzerException);
        // // repack data to use the current exception
        // data = converter.toResponseData(description, analyzerException);
        // }
        }
        hooks.onRequestError(description, data);
    } finally {
        hooks.afterRequestProcessingFinished(description, requestMethod);
    }
}
Also used : RequestResult(com.stanfy.enroscar.rest.RequestMethod.RequestResult) RequestMethod(com.stanfy.enroscar.rest.RequestMethod) ContentAnalyzer(com.stanfy.enroscar.rest.response.ContentAnalyzer) ResponseData(com.stanfy.enroscar.content.loader.ResponseData)

Aggregations

ResponseData (com.stanfy.enroscar.content.loader.ResponseData)1 RequestMethod (com.stanfy.enroscar.rest.RequestMethod)1 RequestResult (com.stanfy.enroscar.rest.RequestMethod.RequestResult)1 ContentAnalyzer (com.stanfy.enroscar.rest.response.ContentAnalyzer)1