Mmuzaf commented on code in PR #2988:
URL: https://github.com/apache/cassandra/pull/2988#discussion_r1537907338


##########
src/java/org/apache/cassandra/locator/HttpServiceConnector.java:
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.cassandra.locator;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+import com.google.common.collect.ImmutableMap;
+
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.exceptions.ConfigurationException;
+
+import static java.lang.String.format;
+
+public class HttpServiceConnector
+{
+    protected final String metadataServiceUrl;
+    protected final int requestTimeoutMs;
+
+    public HttpServiceConnector(String serviceUrl, int requestTimeoutMs)
+    {
+        this.metadataServiceUrl = serviceUrl;
+        this.requestTimeoutMs = requestTimeoutMs;
+    }
+
+    public final String apiCall(String query) throws IOException
+    {
+        return apiCall(metadataServiceUrl, query, "GET", ImmutableMap.of(), 
200);
+    }
+
+    public final String apiCall(String query, Map<String, String> 
extraHeaders) throws IOException
+    {
+        return apiCall(metadataServiceUrl, query, "GET", extraHeaders, 200);
+    }
+
+    public String apiCall(String url,
+                          String query,
+                          String method,
+                          Map<String, String> extraHeaders,
+                          int expectedResponseCode) throws IOException
+    {
+        HttpURLConnection conn = null;
+        try
+        {
+            conn = (HttpURLConnection) new URL(url + query).openConnection();
+            extraHeaders.forEach(conn::setRequestProperty);
+            conn.setRequestMethod(method);
+            conn.setConnectTimeout(requestTimeoutMs);
+            if (conn.getResponseCode() != expectedResponseCode)
+                throw new HttpException(conn.getResponseCode(), 
conn.getResponseMessage());
+
+            List<String> response = new ArrayList<>();
+
+            try (BufferedReader bufferedReader = new BufferedReader(new 
InputStreamReader(conn.getInputStream())))
+            {
+                String line;
+                while ((line = bufferedReader.readLine()) != null) {
+                    response.add(line);
+                }
+            }
+
+            return 
response.stream().collect(Collectors.joining(System.lineSeparator()));

Review Comment:
   The previous version (see below), so I guess it's not a 1:1 replacement. 
   
   ```
               byte[] b = new byte[cl];
               try (DataInputStream d = new DataInputStream((InputStream) 
conn.getContent()))
               {
                   d.readFully(b);
               }
               return new String(b, UTF_8);
   ```



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