API for D4science GCat

This commit is contained in:
Alessia Bardi 2020-06-19 17:37:22 +02:00
parent fb80353018
commit ec19fcace0
3 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,117 @@
package eu.dnetlib.dhp.oa.graph.dump.gcat;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import com.google.gson.Gson;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import javax.ws.rs.HttpMethod;
/**
* Created by Alessia Bardi on 19/06/2020.
*
* @author Alessia Bardi
*/
public class GCatAPIClient {
private static final Log log = LogFactory.getLog(GCatAPIClient.class);
private String gcatBaseURL;
private final String itemPath = "items";
private String applicationToken;
public GCatAPIClient(){}
/**
* Publish the json as in the D4science catalogue as an item.
* TODO: does the POST returns the whole item or just its catalogue identifier?
* @param jsonMetadata
* @return the HTTP status code of the request
* @throws IOException
*/
public int publish(final String jsonMetadata) throws IOException {
try(CloseableHttpClient client = HttpClients.createDefault()) {
HttpPost post = new HttpPost(getGcatBaseURL() + itemPath);
post.setHeader("gcube-token", getApplicationToken());
post.addHeader("Content-Type", "application/json");
post.addHeader("Accept", "application/json");
StringEntity entity = new StringEntity(jsonMetadata, StandardCharsets.UTF_8);
post.setEntity(entity);
HttpResponse response = client.execute(post);
System.out.println(response.getStatusLine().getStatusCode());
return response.getStatusLine().getStatusCode();
}
}
/**
* List items in the catalogue
* @param offset offset
* @param limit limit
* @return list of json items
* @throws IOException
* @throws URISyntaxException
*/
public List<String> list(final int offset, final int limit) throws IOException, URISyntaxException {
try(CloseableHttpClient client = HttpClients.createDefault()) {
URIBuilder builder = new URIBuilder(getGcatBaseURL() + itemPath)
.addParameter("offset", String.valueOf(offset))
.addParameter("limit", String.valueOf(limit));
HttpGet get = new HttpGet(builder.build());
get.setHeader("gcube-token", getApplicationToken());
get.addHeader("Content-Type", "application/json");
get.addHeader("Accept", "application/json");
// Create a custom response handler
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
@Override
public String handleResponse(
final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
String responseBody = client.execute(get, responseHandler);
System.out.println(responseBody);
Gson gson = new Gson();
return gson.fromJson(responseBody, List.class);
}
}
public String getGcatBaseURL() {
return gcatBaseURL;
}
public void setGcatBaseURL(String gcatBaseURL) {
this.gcatBaseURL = gcatBaseURL;
}
public String getApplicationToken() {
return applicationToken;
}
public void setApplicationToken(String applicationToken) {
this.applicationToken = applicationToken;
}
}

View File

@ -0,0 +1,35 @@
package eu.dnetlib.dhp.oa.graph.dump;
import eu.dnetlib.dhp.oa.graph.dump.gcat.GCatAPIClient;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URISyntaxException;
/**
* TODO: ask for a token for the dev gcat
*/
public class GCatAPIClientTest {
private static GCatAPIClient client;
//@BeforeAll
public static void setup(){
client = new GCatAPIClient();
client.setApplicationToken("");
client.setGcatBaseURL("https://gcat.d4science.org/gcat/");
}
// @Test
public void testList() throws IOException, URISyntaxException {
System.out.println( client.list(0, 10));
}
//@Test
public void testPublish() throws IOException {
String json = IOUtils.toString(getClass().getResourceAsStream("eu/dnetlib/dhp/oa/graph/dump/gcat/gcat_pub.json"));
client.publish(json);
}
}