diff --git a/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/transformation/xslt/DataFetcher.java b/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/transformation/xslt/DataFetcher.java new file mode 100644 index 000000000..281f45e98 --- /dev/null +++ b/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/transformation/xslt/DataFetcher.java @@ -0,0 +1,64 @@ +package eu.dnetlib.dhp.transformation.xslt; + +import com.mongodb.util.JSON; +import org.apache.commons.io.IOUtils; +import org.json.JSONObject; + +import java.io.IOException; +import java.net.URL; +import java.nio.charset.Charset; +import java.util.List; + + +/** + * This class fetches JSON from a provided link and returns + * a Dublin Core. This functionality is particularly needed for OSF Preprints + */ + +/** + * this method fetches JSON from a provided URL and returns it as JSON Object + */ +public class DataFetcher { + // fetch data + static JSONObject getJson(URL url) throws IOException { + + String json = IOUtils.toString(url); + return new JSONObject(json); + } + + /** + * This method extracts authors (contributors) from a given JSON + * @param jsonObject + * @return + */ + static List getContributorsFromJson(JSONObject jsonObject){ + List contributors; + + // count of authors + int countAuthors = jsonObject.getJSONArray("data").length(); + + +// jsonObject.getJSONArray("data").forEach(json1 -> System.out.println(json1)); + + + +// String x = testJsonObj +// .getJSONArray("data") +// .getJSONObject(0) +// .getJSONObject("embeds") +// .getJSONObject("users") +// .getJSONObject("data") +// .getJSONObject("attributes") +// .getString("full_name"); + + return null; + + + } + + + // transform it into Dublin Core + private void transformToDublinCore(JSON jsonData) { + System.out.println(jsonData); + } +} diff --git a/dhp-workflows/dhp-aggregation/src/test/java/eu/dnetlib/dhp/transformation/xslt/DataFetcherTest.java b/dhp-workflows/dhp-aggregation/src/test/java/eu/dnetlib/dhp/transformation/xslt/DataFetcherTest.java new file mode 100644 index 000000000..aa8e987b2 --- /dev/null +++ b/dhp-workflows/dhp-aggregation/src/test/java/eu/dnetlib/dhp/transformation/xslt/DataFetcherTest.java @@ -0,0 +1,51 @@ +package eu.dnetlib.dhp.transformation.xslt; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.junit.jupiter.api.Assertions.*; + +class DataFetcherTest { + + @BeforeEach + void setUp() { + } + + @AfterEach + void tearDown() { + } + + @Test + void getJson() throws IOException, URISyntaxException { + URL contributorsUrl = new URI("https://api.osf.io/v2/preprints/mrwqb/contributors/?format=json").toURL(); + JSONObject testJsonObj = DataFetcher.getJson(contributorsUrl); + + String x = testJsonObj + .getJSONArray("data") + .getJSONObject(0) + .getJSONObject("embeds") + .getJSONObject("users") + .getJSONObject("data") + .getJSONObject("attributes") + .getString("full_name"); + + System.out.println(x); + + System.out.println(testJsonObj.getJSONArray("data").length()); + + testJsonObj.getJSONArray("data").forEach(json1 -> System.out.println(json1)); + + + } +} \ No newline at end of file