1
|
package eu.dnetlib.data.collector.plugins.parthenos.ehri;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.InputStreamReader;
|
5
|
import java.net.URI;
|
6
|
import java.net.URISyntaxException;
|
7
|
import java.util.Iterator;
|
8
|
|
9
|
import com.google.common.collect.Iterables;
|
10
|
import com.google.common.collect.Lists;
|
11
|
import com.google.gson.JsonArray;
|
12
|
import com.google.gson.JsonObject;
|
13
|
import com.google.gson.JsonParser;
|
14
|
import eu.dnetlib.rmi.data.CollectorServiceException;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
import org.apache.http.HttpEntity;
|
18
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
19
|
import org.apache.http.client.methods.HttpPost;
|
20
|
import org.apache.http.entity.StringEntity;
|
21
|
import org.apache.http.impl.client.CloseableHttpClient;
|
22
|
import org.apache.http.impl.client.HttpClients;
|
23
|
import org.apache.http.util.EntityUtils;
|
24
|
|
25
|
/**
|
26
|
* IDs of units are retrieved from the EHRI GraphQL API, see docs at https://portal.ehri-project.eu/api/graphql
|
27
|
*
|
28
|
* Created by Alessia Bardi on 19/04/2017.
|
29
|
*
|
30
|
* @author Alessia Bardi
|
31
|
*/
|
32
|
public class EHRIGraphQLClient {
|
33
|
|
34
|
private static final Log log = LogFactory.getLog(EHRIGraphQLClient.class);
|
35
|
|
36
|
public Iterator<String> collect(final String baseURL, final String graphQLQuery) throws URISyntaxException, IOException, CollectorServiceException {
|
37
|
/*
|
38
|
The curl equivalent:
|
39
|
curl --header X-Stream:true \
|
40
|
--header Content-type:application/json \
|
41
|
https://portal.ehri-project.eu/api/graphql \
|
42
|
--data-binary '{"query":"{topLevelDocumentaryUnits{items{id}}}"}'
|
43
|
*/
|
44
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
45
|
URI baseURI = new URI(baseURL);
|
46
|
HttpPost httpPost = new HttpPost();
|
47
|
httpPost.setURI(baseURI);
|
48
|
httpPost.setHeader("X-Stream", "true");
|
49
|
httpPost.setHeader("Content-type", "application/json" );
|
50
|
log.info(graphQLQuery);
|
51
|
StringEntity postQuery = new StringEntity(graphQLQuery);
|
52
|
httpPost.setEntity(postQuery);
|
53
|
HttpEntity entity = null;
|
54
|
try(CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
55
|
switch(response.getStatusLine().getStatusCode()){
|
56
|
case 200:
|
57
|
entity = response.getEntity();
|
58
|
InputStreamReader reader = new InputStreamReader(entity.getContent());
|
59
|
return getIdentifiers(reader);
|
60
|
default:
|
61
|
log.error(httpPost);
|
62
|
log.error(response.getStatusLine());
|
63
|
throw new CollectorServiceException(response.getStatusLine().toString());
|
64
|
}
|
65
|
} finally {
|
66
|
if(entity != null) EntityUtils.consume(entity);
|
67
|
}
|
68
|
}
|
69
|
|
70
|
protected Iterator<String> getIdentifiers(final InputStreamReader input){
|
71
|
JsonObject jsonObject = new JsonParser().parse(input).getAsJsonObject();
|
72
|
JsonArray items = jsonObject.getAsJsonObject("data").getAsJsonObject("topLevelDocumentaryUnits").getAsJsonArray("items");
|
73
|
log.debug(items);
|
74
|
return Lists.newArrayList(Iterables.transform(items, jelem -> jelem.getAsJsonObject().get("id").getAsString())).iterator();
|
75
|
}
|
76
|
|
77
|
}
|