Fokko commented on code in PR #5698:
URL: https://github.com/apache/iceberg/pull/5698#discussion_r962360787


##########
core/src/main/java/org/apache/iceberg/rest/responses/OAuthErrorResponseParser.java:
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.responses;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.util.JsonUtil;
+
+public class OAuthErrorResponseParser {
+
+  private OAuthErrorResponseParser() {}
+
+  private static final String ERROR = "error";
+  private static final String ERROR_DESCRIPTION = "error_description";
+  private static final String ERROR_URI = "error_uri";
+
+  public static String toJson(OAuthErrorResponse errorResponse) {
+    return toJson(errorResponse, false);
+  }
+
+  public static String toJson(OAuthErrorResponse errorResponse, boolean 
pretty) {
+    try {
+      StringWriter writer = new StringWriter();
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      if (pretty) {
+        generator.useDefaultPrettyPrinter();
+      }
+      toJson(errorResponse, generator);
+      generator.flush();
+      return writer.toString();
+    } catch (IOException e) {
+      throw new UncheckedIOException(
+          String.format("Failed to write error response json for: %s", 
errorResponse), e);
+    }
+  }
+
+  public static void toJson(OAuthErrorResponse errorResponse, JsonGenerator 
generator)
+      throws IOException {
+    generator.writeStartObject();
+
+    generator.writeStringField(ERROR, errorResponse.error());
+    generator.writeStringField(ERROR_DESCRIPTION, 
errorResponse.errorDescription());
+    generator.writeStringField(ERROR_URI, errorResponse.errorUri());
+
+    generator.writeEndObject();
+  }
+
+  /**
+   * Read OAuthErrorResponse from a JSON string.
+   *
+   * @param json a JSON string of an OAuthErrorResponse
+   * @return an OAuthErrorResponse object
+   */
+  public static OAuthErrorResponse fromJson(String json) {
+    try {
+      return fromJson(JsonUtil.mapper().readValue(json, JsonNode.class));
+    } catch (IOException e) {
+      throw new UncheckedIOException("Failed to read JSON string: " + json, e);
+    }
+  }
+
+  public static OAuthErrorResponse fromJson(JsonNode jsonNode) {
+    Preconditions.checkArgument(
+        jsonNode != null && jsonNode.isObject(),
+        "Cannot parse error response from non-object value: %s",
+        jsonNode);
+    Preconditions.checkArgument(jsonNode.has(ERROR), "Cannot parse missing 
field: error");
+    String error = JsonUtil.getString(ERROR, jsonNode);
+    String errorDescription = JsonUtil.getStringOrNull(ERROR_DESCRIPTION, 
jsonNode);

Review Comment:
   👍🏻 



##########
core/src/test/java/org/apache/iceberg/rest/responses/TestOAuthErrorResponseParser.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.responses;
+
+import org.apache.iceberg.rest.auth.OAuth2Properties;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestOAuthErrorResponseParser {
+
+  @Test
+  public void testOAuthErrorResponseToJson() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String description = "Credentials given were invalid";
+    String uri = "http://iceberg.apache.org";;
+    String json =
+        String.format(
+            
"{\"error\":\"%s\",\"error_description\":\"%s\",\"error_uri\":\"%s\"}",

Review Comment:
   Only the error is required according to the spec: 
https://github.com/apache/iceberg/blob/master/open-api/rest-catalog-open-api.yaml#L1675-L1695
   
   Maybe good to test those cases as well.



##########
open-api/rest-catalog-open-api.yaml:
##########
@@ -173,11 +173,7 @@ paths:
         400:
           $ref: '#/components/responses/OAuthErrorResponse'
         401:
-          $ref: '#/components/responses/UnauthorizedResponse'
-        503:

Review Comment:
   Shouldn't we still include 5xx responses? The oauth endpoint can also be 
unavailable.



##########
core/src/main/java/org/apache/iceberg/rest/responses/OAuthErrorResponseParser.java:
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.responses;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.util.JsonUtil;
+
+public class OAuthErrorResponseParser {
+
+  private OAuthErrorResponseParser() {}
+
+  private static final String ERROR = "error";
+  private static final String ERROR_DESCRIPTION = "error_description";
+  private static final String ERROR_URI = "error_uri";
+
+  public static String toJson(OAuthErrorResponse errorResponse) {
+    return toJson(errorResponse, false);
+  }
+
+  public static String toJson(OAuthErrorResponse errorResponse, boolean 
pretty) {
+    try {

Review Comment:
   I know that the StringWriter close is a no-op, but maybe nice to close the 
resources:
   ```java
     public static String toJson(OAuthErrorResponse errorResponse, boolean 
pretty) {
       try (StringWriter writer = new StringWriter();
            JsonGenerator generator = 
JsonUtil.factory().createGenerator(writer)) {
         if (pretty) {
           generator.useDefaultPrettyPrinter();
         }
         toJson(errorResponse, generator);
         generator.flush();
         return writer.toString();
       } catch (IOException e) {
         throw new UncheckedIOException(
             String.format("Failed to write error response json for: %s", 
errorResponse), e);
       }
     }
   ```



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