use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class HttpClientProxyTest method downloadToFileByRequestTest.
@Test
public void downloadToFileByRequestTest() throws Exception {
// given
String fileUrl = "https://resources.tvd12.com/ezy-settings-1.0.0.xsd";
DownloadRequest request = new DownloadRequest().setFileURL(fileUrl).setConnectTimeout(5000).setReadTimeout(5000).setHeaders(MultiValueMap.builder().setValue("hello", "world").build());
HttpClientProxy sut = HttpClientProxy.builder().requestQueueCapacity(1).threadPoolSize(1).build();
// when
String fileName = sut.download(request, new File("test-output/no-commit"));
// then
Asserts.assertEquals(fileName, "ezy-settings-1.0.0.xsd");
Asserts.assertTrue(new File("test-output/no-commit/ezy-settings-1.0.0.xsd").exists());
sut.close();
sut.stop();
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class HttpClient method decorateConnection.
private void decorateConnection(HttpURLConnection connection, DownloadRequest request) {
int connectTimeout = request.getReadTimeout();
int readTimeout = request.getReadTimeout();
connection.setConnectTimeout(connectTimeout > 0 ? connectTimeout : defaultConnectTimeout);
connection.setReadTimeout(readTimeout > 0 ? readTimeout : defaultReadTimeout);
MultiValueMap requestHeaders = request.getHeaders();
if (requestHeaders != null) {
Map<String, String> encodedHeaders = requestHeaders.toMap();
for (Entry<String, String> requestHeader : encodedHeaders.entrySet()) {
connection.setRequestProperty(requestHeader.getKey(), requestHeader.getValue());
}
}
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class HttpClientProxy method execute.
public void execute(Request request, RequestCallback<ResponseEntity> callback) {
EzyFuture future = new RequestFutureTask(callback);
futures.addFuture(request, future);
try {
addRequest(request);
} catch (Exception e) {
futures.removeFuture(request);
throw e;
}
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class HttpClientProxy method handleRequests.
protected void handleRequests() {
Request request = null;
EzyFuture future = null;
Exception exception = null;
ResponseEntity response = null;
try {
request = requestQueue.take();
future = futures.removeFuture(request);
response = client.request(request);
} catch (Exception e) {
exception = e;
}
try {
if (future != null) {
if (exception != null) {
future.setException(exception);
} else {
future.setResult(response);
}
} else {
if (exception != null) {
logger.info("handled request: {} with exception, but there is no future", request, exception);
} else {
logger.info("handled request: {} with response: {}, but there is no future", request, response);
}
}
} catch (Throwable e) {
logger.info("handle request result error", e);
}
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class HttpClientTest method postMethodPaymentRequired.
@Test
public void postMethodPaymentRequired() {
// given
HttpClient sut = HttpClient.builder().build();
PostRequest request = new PostRequest().setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL(URI.create("http://127.0.0.1:18081/402"));
// when
Throwable e = Asserts.assertThrows(() -> sut.call(request));
// then
Asserts.assertThat(e).isEqualsType(HttpPaymentRequiredException.class);
}
Aggregations