Search in sources :

Example 1 with LifeCycleHandler

use of com.dtflys.forest.handler.LifeCycleHandler in project forest by dromara.

the class ForestRequest method execute.

/**
 * 执行请求发送过程
 *
 * @param type 结果返回类型, {@link Type} 接口实例
 * @param <R> 泛型参数: 结果返回类型
 * @return 请求执行响应后返回的结果, 其为 {@link Type} 参数所指定的类型
 */
public <R> R execute(Type type) {
    LifeCycleHandler lifeCycleHandler = getLifeCycleHandler();
    MethodLifeCycleHandler<R> methodLifeCycleHandler = new MethodLifeCycleHandler<>(type, lifeCycleHandler.getOnSuccessClassGenericType());
    Object ret = execute(getBackend(), methodLifeCycleHandler);
    return (R) ret;
}
Also used : MethodLifeCycleHandler(com.dtflys.forest.reflection.MethodLifeCycleHandler) LifeCycleHandler(com.dtflys.forest.handler.LifeCycleHandler) MethodLifeCycleHandler(com.dtflys.forest.reflection.MethodLifeCycleHandler)

Example 2 with LifeCycleHandler

use of com.dtflys.forest.handler.LifeCycleHandler in project forest by dromara.

the class ForestRequest method execute.

/**
 * 执行请求发送过程
 *
 * @param clazz 结果返回类型, {@link Class} 对象
 * @param <R> 泛型参数: 结果返回类型
 * @return 请求执行响应后返回的结果, 其为 {@code Class<R>} 参数所指定的类型
 */
public <R> R execute(Class<R> clazz) {
    LifeCycleHandler lifeCycleHandler = getLifeCycleHandler();
    MethodLifeCycleHandler<R> methodLifeCycleHandler = new MethodLifeCycleHandler<>(clazz, lifeCycleHandler.getOnSuccessClassGenericType());
    Object ret = execute(getBackend(), methodLifeCycleHandler);
    return (R) ret;
}
Also used : MethodLifeCycleHandler(com.dtflys.forest.reflection.MethodLifeCycleHandler) LifeCycleHandler(com.dtflys.forest.handler.LifeCycleHandler) MethodLifeCycleHandler(com.dtflys.forest.reflection.MethodLifeCycleHandler)

Example 3 with LifeCycleHandler

use of com.dtflys.forest.handler.LifeCycleHandler in project forest by dromara.

the class TestResponseFactory method testHttpclientForestResponseFactory.

@Test
public void testHttpclientForestResponseFactory() {
    ForestRequest request = new ForestRequest(ForestConfiguration.configuration());
    Date requestTime = new Date();
    HttpclientForestResponseFactory responseFactory = new HttpclientForestResponseFactory();
    HttpResponse httpResponse = mock(HttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    when(statusLine.getStatusCode()).thenReturn(200);
    when(httpResponse.getStatusLine()).thenReturn(statusLine);
    LifeCycleHandler lifeCycleHandler = new NoneLifeCycleHandler();
    ForestResponse response = responseFactory.createResponse(request, httpResponse, lifeCycleHandler, null, requestTime);
    assertThat(response).isNotNull().extracting(ForestResponse::getStatusCode, ForestResponse::getContent, ForestResponse::getResult).contains(200, "", null);
}
Also used : StatusLine(org.apache.http.StatusLine) ForestResponse(com.dtflys.forest.http.ForestResponse) NoneLifeCycleHandler(com.dtflys.forest.reflection.NoneLifeCycleHandler) LifeCycleHandler(com.dtflys.forest.handler.LifeCycleHandler) HttpclientForestResponseFactory(com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory) HttpResponse(org.apache.http.HttpResponse) ForestRequest(com.dtflys.forest.http.ForestRequest) Date(java.util.Date) NoneLifeCycleHandler(com.dtflys.forest.reflection.NoneLifeCycleHandler) Test(org.junit.Test)

Aggregations

LifeCycleHandler (com.dtflys.forest.handler.LifeCycleHandler)3 MethodLifeCycleHandler (com.dtflys.forest.reflection.MethodLifeCycleHandler)2 HttpclientForestResponseFactory (com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory)1 ForestRequest (com.dtflys.forest.http.ForestRequest)1 ForestResponse (com.dtflys.forest.http.ForestResponse)1 NoneLifeCycleHandler (com.dtflys.forest.reflection.NoneLifeCycleHandler)1 Date (java.util.Date)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 Test (org.junit.Test)1