1
0
Fork 0

refactoring

This commit is contained in:
Miriam Baglioni 2023-04-19 09:32:13 +02:00
parent 24c41806ac
commit 9fc8ebe98b
1 changed files with 13 additions and 25 deletions

View File

@ -9,13 +9,13 @@ import java.util.concurrent.TimeUnit;
import org.apache.http.HttpHeaders; import org.apache.http.HttpHeaders;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.jetbrains.annotations.NotNull;
import com.google.gson.Gson; import com.google.gson.Gson;
import eu.dnetlib.dhp.common.api.zenodo.ZenodoModel; import eu.dnetlib.dhp.common.api.zenodo.ZenodoModel;
import eu.dnetlib.dhp.common.api.zenodo.ZenodoModelList; import eu.dnetlib.dhp.common.api.zenodo.ZenodoModelList;
import okhttp3.*; import okhttp3.*;
import org.jetbrains.annotations.NotNull;
public class ZenodoAPIClient implements Serializable { public class ZenodoAPIClient implements Serializable {
@ -80,7 +80,7 @@ public class ZenodoAPIClient implements Serializable {
int responseCode = conn.getResponseCode(); int responseCode = conn.getResponseCode();
conn.disconnect(); conn.disconnect();
if(!checkOKStatus(responseCode)) if (!checkOKStatus(responseCode))
throw new IOException("Unexpected code " + responseCode + body); throw new IOException("Unexpected code " + responseCode + body);
ZenodoModel newSubmission = new Gson().fromJson(body, ZenodoModel.class); ZenodoModel newSubmission = new Gson().fromJson(body, ZenodoModel.class);
@ -115,7 +115,7 @@ public class ZenodoAPIClient implements Serializable {
} }
int responseCode = conn.getResponseCode(); int responseCode = conn.getResponseCode();
if(! checkOKStatus(responseCode)){ if (!checkOKStatus(responseCode)) {
throw new IOException("Unexpected code " + responseCode + getBody(conn)); throw new IOException("Unexpected code " + responseCode + getBody(conn));
} }
@ -126,7 +126,7 @@ public class ZenodoAPIClient implements Serializable {
private String getBody(HttpURLConnection conn) throws IOException { private String getBody(HttpURLConnection conn) throws IOException {
String body = "{}"; String body = "{}";
try (BufferedReader br = new BufferedReader( try (BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "utf-8"))) { new InputStreamReader(conn.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
String responseLine = null; String responseLine = null;
while ((responseLine = br.readLine()) != null) { while ((responseLine = br.readLine()) != null) {
@ -155,7 +155,6 @@ public class ZenodoAPIClient implements Serializable {
conn.setDoOutput(true); conn.setDoOutput(true);
conn.setRequestMethod("PUT"); conn.setRequestMethod("PUT");
try (OutputStream os = conn.getOutputStream()) { try (OutputStream os = conn.getOutputStream()) {
byte[] input = metadata.getBytes("utf-8"); byte[] input = metadata.getBytes("utf-8");
os.write(input, 0, input.length); os.write(input, 0, input.length);
@ -164,19 +163,18 @@ public class ZenodoAPIClient implements Serializable {
final int responseCode = conn.getResponseCode(); final int responseCode = conn.getResponseCode();
conn.disconnect(); conn.disconnect();
if(!checkOKStatus(responseCode)) if (!checkOKStatus(responseCode))
throw new IOException("Unexpected code " + responseCode + getBody(conn)); throw new IOException("Unexpected code " + responseCode + getBody(conn));
return responseCode; return responseCode;
} }
private boolean checkOKStatus(int responseCode) { private boolean checkOKStatus(int responseCode) {
if(HttpURLConnection.HTTP_OK != responseCode || if (HttpURLConnection.HTTP_OK != responseCode ||
HttpURLConnection.HTTP_CREATED != responseCode) HttpURLConnection.HTTP_CREATED != responseCode)
return true ; return true;
return false; return false;
} }
@ -233,7 +231,6 @@ public class ZenodoAPIClient implements Serializable {
conn.setDoOutput(true); conn.setDoOutput(true);
conn.setRequestMethod("POST"); conn.setRequestMethod("POST");
try (OutputStream os = conn.getOutputStream()) { try (OutputStream os = conn.getOutputStream()) {
byte[] input = json.getBytes("utf-8"); byte[] input = json.getBytes("utf-8");
os.write(input, 0, input.length); os.write(input, 0, input.length);
@ -245,7 +242,7 @@ public class ZenodoAPIClient implements Serializable {
int responseCode = conn.getResponseCode(); int responseCode = conn.getResponseCode();
conn.disconnect(); conn.disconnect();
if(!checkOKStatus(responseCode)) if (!checkOKStatus(responseCode))
throw new IOException("Unexpected code " + responseCode + body); throw new IOException("Unexpected code " + responseCode + body);
ZenodoModel zenodoModel = new Gson().fromJson(body, ZenodoModel.class); ZenodoModel zenodoModel = new Gson().fromJson(body, ZenodoModel.class);
@ -290,13 +287,12 @@ public class ZenodoAPIClient implements Serializable {
int responseCode = conn.getResponseCode(); int responseCode = conn.getResponseCode();
conn.disconnect(); conn.disconnect();
if(!checkOKStatus(responseCode)) if (!checkOKStatus(responseCode))
throw new IOException("Unexpected code " + responseCode + body); throw new IOException("Unexpected code " + responseCode + body);
ZenodoModel zenodoModel = new Gson().fromJson(body, ZenodoModel.class); ZenodoModel zenodoModel = new Gson().fromJson(body, ZenodoModel.class);
bucket = zenodoModel.getLinks().getBucket(); bucket = zenodoModel.getLinks().getBucket();
return responseCode; return responseCode;
} }
@ -331,22 +327,16 @@ public class ZenodoAPIClient implements Serializable {
conn.setDoOutput(true); conn.setDoOutput(true);
conn.setRequestMethod("GET"); conn.setRequestMethod("GET");
String body = getBody(conn); String body = getBody(conn);
int responseCode = conn.getResponseCode(); int responseCode = conn.getResponseCode();
conn.disconnect(); conn.disconnect();
if(!checkOKStatus(responseCode)) if (!checkOKStatus(responseCode))
throw new IOException("Unexpected code " + responseCode + body); throw new IOException("Unexpected code " + responseCode + body);
return body; return body;
} }
private String getBucket(String inputUurl) throws IOException { private String getBucket(String inputUurl) throws IOException {
@ -363,15 +353,13 @@ public class ZenodoAPIClient implements Serializable {
int responseCode = conn.getResponseCode(); int responseCode = conn.getResponseCode();
conn.disconnect(); conn.disconnect();
if(!checkOKStatus(responseCode)) if (!checkOKStatus(responseCode))
throw new IOException("Unexpected code " + responseCode + body); throw new IOException("Unexpected code " + responseCode + body);
ZenodoModel zenodoModel = new Gson().fromJson(body, ZenodoModel.class); ZenodoModel zenodoModel = new Gson().fromJson(body, ZenodoModel.class);
return zenodoModel.getLinks().getBucket(); return zenodoModel.getLinks().getBucket();
} }
} }