Search in sources :

Example 1 with RequestMethod

use of com.stanfy.enroscar.rest.RequestMethod 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)

Example 2 with RequestMethod

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

the class AbstractMockServerTest method whenBeansConfigured.

@Override
protected void whenBeansConfigured() {
    super.whenBeansConfigured();
    BeansManager.get(getApplication()).getContainer().getBean(RemoteServerApiConfiguration.class).setDefaultRequestMethod(new RequestMethod() {

        @Override
        protected void before(final Context systemContext, final RequestDescription description) {
        // do not use TrafficStats
        }

        @Override
        protected void after(final Context systemContext, final RequestDescription description) {
        // do not use TrafficStats
        }
    });
    if (config == null) {
        config = BeanUtils.getAnnotationFromHierarchy(getClass(), EnroscarNetConfig.class);
    }
    if (config != null && config.connectionEngineRequired()) {
        final Config config = EnroscarConnectionsEngine.config();
        configureConnectionsEngine(config);
        config.treatFileScheme(false);
        config.setup(Robolectric.application);
    }
    configureServiceBind();
}
Also used : Context(android.content.Context) EnroscarNetConfig(com.stanfy.enroscar.test.EnroscarNetConfig) RemoteServerApiConfiguration(com.stanfy.enroscar.rest.RemoteServerApiConfiguration) RequestMethod(com.stanfy.enroscar.rest.RequestMethod) Config(com.stanfy.enroscar.net.EnroscarConnectionsEngine.Config) EnroscarNetConfig(com.stanfy.enroscar.test.EnroscarNetConfig) RequestDescription(com.stanfy.enroscar.net.operation.RequestDescription)

Aggregations

RequestMethod (com.stanfy.enroscar.rest.RequestMethod)2 Context (android.content.Context)1 ResponseData (com.stanfy.enroscar.content.loader.ResponseData)1 Config (com.stanfy.enroscar.net.EnroscarConnectionsEngine.Config)1 RequestDescription (com.stanfy.enroscar.net.operation.RequestDescription)1 RemoteServerApiConfiguration (com.stanfy.enroscar.rest.RemoteServerApiConfiguration)1 RequestResult (com.stanfy.enroscar.rest.RequestMethod.RequestResult)1 ContentAnalyzer (com.stanfy.enroscar.rest.response.ContentAnalyzer)1 EnroscarNetConfig (com.stanfy.enroscar.test.EnroscarNetConfig)1