use of com.stanfy.enroscar.content.loader.ResponseData 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);
}
}
use of com.stanfy.enroscar.content.loader.ResponseData in project enroscar by stanfy.
the class LoaderSetTest method shouldWaitFor3Requests.
@Ignore
@Test
public void shouldWaitFor3Requests() throws Throwable {
// enqueue
getWebServer().enqueue(new MockResponse().setBody("R1"));
getWebServer().enqueue(new MockResponse().setBody("R2"));
getWebServer().enqueue(new MockResponse().setBody("R3"));
final URL url = getWebServer().getUrl("/");
final Fragment fragment = createFragment();
final LoaderManager loaderManager = fragment.getLoaderManager();
assertThat(loaderManager).isNotNull();
final CountDownLatch waiter = new CountDownLatch(1);
// describe loader
final LoaderSet set = LoaderSet.build(getApplication()).withManager(loaderManager).withCallbacks(new LoaderSet.SetCallbacksAdapter<ResponseData<String>>() {
@Override
public Loader<ResponseData<String>> onCreateLoader(final int id, final Bundle args) {
return new MyRequestBuilder<String>(getApplication()) {
}.setUrl(url.toString()).setFormat("string").getLoader();
}
}, 1).withCallbacks(new LoaderSet.SetCallbacksAdapter<ResponseData<String>>() {
@Override
public Loader<ResponseData<String>> onCreateLoader(final int id, final Bundle args) {
return new MyRequestBuilder<String>(getApplication()) {
}.setUrl(url.toString()).setFormat("string").getLoader();
}
}, 2, 3).create();
final LoaderSet.LoaderSetCallback callbacks = new LoaderSet.LoaderSetCallback() {
@Override
public void onLoadFinished(final Object[] data) {
Log.i("ChainTest", Arrays.toString(data));
waiter.countDown();
}
};
set.init(null, callbacks);
// TODO finish it
waitAndAssert(new Waiter<Object[]>() {
@Override
public Object[] waitForData() {
try {
waiter.await(2, TimeUnit.SECONDS);
return LoaderSetAccess.getResults(set);
} catch (final InterruptedException e) {
return null;
}
}
}, new Asserter<Object[]>() {
@SuppressWarnings("unchecked")
@Override
public void makeAssertions(final Object[] data) throws Exception {
assertThat(data.length).isEqualTo(3);
assertThat(((ResponseData<String>) data[0]).getModel()).isEqualTo("R1");
assertThat(((ResponseData<String>) data[1]).getModel()).isEqualTo("R2");
assertThat(((ResponseData<String>) data[2]).getModel()).isEqualTo("R3");
}
});
}
Aggregations