rdblue commented on a change in pull request #3424: URL: https://github.com/apache/iceberg/pull/3424#discussion_r739882478
########## File path: core/src/main/java/org/apache/iceberg/rest/http/HttpClient.java ########## @@ -0,0 +1,235 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iceberg.rest.http; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.net.URI; +import java.util.Map; +import java.util.function.Consumer; +import javax.annotation.Nullable; +import org.apache.hc.client5.http.classic.methods.HttpUriRequest; +import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.Method; +import org.apache.hc.core5.http.io.entity.StringEntity; +import org.apache.hc.core5.io.CloseMode; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.rest.RestClient; +import org.apache.iceberg.rest.UncheckedRestException; + +/** + * An HttpClient that can be used for the RestClient (eventually). + * TODO - Extract out top for all of this so that other implementations are easy to implement (e.g. gRPC). + */ +public class HttpClient implements RestClient { + private final String baseUrl; + + // Need their own Builders? + private final CloseableHttpClient httpClient; + private final Consumer<CloseableHttpResponse> defaultErrorHandler; + private final ObjectMapper mapper; + private Map<String, String> additionalHeaders = Maps.newHashMap(); + private Consumer<HttpUriRequest> requestInterceptor = (r) -> { }; + + private HttpClient( + String baseUrl, CloseableHttpClient httpClient, ObjectMapper mapper, + Map<String, String> additionalHeaders, Consumer<HttpUriRequest> requestIntercepter, + Consumer<CloseableHttpResponse> defaultErrorHandler) { + this.baseUrl = baseUrl; + this.httpClient = httpClient != null ? httpClient : HttpClients.createDefault(); + this.mapper = mapper != null ? mapper : new ObjectMapper(); + this.additionalHeaders = additionalHeaders != null ? additionalHeaders : Maps.newHashMap(); + // TODO - Best way to handle Preconditions here. + this.requestInterceptor = requestIntercepter; + this.defaultErrorHandler = defaultErrorHandler; + } + + public static Builder builder() { + return new Builder(); + } + + /** + * Method to execute an HTTP request and process the corresponding response. + * + * @param method - HTTP method, such as GET, POST, HEAD, etc. + * @param path - URL path to send the request to + * @param body - Contents of the request body. + * @param responseType - Class of the Response type. Needs to have serializer registered with ObjectMapper + * @param errorHandler - Error handler delegated for HTTP responses which handles server error responses + * @param <T> - Class type of the response for deserialization. Must be registered with the ObjectMapper. + * @return The Response enttity, parsed and converted to its type T + * @throws UncheckedIOException - Shouldn't throw this as the requestInterceptor should handle expected cases. + * @throws UncheckedRestException - Wraps exceptions to avoid having to use checked exceptions everywhere. + */ + @Nullable + public <T> T execute( + Method method, String path, Object body, Class<T> responseType, + Consumer<CloseableHttpResponse> errorHandler) { + HttpUriRequestBase request = new HttpUriRequestBase(method.name(), URI.create(String.format("%s/%s", baseUrl, + path))); + addRequestHeaders(request); + + if (body != null) { + try { + request.setEntity(new StringEntity(mapper.writeValueAsString(body))); + } catch (JsonProcessingException e) { + throw new UncheckedRestException(e, "Failed to write request body"); + } + } + + requestInterceptor.accept(request); + + try (CloseableHttpResponse response = httpClient.execute(request)) { + if (response.getCode() != HttpStatus.SC_OK) { + errorHandler.accept(response); + } + + if (responseType == null || response.getEntity() == null) { + return null; + } + + HttpEntity entity = response.getEntity(); + return mapper.readValue(entity.getContent(), responseType); + } catch (IOException e) { + throw new UncheckedIOException(e); + } catch (Exception e) { Review comment: Something throws `Exception`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
