2023-10-09 14:26:33 +02:00
|
|
|
|
|
|
|
package eu.dnetlib.dhp.api;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author miriam.baglioni
|
|
|
|
* @Date 06/10/23
|
|
|
|
*/
|
|
|
|
public class QueryCommunityAPI {
|
2023-11-14 14:53:34 +01:00
|
|
|
|
2023-10-09 14:52:17 +02:00
|
|
|
private static String get(String geturl) throws IOException {
|
2023-10-09 14:26:33 +02:00
|
|
|
URL url = new URL(geturl);
|
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
|
conn.setDoOutput(true);
|
|
|
|
conn.setRequestMethod("GET");
|
|
|
|
|
|
|
|
int responseCode = conn.getResponseCode();
|
|
|
|
String body = getBody(conn);
|
|
|
|
conn.disconnect();
|
|
|
|
if (responseCode != HttpURLConnection.HTTP_OK)
|
|
|
|
throw new IOException("Unexpected code " + responseCode + body);
|
|
|
|
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
2023-11-14 14:53:34 +01:00
|
|
|
public static String communities(String baseURL) throws IOException {
|
|
|
|
|
2023-11-27 15:13:15 +01:00
|
|
|
return get(baseURL + "communities");
|
2023-11-14 14:53:34 +01:00
|
|
|
|
2023-10-09 14:26:33 +02:00
|
|
|
}
|
|
|
|
|
2023-11-27 15:13:15 +01:00
|
|
|
public static String community(String id, String baseURL) throws IOException {
|
2023-11-14 14:53:34 +01:00
|
|
|
|
2023-11-27 15:13:15 +01:00
|
|
|
return get(baseURL + id);
|
2023-11-14 14:53:34 +01:00
|
|
|
|
2023-10-09 14:26:33 +02:00
|
|
|
}
|
|
|
|
|
2023-11-27 15:13:15 +01:00
|
|
|
public static String communityDatasource(String id, String baseURL) throws IOException {
|
2023-11-14 14:53:34 +01:00
|
|
|
|
2023-11-27 15:13:15 +01:00
|
|
|
return get(baseURL + id + "/contentproviders");
|
2023-10-09 14:26:33 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-27 15:13:15 +01:00
|
|
|
public static String communityPropagationOrganization(String id, String baseURL) throws IOException {
|
2023-11-14 14:53:34 +01:00
|
|
|
|
2023-11-27 15:13:15 +01:00
|
|
|
return get(baseURL + id + "/propagationOrganizations");
|
2023-11-14 14:53:34 +01:00
|
|
|
|
2023-10-09 14:26:33 +02:00
|
|
|
}
|
|
|
|
|
2023-11-27 15:13:15 +01:00
|
|
|
public static String communityProjects(String id, String page, String size, String baseURL) throws IOException {
|
2023-11-14 14:53:34 +01:00
|
|
|
|
2023-11-27 15:13:15 +01:00
|
|
|
return get(baseURL + id + "/projects/" + page + "/" + size);
|
2023-11-14 14:53:34 +01:00
|
|
|
|
2023-10-09 14:26:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@NotNull
|
|
|
|
private static String getBody(HttpURLConnection conn) throws IOException {
|
|
|
|
String body = "{}";
|
|
|
|
try (BufferedReader br = new BufferedReader(
|
|
|
|
new InputStreamReader(conn.getInputStream(), "utf-8"))) {
|
|
|
|
StringBuilder response = new StringBuilder();
|
|
|
|
String responseLine = null;
|
|
|
|
while ((responseLine = br.readLine()) != null) {
|
|
|
|
response.append(responseLine.trim());
|
|
|
|
}
|
|
|
|
|
|
|
|
body = response.toString();
|
|
|
|
|
|
|
|
}
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|