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.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
*/
public class WebClient {
@ -37,26 +84,46 @@ public class WebClient {
private HttpClient.Redirect followRedirects = HttpClient.Redirect.NORMAL;
/**http . HTTP/2
* @param version http
* @return WebClient
*/
public WebClient version(HttpClient.Version version) {
this.version = version;
return this;
}
/** () . 30
* @param seconds ()
* @return WebClient
*/
public WebClient timeout(int seconds) {
this.timeout = Duration.ofSeconds(seconds);
return this;
}
/** Authenticator .
* @param authenticator Authenticator
* @return WebClient
*/
public WebClient authenticator(Authenticator authenticator) {
this.authenticator = authenticator;
return this;
}
/** proxy .
* @param proxy
* @return WebClient
*/
public WebClient proxy(ProxySelector proxy) {
this.proxy = proxy;
return this;
}
/** redirect . HttpClient.Redirect.NORMAL.
* @param redirect redirect
* @return WebClient
*/
public WebClient followRedirects(HttpClient.Redirect redirect) {
this.followRedirects = redirect;
return this;
@ -76,20 +143,25 @@ public class WebClient {
return builder.build();
}
/**"GET" .
* @param <T> body
* @param configurer "GET"
* @return http
*/
@SuppressWarnings("unchecked")
public <T> HttpResponse<T> get(Consumer<Config> configurer) {
Config conf = new Config();
configurer.accept(conf);
HttpRequest hreq = conf.get();
public <T> HttpResponse<T> get(Consumer<Request> configurer) {
Request req = new Request();
configurer.accept(req);
HttpRequest hreq = req.get();
try {
if (conf.download)
return (HttpResponse<T>)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofInputStream(), conf.downloadHandler);
if (req.download)
return (HttpResponse<T>)handleRequest(req.async, hreq, HttpResponse.BodyHandlers.ofInputStream(), req.downloadHandler);
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) {
if (conf.async) {
conf.errorHandler.accept(Assert.rootCause(e));
if (req.async) {
req.errorHandler.accept(Assert.rootCause(e));
return null;
} else
throw Assert.runtimeException(e);
@ -109,26 +181,35 @@ public class WebClient {
}
}
/**"POST" .
* @param <T> body
* @param configurer "POST"
* @return http
*/
@SuppressWarnings("unchecked")
public <T> HttpResponse<T> post(Consumer<Config> configurer) {
Config conf = new Config();
configurer.accept(conf);
HttpRequest hreq = conf.post();
public <T> HttpResponse<T> post(Consumer<Request> configurer) {
Request req = new Request();
configurer.accept(req);
HttpRequest hreq = req.post();
try {
if (conf.download)
return (HttpResponse<T>)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofInputStream(), conf.downloadHandler);
if (req.download)
return (HttpResponse<T>)handleRequest(req.async, hreq, HttpResponse.BodyHandlers.ofInputStream(), req.downloadHandler);
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) {
if (conf.async) {
conf.errorHandler.accept(Assert.rootCause(e));
if (req.async) {
req.errorHandler.accept(Assert.rootCause(e));
return null;
} else
throw Assert.runtimeException(e);
}
}
/** , .
* @param inputStream
* @param path
*/
public void write(InputStream inputStream, String path) {
try (
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 boolean
async,
@ -160,56 +257,107 @@ public class WebClient {
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)
headers = new LinkedHashMap<>();
headers.put(key, value);
return this;
}
public Config uri(String uri) {
/** uri .
* @param uri uri
* @return Request
*/
public Request uri(String uri) {
this.uri = uri;
return this;
}
public Config charset(Charset charset) {
/** . UTF-8.
* @param charset
* @return Request
*/
public Request charset(Charset charset) {
this.charset = charset;
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;
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;
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;
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)
data = new LinkedHashMap<>();
data.put(key, value);
return this;
}
public Config onResponse(Consumer<HttpResponse<String>> handler) {
/** .
* @param handler
* @return Request
*/
public Request onResponse(Consumer<HttpResponse<String>> handler) {
textHandler = handler;
return this;
}
public Config onDownload(Consumer<HttpResponse<InputStream>> handler) {
/** .
* @param handler
* @return Request
*/
public Request onDownload(Consumer<HttpResponse<InputStream>> handler) {
downloadHandler = handler;
return this;
}
public Config onError(Consumer<Throwable> handler) {
/** .
* @param handler
* @return Request
*/
public Request onError(Consumer<Throwable> handler) {
errorHandler = handler;
return this;
}

Loading…
Cancel
Save