kbendick commented on a change in pull request #4245:
URL: https://github.com/apache/iceberg/pull/4245#discussion_r824035564



##########
File path: core/src/main/java/org/apache/iceberg/rest/HttpRESTClient.java
##########
@@ -0,0 +1,288 @@
+/*
+ * 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;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Map;
+import java.util.Optional;
+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.HttpHeaders;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.Method;
+import org.apache.hc.core5.http.ParseException;
+import org.apache.hc.core5.http.impl.EnglishReasonPhraseCatalog;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.io.CloseMode;
+import org.apache.iceberg.exceptions.RESTException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.rest.responses.ErrorResponse;
+import org.apache.iceberg.rest.responses.ErrorResponseParser;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * An HttpClient for usage with the REST catalog.
+ */
+public class HttpRESTClient implements RESTClient {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(HttpRESTClient.class);
+
+  private final String uri;
+  private final CloseableHttpClient httpClient;
+  private final ObjectMapper mapper;
+  private final Map<String, String> additionalHeaders;
+  private final Consumer<HttpUriRequest> requestInterceptor;
+
+  private HttpRESTClient(
+      String uri,
+      CloseableHttpClient httpClient,
+      ObjectMapper mapper,
+      Map<String, String> additionalHeaders,
+      Consumer<HttpUriRequest> requestInterceptor) {
+    this.uri = uri;
+    this.httpClient = httpClient != null ? httpClient : 
HttpClients.createDefault();
+    this.mapper = mapper != null ? mapper : new ObjectMapper();
+    this.additionalHeaders = additionalHeaders != null ? additionalHeaders : 
ImmutableMap.of();
+    this.requestInterceptor = requestInterceptor;
+  }
+
+  private static Optional<String> 
extractResponseBodyAsString(CloseableHttpResponse response, Class<?> 
responseType) {
+    try {
+      if (response.getEntity() == null) {
+        return Optional.empty();
+      }
+      return Optional.ofNullable(EntityUtils.toString(response.getEntity(), 
"UTF-8"));
+    } catch (IOException | ParseException e) {
+      if (responseType != null) {
+        LOG.warn("Failed to convert HTTP response body to string", e);
+      }
+      return Optional.empty();
+    }
+  }
+
+  // Per the spec, the only currently defined / used "success" responses are 
200 and 202.
+  private static boolean isSuccessful(CloseableHttpResponse response) {
+    int code = response.getCode();
+    return code ==  HttpStatus.SC_OK || code == HttpStatus.SC_ACCEPTED;
+  }
+
+  private static ErrorResponse buildDefaultErrorResponse(CloseableHttpResponse 
response) {
+    String responseReason = response.getReasonPhrase();
+    String message =
+        responseReason != null && !responseReason.isEmpty() ? responseReason :
+            EnglishReasonPhraseCatalog.INSTANCE.getReason(response.getCode(), 
null /* ignored */);
+    String type = "ClientGeneratedErrorResponse - No error response body could 
be parsed";
+    return ErrorResponse.builder()
+        .responseCode(response.getCode())
+        .withMessage(message)
+        .withType(type)
+        .build();
+  }
+
+  private static ErrorResponse parseOrCreateErrorResponse(
+      CloseableHttpResponse response, Optional<String> responseBody, Method 
method, Class<?> responseType) {
+    try {
+      return responseBody.map(ErrorResponseParser::fromJson).orElseGet(() -> 
buildDefaultErrorResponse(response));
+    } catch (Exception e) {

Review comment:
       The benefit of still calling `buildDefaultError` is that it uses the 
response code from the response still, so the error handlers _should_ still 
pick up on things like ELB 5xx or client connection disconnects or something 
that don't even get far enough to generate a formatted `ErrorResponse`.




-- 
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]

Reply via email to