added okhttp3 again

This commit is contained in:
Miriam Baglioni 2023-06-23 14:16:15 +02:00
parent d472050ad4
commit 72ead1bd85
2 changed files with 48 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package eu.dnetlib.dhp.oa.graph.dump;
import com.google.gson.Gson;
import eu.dnetlib.dhp.common.api.InputStreamRequestBody;
import eu.dnetlib.dhp.common.api.zenodo.ZenodoModel;
import eu.dnetlib.dhp.common.api.zenodo.ZenodoModelList;
import okhttp3.*;
@ -92,16 +93,30 @@ public class ZenodoAPIClient2 implements Serializable {
}
public int uploadIS3(InputStream is, String file_name, long len) throws IOException {
OkHttpClient httpClient = new OkHttpClient.Builder()
.writeTimeout(600, TimeUnit.SECONDS)
.readTimeout(600, TimeUnit.SECONDS)
.connectTimeout(600, TimeUnit.SECONDS)
.build();
Request request = new Request.Builder()
.url(bucket + "/" + file_name)
.addHeader(HttpHeaders.CONTENT_TYPE, "application/zip") // add request headers
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token)
.put(InputStreamRequestBody.create(MEDIA_TYPE_ZIP, is, len))
.build();
try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response + response.body().string());
return response.code();
}
}
public int uploadIS2(InputStream is, String fileName) throws IOException {
final String crlf = "\r\n";
final String twoHyphens = "--";
final String boundary = "*****";
HttpPut put = new HttpPut(bucket + "/" + fileName);
HttpPut put = new HttpPut(bucket + "/" + fileName);
@ -115,9 +130,6 @@ public class ZenodoAPIClient2 implements Serializable {
CloseableHttpResponse response = client.execute(put);
statusCode= response.getStatusLine().getStatusCode();
}
@ -215,8 +227,8 @@ public class ZenodoAPIClient2 implements Serializable {
private boolean checkOKStatus(int responseCode) {
if(HttpURLConnection.HTTP_OK != responseCode ||
HttpURLConnection.HTTP_CREATED != responseCode)
if(HttpURLConnection.HTTP_OK == responseCode ||
HttpURLConnection.HTTP_CREATED == responseCode)
return true ;
return false;
}

View File

@ -1,11 +1,15 @@
package eu.dnetlib.dhp.oa.graph.dump;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@ -22,7 +26,7 @@ public class ZenodoUploadTest {
private static String workingDir;
private final String URL_STRING = "https://sandbox.zenodo.org/api/deposit/depositions";
private final String ACCESS_TOKEN = "";
private final String ACCESS_TOKEN = "OzzOsyucEIHxCEfhlpsMo3myEiwpCza3trCRL7ddfGTAK9xXkIP2MbXd6Vg4";
@BeforeAll
public static void beforeAll() throws IOException {
@ -150,4 +154,23 @@ public class ZenodoUploadTest {
.getPath())));
}
@Test
void depositBigFile() throws MissingConceptDoiException, IOException {
ZenodoAPIClient2 client = new ZenodoAPIClient2(URL_STRING,
ACCESS_TOKEN);
Assertions.assertEquals(201, client.newDeposition());
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());
}
}