Skip to content

Instantly share code, notes, and snippets.

@jaceshim
Created September 3, 2021 08:50
Show Gist options
  • Save jaceshim/9db2f148d8d0a18c40256af129c9ef26 to your computer and use it in GitHub Desktop.
Save jaceshim/9db2f148d8d0a18c40256af129c9ef26 to your computer and use it in GitHub Desktop.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Map;
public final class HttpUtils {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(HttpUtils.class);
private static final HttpClient httpClient;
static {
httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1).build();
}
private HttpUtils() {
throw new UnsupportedOperationException("Unable to create instance");
}
public static String get(String uri) {
return get(uri, null, null, String.class);
}
public static <T> T get(String uri, Class<T> clazz) {
return get(uri, null, null, clazz);
}
public static <T> T getWithParams(String uri, Map<String, Object> params, Class<T> clazz) {
return get(uri, null, params, clazz);
}
public static <T> T getWithHeaders(String uri, Map<String, String> headers, Class<T> clazz) {
return get(uri, headers, null, clazz);
}
public static <T> T get(String uri, Map<String, String> headers, Map<String, Object> params, Class<T> clazz) {
String buildUri = buildUri(uri, params);
final HttpRequest.Builder requestBuilder = HttpRequest
.newBuilder()
.GET()
.uri(URI.create(buildUri));
if (headers != null) {
headers.forEach((key, value) -> requestBuilder.setHeader(key, value));
}
return send(requestBuilder.build(), clazz);
}
private static String buildUri(String uri, Map<String, Object> params) {
if (params == null || params.isEmpty()) {
return uri;
}
StringBuilder buff = new StringBuilder(uri + "?");
params.forEach((key, value) -> {
buff.append(key).append("=").append(value).append("&");
});
return buff.toString();
}
private static <T> T send(HttpRequest request, Class<T> tClass) {
try {
return httpClient.send(request, new JsonBodyHandler<>(tClass)).body();
} catch (Exception e) {
log.error("Nbase Arc Seed address 조회 에러 : {}", e.getMessage(), e);
return null;
}
}
/**
* Response Json을 W 타입으로 변환한다.
* @param <W>
*/
private static class JsonBodyHandler<W> implements HttpResponse.BodyHandler<W> {
private Class<W> clazz;
public JsonBodyHandler(Class<W> clazz) {
this.clazz = clazz;
}
@Override
public HttpResponse.BodySubscriber<W> apply(HttpResponse.ResponseInfo responseInfo) {
return asJSON(clazz);
}
private <T> HttpResponse.BodySubscriber<T> asJSON(Class<T> targetType) {
HttpResponse.BodySubscriber<String> upstream = HttpResponse.BodySubscribers.ofString(
StandardCharsets.UTF_8);
return HttpResponse.BodySubscribers.mapping(
upstream,
(String body) -> {
try {
return JsonUtils.fromJson(body, targetType);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment