From 6994a8b205a22a8926f241c521b50e4421edcc6f Mon Sep 17 00:00:00 2001 From: mjkhan21 Date: Tue, 25 Jul 2023 18:28:05 +0900 Subject: [PATCH] =?UTF-8?q?sync,=20async=20=EC=B2=98=EB=A6=AC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cokr/xit/foundation/web/WebClient.java | 87 +++++++++++++------ 1 file changed, 62 insertions(+), 25 deletions(-) diff --git a/src/main/java/cokr/xit/foundation/web/WebClient.java b/src/main/java/cokr/xit/foundation/web/WebClient.java index 3e4de48..6f7628b 100644 --- a/src/main/java/cokr/xit/foundation/web/WebClient.java +++ b/src/main/java/cokr/xit/foundation/web/WebClient.java @@ -1,5 +1,7 @@ package cokr.xit.foundation.web; +import java.io.FileOutputStream; +import java.io.InputStream; import java.net.Authenticator; import java.net.ProxySelector; import java.net.URI; @@ -74,44 +76,67 @@ public class WebClient { return builder.build(); } - public void get(Consumer configurer) { + @SuppressWarnings("unchecked") + public HttpResponse get(Consumer configurer) { Config conf = new Config(); configurer.accept(conf); HttpRequest hreq = conf.get(); try { - if (!conf.async) { - HttpResponse hresp = client().send(hreq, HttpResponse.BodyHandlers.ofString()); - conf.respHandler.accept(hresp); - } else { - CompletableFuture> future = client() - .sendAsync(hreq, HttpResponse.BodyHandlers.ofString()); - HttpResponse hresp = future.get(); - conf.respHandler.accept(hresp); - } + if (conf.download) + return (HttpResponse)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofInputStream(), conf.downloadHandler); + else + return (HttpResponse)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofString(), conf.textHandler); } catch (Throwable e) { - conf.errorHandler.accept(Assert.rootCause(e)); + if (conf.async) { + conf.errorHandler.accept(Assert.rootCause(e)); + return null; + } else + throw Assert.runtimeException(e); } + } + private HttpResponse handleRequest(boolean async, HttpRequest hreq, HttpResponse.BodyHandler bodyHandler, Consumer> respHandler) throws Exception { + if (!async) { + return client().send(hreq, bodyHandler); + } else { + CompletableFuture> future = client().sendAsync(hreq, bodyHandler); + future.thenApply(resp -> { + respHandler.accept(resp); + return resp; + }); + return null; + } } - public void post(Consumer configurer) { + @SuppressWarnings("unchecked") + public HttpResponse post(Consumer configurer) { Config conf = new Config(); configurer.accept(conf); HttpRequest hreq = conf.post(); try { - if (!conf.async) { - HttpResponse hresp = client().send(hreq, HttpResponse.BodyHandlers.ofString()); - conf.respHandler.accept(hresp); - } else { - CompletableFuture> future = - client().sendAsync(hreq, HttpResponse.BodyHandlers.ofString()); - HttpResponse hresp = future.get(); - conf.respHandler.accept(hresp); - } + if (conf.download) + return (HttpResponse)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofInputStream(), conf.downloadHandler); + else + return (HttpResponse)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofString(), conf.textHandler); } catch (Throwable e) { - conf.errorHandler.accept(Assert.rootCause(e)); + if (conf.async) { + conf.errorHandler.accept(Assert.rootCause(e)); + return null; + } else + throw Assert.runtimeException(e); + } + } + + public void write(InputStream inputStream, String path) { + try ( + InputStream in = inputStream; + FileOutputStream out = new FileOutputStream(path); + ) { + inputStream.transferTo(out); + } catch (Exception e) { + throw Assert.runtimeException(e); } } @@ -119,16 +144,18 @@ public class WebClient { private String uri; private boolean async, - json; + json, + download; private Charset charset = StandardCharsets.UTF_8; private LinkedHashMap headers; private LinkedHashMap data; - private Consumer> respHandler = hresp -> { + private Consumer> textHandler = hresp -> { HttpHeaders headers = hresp.headers(); headers.map().forEach((k, v) -> System.out.println(k + " = " + v)); System.out.println("status: " + hresp.statusCode()); System.out.println(hresp.body()); }; + private Consumer> downloadHandler = hresp -> {}; private Consumer errorHandler = t -> { t.printStackTrace(System.err); }; @@ -160,6 +187,11 @@ public class WebClient { return this; } + public Config download(boolean download) { + this.download = download; + return this; + } + public Config data(String key, Object value) { if (data == null) data = new LinkedHashMap<>(); @@ -168,7 +200,12 @@ public class WebClient { } public Config onResponse(Consumer> handler) { - respHandler = handler; + textHandler = handler; + return this; + } + + public Config onDownload(Consumer> handler) { + downloadHandler = handler; return this; }