62 lines
1.7 KiB
Java
62 lines
1.7 KiB
Java
|
package org.gcube.data.publishing.ckan2zenodo.clients;
|
||
|
|
||
|
import javax.ws.rs.client.Client;
|
||
|
import javax.ws.rs.client.ClientBuilder;
|
||
|
import javax.ws.rs.core.Response;
|
||
|
|
||
|
import org.gcube.data.publishing.ckan2zenodo.model.faults.ZenodoException;
|
||
|
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.ZenodoDeposition;
|
||
|
import org.glassfish.jersey.client.ClientProperties;
|
||
|
|
||
|
import lombok.NonNull;
|
||
|
import lombok.RequiredArgsConstructor;
|
||
|
|
||
|
@RequiredArgsConstructor
|
||
|
public class Zenodo {
|
||
|
|
||
|
private static final String BASE_URL="https://zenodo.org/api/";
|
||
|
|
||
|
private static final String CONTENT_TYPE="application/json";
|
||
|
|
||
|
private static final String DEPOSITION_BASE_URL="deposit/depositions";
|
||
|
|
||
|
private static final String AUTH="Authentication";
|
||
|
private static final String BEARER="Bearer ";
|
||
|
|
||
|
@NonNull
|
||
|
private String tokenValue;
|
||
|
|
||
|
|
||
|
Client client;
|
||
|
|
||
|
private synchronized Client getWebClient() {
|
||
|
if(client==null)
|
||
|
client = ClientBuilder.newClient(
|
||
|
// new ClientConfig().register(new HTTPBasicProvider(username, password))
|
||
|
)
|
||
|
.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
|
||
|
return client;
|
||
|
}
|
||
|
|
||
|
public ZenodoDeposition readDeposition(String id) throws ZenodoException {
|
||
|
|
||
|
// String respString=null;
|
||
|
return getWebClient().target(BASE_URL).
|
||
|
path(DEPOSITION_BASE_URL).path(id).request(CONTENT_TYPE)
|
||
|
.header(AUTH, BEARER+tokenValue)
|
||
|
.get(ZenodoDeposition.class);
|
||
|
// respString=check(resp);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
private static String check(Response resp) throws ZenodoException {
|
||
|
if(resp.getStatus()<200||resp.getStatus()>=300) {
|
||
|
throw new ZenodoException("RESP STATUS IS "+resp.getStatus()+". Message : "+resp.readEntity(String.class));
|
||
|
}else
|
||
|
return resp.readEntity(String.class);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|