WebClient 주석 추가

master
mjkhan21 1 year ago
parent 6994a8b205
commit 9f629a49fc

@ -21,7 +21,54 @@ import java.util.function.Consumer;
import cokr.xit.foundation.Assert; import cokr.xit.foundation.Assert;
import cokr.xit.foundation.data.JSON; import cokr.xit.foundation.data.JSON;
/** /** http .
* <p>WebClient {@link #get(Consumer) GET}, {@link #post(Consumer) POST} . {@link Request }
* <ul><li> / </li>
* <li> / / JSON</li>
* </ul>
* .<br />
* .
* <ul><li>
* <pre><code> // 텍스트
* try {
* HttpResponse{@code <String>} hresp = new WebClient().get(req ->
* req.uri("http://xit.co.kr")
* );
* System.out.println(hresp.body());
* } catch (Exception e) {
* e.printStackTrace();
* }
* // 파일 다운로드
* try {
* HttpResponse{@code <InputStream>} hresp2 = new WebClient().get(req ->
* req.uri("http://www.xit.co.kr/images/project/homepage/mainVisual.png")
* .download(true)
* );
* InputStream input = hresp2.body(); // -> 파일 저장
* } catch (Exception e) {
* e.printStackTrace();
* } </code></pre>
* </li>
* <li>
* <pre><code> // 텍스트
* new WebClient().get(req ->
* req.uri("http://xit.co.kr")
* <b>.async(true)</b>
* <b>.onResponse</b>(hresp -> System.out.println(hresp.body())
* <b>.onError</b>(e -> e.printStackTrace())
* );
* // 파일 다운로드
* new WebClient().get(req ->
* req.uri("http://www.xit.co.kr/images/project/homepage/mainVisual.png")
* <b>.async(true)</b>
* .download(true)
* <b>.onDownload</b>(hresp -> {
* InputStream input = hresp.body(); // -> 파일 저장
* })
* <b>.onError</b>(e -> e.printStackTrace())
* ); </code></pre>
* </li>
* </ul>
* @author mjkhan * @author mjkhan
*/ */
public class WebClient { public class WebClient {
@ -37,26 +84,46 @@ public class WebClient {
private HttpClient.Redirect followRedirects = HttpClient.Redirect.NORMAL; private HttpClient.Redirect followRedirects = HttpClient.Redirect.NORMAL;
/**http . HTTP/2
* @param version http
* @return WebClient
*/
public WebClient version(HttpClient.Version version) { public WebClient version(HttpClient.Version version) {
this.version = version; this.version = version;
return this; return this;
} }
/** () . 30
* @param seconds ()
* @return WebClient
*/
public WebClient timeout(int seconds) { public WebClient timeout(int seconds) {
this.timeout = Duration.ofSeconds(seconds); this.timeout = Duration.ofSeconds(seconds);
return this; return this;
} }
/** Authenticator .
* @param authenticator Authenticator
* @return WebClient
*/
public WebClient authenticator(Authenticator authenticator) { public WebClient authenticator(Authenticator authenticator) {
this.authenticator = authenticator; this.authenticator = authenticator;
return this; return this;
} }
/** proxy .
* @param proxy
* @return WebClient
*/
public WebClient proxy(ProxySelector proxy) { public WebClient proxy(ProxySelector proxy) {
this.proxy = proxy; this.proxy = proxy;
return this; return this;
} }
/** redirect . HttpClient.Redirect.NORMAL.
* @param redirect redirect
* @return WebClient
*/
public WebClient followRedirects(HttpClient.Redirect redirect) { public WebClient followRedirects(HttpClient.Redirect redirect) {
this.followRedirects = redirect; this.followRedirects = redirect;
return this; return this;
@ -76,20 +143,25 @@ public class WebClient {
return builder.build(); return builder.build();
} }
/**"GET" .
* @param <T> body
* @param configurer "GET"
* @return http
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> HttpResponse<T> get(Consumer<Config> configurer) { public <T> HttpResponse<T> get(Consumer<Request> configurer) {
Config conf = new Config(); Request req = new Request();
configurer.accept(conf); configurer.accept(req);
HttpRequest hreq = conf.get(); HttpRequest hreq = req.get();
try { try {
if (conf.download) if (req.download)
return (HttpResponse<T>)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofInputStream(), conf.downloadHandler); return (HttpResponse<T>)handleRequest(req.async, hreq, HttpResponse.BodyHandlers.ofInputStream(), req.downloadHandler);
else else
return (HttpResponse<T>)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofString(), conf.textHandler); return (HttpResponse<T>)handleRequest(req.async, hreq, HttpResponse.BodyHandlers.ofString(), req.textHandler);
} catch (Throwable e) { } catch (Throwable e) {
if (conf.async) { if (req.async) {
conf.errorHandler.accept(Assert.rootCause(e)); req.errorHandler.accept(Assert.rootCause(e));
return null; return null;
} else } else
throw Assert.runtimeException(e); throw Assert.runtimeException(e);
@ -109,26 +181,35 @@ public class WebClient {
} }
} }
/**"POST" .
* @param <T> body
* @param configurer "POST"
* @return http
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> HttpResponse<T> post(Consumer<Config> configurer) { public <T> HttpResponse<T> post(Consumer<Request> configurer) {
Config conf = new Config(); Request req = new Request();
configurer.accept(conf); configurer.accept(req);
HttpRequest hreq = conf.post(); HttpRequest hreq = req.post();
try { try {
if (conf.download) if (req.download)
return (HttpResponse<T>)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofInputStream(), conf.downloadHandler); return (HttpResponse<T>)handleRequest(req.async, hreq, HttpResponse.BodyHandlers.ofInputStream(), req.downloadHandler);
else else
return (HttpResponse<T>)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofString(), conf.textHandler); return (HttpResponse<T>)handleRequest(req.async, hreq, HttpResponse.BodyHandlers.ofString(), req.textHandler);
} catch (Throwable e) { } catch (Throwable e) {
if (conf.async) { if (req.async) {
conf.errorHandler.accept(Assert.rootCause(e)); req.errorHandler.accept(Assert.rootCause(e));
return null; return null;
} else } else
throw Assert.runtimeException(e); throw Assert.runtimeException(e);
} }
} }
/** , .
* @param inputStream
* @param path
*/
public void write(InputStream inputStream, String path) { public void write(InputStream inputStream, String path) {
try ( try (
InputStream in = inputStream; InputStream in = inputStream;
@ -140,7 +221,23 @@ public class WebClient {
} }
} }
public static class Config { /** http .
* <p>Request .
* <ul><li>{@link #uri(String) uri}: uri</li>
* <li>{@link #json(boolean) json}: JSON </li>
* <li>{@link #async(boolean) async}: </li>
* <li>{@link #download(boolean) download}: </li>
* <li>{@link #header(String, String) header}: http </li>
* <li>{@link #data(String, Object) data}: </li>
* </ul>
* .
* <ul><li>{@link #onResponse(Consumer) onResponse}: </li>
* <li>{@link #onDownload(Consumer) onDownload}: </li>
* <li>{@link #onError(Throwable) onError}: </li>
* </ul>
* @author mjkhan
*/
public static class Request {
private String uri; private String uri;
private boolean private boolean
async, async,
@ -160,56 +257,107 @@ public class WebClient {
t.printStackTrace(System.err); t.printStackTrace(System.err);
}; };
public Config header(String key, String value) { /** .
* @param key
* @param value
* @return Request
*/
public Request header(String key, String value) {
if (headers == null) if (headers == null)
headers = new LinkedHashMap<>(); headers = new LinkedHashMap<>();
headers.put(key, value); headers.put(key, value);
return this; return this;
} }
public Config uri(String uri) { /** uri .
* @param uri uri
* @return Request
*/
public Request uri(String uri) {
this.uri = uri; this.uri = uri;
return this; return this;
} }
public Config charset(Charset charset) { /** . UTF-8.
* @param charset
* @return Request
*/
public Request charset(Charset charset) {
this.charset = charset; this.charset = charset;
return this; return this;
} }
public Config async(boolean async) { /** . false().
* @param async
* <ul><li> true</li>
* <li> false</li>
* </ul>
* @return Request
*/
public Request async(boolean async) {
this.async = async; this.async = async;
return this; return this;
} }
public Config json(boolean json) { /** JSON . false.
* @param json JSON
* <ul><li> JSON true</li>
* <li> false</li>
* </ul>
* @return Request
*/
public Request json(boolean json) {
this.json = json; this.json = json;
return this; return this;
} }
public Config download(boolean download) { /** . false().
* @param download
* <ul><li> file true</li>
* <li> false</li>
* </ul>
* @return Request
*/
public Request download(boolean download) {
this.download = download; this.download = download;
return this; return this;
} }
public Config data(String key, Object value) { /** .
* @param key ()
* @param value
* @return Request
*/
public Request data(String key, Object value) {
if (data == null) if (data == null)
data = new LinkedHashMap<>(); data = new LinkedHashMap<>();
data.put(key, value); data.put(key, value);
return this; return this;
} }
public Config onResponse(Consumer<HttpResponse<String>> handler) { /** .
* @param handler
* @return Request
*/
public Request onResponse(Consumer<HttpResponse<String>> handler) {
textHandler = handler; textHandler = handler;
return this; return this;
} }
public Config onDownload(Consumer<HttpResponse<InputStream>> handler) { /** .
* @param handler
* @return Request
*/
public Request onDownload(Consumer<HttpResponse<InputStream>> handler) {
downloadHandler = handler; downloadHandler = handler;
return this; return this;
} }
public Config onError(Consumer<Throwable> handler) { /** .
* @param handler
* @return Request
*/
public Request onError(Consumer<Throwable> handler) {
errorHandler = handler; errorHandler = handler;
return this; return this;
} }

Loading…
Cancel
Save