Skip to content

Commit 530c1a5

Browse files
committed
Refactorings and new entity functionality
1 parent 1d4150e commit 530c1a5

File tree

6 files changed

+145
-40
lines changed

6 files changed

+145
-40
lines changed

src/main/java/de/tud/cs/stg/zenodo/API.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ private Deposit() {}
1717
public final static String Discard = "api/deposit/depositions/{id}/actions/discard";
1818
public final static String Edit = "api/deposit/depositions/{id}/actions/edit";
1919

20+
public final static String Entity = "api/deposit/depositions/{id}";
2021

2122
}
2223
}

src/main/java/de/tud/cs/stg/zenodo/Deposition.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,19 @@ public class Deposition {
2020
public Links links;
2121
public Metadata metadata;
2222

23+
/**
24+
* Created by benhermann on 31.05.17.
25+
*/
26+
@JsonIgnoreProperties(ignoreUnknown = true)
27+
public static class Links {
28+
public String bucket;
29+
public String discard;
30+
public String edit;
31+
public String files;
32+
public String html;
33+
public String latest_draft;
34+
public String latest_draft_html;
35+
public String publish;
36+
public String self;
37+
}
2338
}

src/main/java/de/tud/cs/stg/zenodo/Links.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/de/tud/cs/stg/zenodo/Metadata.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@
99
*/
1010
@JsonIgnoreProperties(ignoreUnknown = true)
1111
public class Metadata {
12+
13+
Metadata() {
14+
15+
}
16+
17+
public Metadata(UploadType upload_type,
18+
Date publication_date,
19+
String title,
20+
String description,
21+
AccessRight accessRight) {
22+
this.upload_type = upload_type.toString();
23+
this.publication_date = publication_date;
24+
this.title = title;
25+
this.description = description;
26+
this.access_right = accessRight.toString();
27+
}
28+
1229
public PreserveDOI preserve_doi;
1330
public String upload_type;
1431
public String publication_type;
@@ -51,4 +68,58 @@ public class Metadata {
5168
// public ArrayList<Subject> subjects;
5269

5370

71+
/**
72+
* Created by benhermann on 04.06.17.
73+
*/
74+
public static class AccessRight {
75+
private String accessRight;
76+
77+
private AccessRight(String accessRight) {
78+
this.accessRight = accessRight;
79+
}
80+
81+
@Override
82+
public String toString() {
83+
return accessRight;
84+
}
85+
86+
public static final AccessRight OPEN = new AccessRight("open");
87+
public static final AccessRight EMBARGOED = new AccessRight("embargoed");
88+
public static final AccessRight RESTRICTED = new AccessRight("restricted");
89+
public static final AccessRight CLOSED = new AccessRight("closed");
90+
}
91+
92+
/**
93+
* Created by benhermann on 04.06.17.
94+
*/
95+
public static class UploadType {
96+
private String uploadType;
97+
98+
private UploadType(String uploadType) {
99+
this.uploadType = uploadType;
100+
}
101+
102+
@Override
103+
public String toString() {
104+
return uploadType;
105+
}
106+
107+
public static final UploadType PUBLICATION = new UploadType("publication");
108+
public static final UploadType POSTER = new UploadType("poster");
109+
public static final UploadType PRESENTATION = new UploadType("presentation");
110+
public static final UploadType DATASET = new UploadType("dataset");
111+
public static final UploadType IMAGE = new UploadType("image");
112+
public static final UploadType VIDEO = new UploadType("video");
113+
public static final UploadType SOFTWARE = new UploadType("software");
114+
115+
116+
}
117+
118+
/**
119+
* Created by benhermann on 01.06.17.
120+
*/
121+
@JsonIgnoreProperties(ignoreUnknown = true)
122+
public static class PreserveDOI {
123+
124+
}
54125
}

src/main/java/de/tud/cs/stg/zenodo/PreserveDOI.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/main/java/de/tud/cs/stg/zenodo/ZenodoClient.java

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.util.ArrayList;
1818
import java.util.Date;
1919
import java.util.List;
20-
import java.util.TimeZone;
2120

2221
/**
2322
* Created by benhermann on 31.05.17.
@@ -48,7 +47,7 @@ public ZenodoClient(String baseURL, String token) {
4847
final ISO8601DateFormat dateFormat = new ISO8601DateFormat() {
4948
@Override
5049
public Date parse(String source) throws ParseException {
51-
if (!source.endsWith("+0000")) source = source + "+0000";
50+
if (!source.endsWith("+0000") && ! source.endsWith("+00:00")) source = source + "+0000";
5251
return super.parse(source);
5352
}
5453
};
@@ -98,6 +97,18 @@ public boolean test() {
9897
return false;
9998
}
10099

100+
public Deposition getDeposition(Integer id) {
101+
GetRequest request = prepareGetRequest(baseURL + API.Deposit.Entity);
102+
request.routeParam("id", id.toString());
103+
try {
104+
HttpResponse<Deposition> response = request.asObject(Deposition.class);
105+
return response.getBody();
106+
} catch (UnirestException e) {
107+
e.printStackTrace();
108+
}
109+
return null;
110+
}
111+
101112
public List<Deposition> getDepositions() {
102113
ArrayList<Deposition> result = new ArrayList<Deposition>();
103114
GetRequest request = prepareGetRequest(baseURL + API.Deposit.Depositions);
@@ -110,6 +121,28 @@ public List<Deposition> getDepositions() {
110121
return null;
111122
}
112123

124+
public Deposition updateDeposition(Deposition deposition) {
125+
HttpRequestWithBody request = preparePutRequest(baseURL + API.Deposit.Entity);
126+
request.routeParam("id", deposition.id.toString());
127+
try {
128+
HttpResponse<Deposition> response = request.asObject(Deposition.class);
129+
return response.getBody();
130+
} catch (UnirestException e) {
131+
e.printStackTrace();
132+
}
133+
return null;
134+
}
135+
136+
public void deleteDeposition(Integer id) {
137+
HttpRequestWithBody request = prepareDeleteRequest(baseURL + API.Deposit.Entity);
138+
request.routeParam("id", id.toString());
139+
try {
140+
HttpResponse<String> response = request.asString();
141+
} catch (UnirestException e) {
142+
e.printStackTrace();
143+
}
144+
}
145+
113146
private <T> T fromJSON(final TypeReference<T> type, final String jsonPacket) {
114147
T data = null;
115148
try {
@@ -181,20 +214,35 @@ private GetRequest prepareGetRequest(String url) {
181214
.header("Authorization", "Bearer " + token);
182215
}
183216

217+
private HttpRequestWithBody preparePutRequest(String url) {
218+
return Unirest.put(url)
219+
.header("Content-Type", "application/json")
220+
.header("Authorization", "Bearer" + token);
221+
}
222+
223+
private HttpRequestWithBody prepareDeleteRequest(String url) {
224+
return Unirest.delete(url)
225+
.header("Content-Type", "application/json")
226+
.header("Authorization", "Bearer " + token);
227+
}
228+
184229
public static void main(String[] args) {
185230
ZenodoClient client = new ZenodoClient(sandboxURL, sandboxToken);
186231
System.out.println(client.test());
187232

188-
Metadata firstTry = new Metadata();
189-
firstTry.title = "API test";
190-
Deposition deposition = client.createDeposition(firstTry);
191-
System.out.println(deposition.id);
233+
Metadata firstTry = new Metadata(Metadata.UploadType.DATASET,
234+
new Date(),
235+
"API test",
236+
"API test",
237+
Metadata.AccessRight.CLOSED);
238+
239+
// Deposition deposition = client.createDeposition(firstTry);
240+
//System.out.println(deposition.id);
192241

193-
/*
194-
// Produces status 500 currently.
195242
List<Deposition> depositions = client.getDepositions();
196243
for (Deposition d : depositions)
197-
client.discard(d.id);
198-
*/
244+
//System.out.println(d.title);
245+
client.deleteDeposition(d.id);
246+
199247
}
200248
}

0 commit comments

Comments
 (0)