From 21a521b97cf9510afa65f282011d1f449d5c7abb Mon Sep 17 00:00:00 2001 From: "miriam.baglioni" Date: Sat, 15 Jul 2023 10:36:54 +0200 Subject: [PATCH] changed the API to consider the upload only of an already open version --- .../dhp/oa/graph/dump/SendToZenodoHDFS.java | 6 +- .../dhp/oa/zenodoapi/ZenodoAPIClient.java | 71 +++++++++---------- .../dhp/oa/graph/dump/ZenodoUploadTest.java | 38 +++++++--- 3 files changed, 64 insertions(+), 51 deletions(-) diff --git a/dump/src/main/java/eu/dnetlib/dhp/oa/graph/dump/SendToZenodoHDFS.java b/dump/src/main/java/eu/dnetlib/dhp/oa/graph/dump/SendToZenodoHDFS.java index 32031eb..bf90722 100644 --- a/dump/src/main/java/eu/dnetlib/dhp/oa/graph/dump/SendToZenodoHDFS.java +++ b/dump/src/main/java/eu/dnetlib/dhp/oa/graph/dump/SendToZenodoHDFS.java @@ -90,9 +90,9 @@ public class SendToZenodoHDFS implements Serializable { zenodoApiClient.sendMretadata(metadata); } - if (Boolean.TRUE.equals(publish)) { - zenodoApiClient.publish(); - } +// if (Boolean.TRUE.equals(publish)) { +// zenodoApiClient.publish(); +// } } } diff --git a/dump/src/main/java/eu/dnetlib/dhp/oa/zenodoapi/ZenodoAPIClient.java b/dump/src/main/java/eu/dnetlib/dhp/oa/zenodoapi/ZenodoAPIClient.java index 1b3bb7a..d37be74 100644 --- a/dump/src/main/java/eu/dnetlib/dhp/oa/zenodoapi/ZenodoAPIClient.java +++ b/dump/src/main/java/eu/dnetlib/dhp/oa/zenodoapi/ZenodoAPIClient.java @@ -18,7 +18,7 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.InputStreamEntity; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; -//import org.apache.http.impl.client.HttpClients; +// import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.jetbrains.annotations.NotNull; @@ -108,11 +108,11 @@ public class ZenodoAPIClient implements Serializable { RequestBody body = RequestBody.create(json, MEDIA_TYPE_JSON); Request request = new Request.Builder() - .url(urlString) - .addHeader("Content-Type", "application/json") // add request headers - .addHeader("Authorization", "Bearer " + access_token) - .post(body) - .build(); + .url(urlString) + .addHeader("Content-Type", "application/json") // add request headers + .addHeader("Authorization", "Bearer " + access_token) + .post(body) + .build(); try (Response response = httpClient.newCall(request).execute()) { @@ -122,7 +122,8 @@ public class ZenodoAPIClient implements Serializable { // Get response body json = response.body().string(); - eu.dnetlib.dhp.common.api.zenodo.ZenodoModel newSubmission = new Gson().fromJson(json, eu.dnetlib.dhp.common.api.zenodo.ZenodoModel.class); + eu.dnetlib.dhp.common.api.zenodo.ZenodoModel newSubmission = new Gson() + .fromJson(json, eu.dnetlib.dhp.common.api.zenodo.ZenodoModel.class); this.bucket = newSubmission.getLinks().getBucket(); this.deposition_id = newSubmission.getId(); @@ -132,7 +133,6 @@ public class ZenodoAPIClient implements Serializable { } - // public int uploadIS2(InputStream is, String fileName) throws IOException { // // final String crlf = "\r\n"; @@ -340,10 +340,10 @@ public class ZenodoAPIClient implements Serializable { RequestBody body = RequestBody.create(json, MEDIA_TYPE_JSON); Request request = new Request.Builder() - .url(urlString + "/" + deposition_id + "/actions/newversion") - .addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token) - .post(body) - .build(); + .url(urlString + "/" + deposition_id + "/actions/newversion") + .addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token) + .post(body) + .build(); try (Response response = httpClient.newCall(request).execute()) { @@ -374,32 +374,25 @@ public class ZenodoAPIClient implements Serializable { this.deposition_id = deposition_id; - String json = "{}"; + OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build(); - URL url = new URL(urlString + "/" + deposition_id); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + Request request = new Request.Builder() + .url(urlString + "/" + deposition_id) + .addHeader("Authorization", "Bearer " + access_token) + .build(); + + try (Response response = httpClient.newCall(request).execute()) { + + if (!response.isSuccessful()) + throw new IOException("Unexpected code " + response + response.body().string()); + + eu.dnetlib.dhp.common.api.zenodo.ZenodoModel zenodoModel = new Gson() + .fromJson(response.body().string(), eu.dnetlib.dhp.common.api.zenodo.ZenodoModel.class); + bucket = zenodoModel.getLinks().getBucket(); + return response.code(); - conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token); - conn.setRequestMethod("POST"); - conn.setDoOutput(true); - try (OutputStream os = conn.getOutputStream()) { - byte[] input = json.getBytes("utf-8"); - os.write(input, 0, input.length); } - String body = getBody(conn); - - int responseCode = conn.getResponseCode(); - conn.disconnect(); - - if (!checkOKStatus(responseCode)) - throw new IOException("Unexpected code " + responseCode + body); - - ZenodoModel zenodoModel = new Gson().fromJson(body, ZenodoModel.class); - bucket = zenodoModel.getLinks().getBucket(); - - return responseCode; - } private void setDepositionId(String concept_rec_id, Integer page) throws Exception, MissingConceptDoiException { @@ -445,11 +438,11 @@ public class ZenodoAPIClient implements Serializable { String url = urlBuilder.build().toString(); Request request = new Request.Builder() - .url(url) - .addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()) // add request headers - .addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token) - .get() - .build(); + .url(url) + .addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()) // add request headers + .addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token) + .get() + .build(); try (Response response = httpClient.newCall(request).execute()) { diff --git a/dump/src/test/java/eu/dnetlib/dhp/oa/graph/dump/ZenodoUploadTest.java b/dump/src/test/java/eu/dnetlib/dhp/oa/graph/dump/ZenodoUploadTest.java index f119274..25aec29 100644 --- a/dump/src/test/java/eu/dnetlib/dhp/oa/graph/dump/ZenodoUploadTest.java +++ b/dump/src/test/java/eu/dnetlib/dhp/oa/graph/dump/ZenodoUploadTest.java @@ -7,8 +7,6 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; -import eu.dnetlib.dhp.oa.zenodoapi.ZenodoAPIClient; -import eu.dnetlib.dhp.oa.zenodoapi.MissingConceptDoiException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.junit.jupiter.api.Assertions; @@ -19,15 +17,16 @@ import org.junit.jupiter.api.Test; import com.google.gson.Gson; import eu.dnetlib.dhp.oa.graph.dump.community.CommunityMap; - +import eu.dnetlib.dhp.oa.zenodoapi.MissingConceptDoiException; +import eu.dnetlib.dhp.oa.zenodoapi.ZenodoAPIClient; @Disabled public class ZenodoUploadTest { private static String workingDir; - private final String URL_STRING = "https://sandbox.zenodo.org/api/deposit/depositions"; - private final String ACCESS_TOKEN = "OzzOsyucEIHxCEfhlpsMo3myEiwpCza3trCRL7ddfGTAK9xXkIP2MbXd6Vg4"; + private final String URL_STRING = "https://zenodo.org/api/deposit/depositions"; + private final String ACCESS_TOKEN = "GxqutB1JnEmdvBafQI2cCjtUvoOs0novDuie3hxCEQUJcErHVMhkJjawIqhb"; @BeforeAll public static void beforeAll() throws IOException { @@ -86,8 +85,7 @@ public class ZenodoUploadTest { System.out.println(client.sendMretadata(metadata)); - - //System.out.println(client.publish()); + // System.out.println(client.publish()); } @@ -138,7 +136,7 @@ public class ZenodoUploadTest { } - //System.out.println(client.publish()); + // System.out.println(client.publish()); } @@ -146,7 +144,7 @@ public class ZenodoUploadTest { void testNewVersion2() throws Exception, MissingConceptDoiException { ZenodoAPIClient client = new ZenodoAPIClient(URL_STRING, - ACCESS_TOKEN); + ACCESS_TOKEN); client.newVersion("1210237"); @@ -162,6 +160,7 @@ public class ZenodoUploadTest { // Assertions.assertEquals(202, client.publish()); } + @Test void readCommunityMap() throws IOException { LocalFileSystem fs = FileSystem.getLocal(new Configuration()); @@ -195,4 +194,25 @@ public class ZenodoUploadTest { // Assertions.assertEquals(202, client.publish()); } + @Test + void testOnlyUpload() throws Exception, MissingConceptDoiException { + + ZenodoAPIClient client = new ZenodoAPIClient(URL_STRING, + ACCESS_TOKEN); + + client.uploadOpenDeposition("8144316"); + + File file = new File("/Users/miriam.baglioni/Desktop/EOSC_DUMP/publication.tar"); +// File file = new File(getClass() +// .getResource("/eu/dnetlib/dhp/common/api/newVersion2") +// .getPath()); + + InputStream is = new FileInputStream(file); + + Assertions.assertEquals(200, client.uploadIS3(is, "newVersion_deposition", file.length())); + + // Assertions.assertEquals(202, client.publish()); + + } + }