Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## API Enhancement
API call to `/api/guestbooks/{dataverseAlias}/list` can now include `"usageCount":#` and `"responseCount":#` in the response by adding the query param "includeStats=true".
2 changes: 2 additions & 0 deletions doc/sphinx-guides/source/api/native-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,8 @@ The fully expanded example above (without environment variables) looks like this

curl -H "X-Dataverse-key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" "https://demo.dataverse.org/api/guestbooks/root/list"

.. note:: By adding the query param "includeStats=true" `usageCount` and `responseCount` values can be added to the response.

Get a Guestbook for a Dataverse Collection
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/api/Guestbooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.harvard.iq.dataverse.Dataverse;
import edu.harvard.iq.dataverse.Guestbook;
import edu.harvard.iq.dataverse.GuestbookResponseServiceBean;
import edu.harvard.iq.dataverse.GuestbookServiceBean;
import edu.harvard.iq.dataverse.api.auth.AuthRequired;
import edu.harvard.iq.dataverse.authorization.Permission;
Expand Down Expand Up @@ -38,6 +39,8 @@ public class Guestbooks extends AbstractApiBean {

@EJB
GuestbookServiceBean guestbookService;
@EJB
GuestbookResponseServiceBean guestbookResponseService;

@GET
@AuthRequired
Expand All @@ -57,16 +60,21 @@ public Response getGuestbook(@Context ContainerRequestContext crc, @PathParam("i
@GET
@AuthRequired
@Path("{identifier}/list")
public Response getGuestbooks(@Context ContainerRequestContext crc, @PathParam("identifier") String identifier) {
public Response getGuestbooks(@Context ContainerRequestContext crc, @PathParam("identifier") String identifier, @QueryParam("includeStats") boolean includeStats) {
return response( req -> {
Dataverse dataverse = findDataverseOrDie(identifier);
final Long dataverseId = dataverse.getId();
if (!permissionSvc.request(req).on(dataverse).has(Permission.EditDataverse)) {
return error(Response.Status.FORBIDDEN, "Not authorized");
}
List<Guestbook> guestbooks = guestbookService.findGuestbooksForGivenDataverse(dataverse);
JsonArrayBuilder guestbookArray = Json.createArrayBuilder();
JsonPrinter jsonPrinter = new JsonPrinter();
for (Guestbook gb : guestbooks) {
if (includeStats) {
gb.setUsageCount(guestbookService.findCountUsages(gb.getId(), dataverseId));
gb.setResponseCount(guestbookResponseService.findCountByGuestbookId(gb.getId(), dataverseId));
}
guestbookArray.add(jsonPrinter.json(gb));
}
return ok(guestbookArray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,12 @@ public static JsonObjectBuilder json(Guestbook guestbook) {
guestbookObject.add("nameRequired", guestbook.isNameRequired());
guestbookObject.add("institutionRequired", guestbook.isInstitutionRequired());
guestbookObject.add("positionRequired", guestbook.isPositionRequired());
if (guestbook.getUsageCount() != null) {
guestbookObject.add("usageCount", guestbook.getUsageCount());
}
if (guestbook.getResponseCount() != null) {
guestbookObject.add("responseCount", guestbook.getResponseCount());
}
JsonArrayBuilder customQuestions = Json.createArrayBuilder();
if (guestbook.getCustomQuestions() != null) {
for (CustomQuestion cq : guestbook.getCustomQuestions()) {
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3983,6 +3983,7 @@ public void testDownloadFileWithGuestbookResponse() throws IOException, JsonPars
.statusCode(BAD_REQUEST.getStatusCode());
String guestbookResponseForGuest = guestbookResponse.replace("\"guestbookResponse\": {",
"\"guestbookResponse\": { \"name\":\"My Name\", \"email\":\"myemail@example.com\", \"position\":\"My Position\", \"institution\":\"My Institution\",");

// With GuestbookResponse. Guest user doesn't have the required Name, etc. So we will add those to the Guestbook Response
downloadResponse = UtilIT.postDownloadFile(fileId4, guestbookResponseForGuest);
downloadResponse.prettyPrint();
Expand All @@ -4008,6 +4009,12 @@ public void testDownloadFileWithGuestbookResponse() throws IOException, JsonPars
.statusCode(OK.getStatusCode());
signedUrl = UtilIT.getSignedUrlFromResponse(downloadResponse);

// Verify that the Guestbook Response is persisted
Response guestbookResponseResponse = UtilIT.getGuestbookResponses(dataverseAlias, guestbook.getId(), ownerApiToken);
guestbookResponseResponse.then().assertThat()
.statusCode(OK.getStatusCode());
assertTrue(guestbookResponseResponse.prettyPrint().contains("What color car do you drive,Yellow"));

// Download the file using the signed url
signedUrlResponse = get(signedUrl);
assertEquals(OK.getStatusCode(), signedUrlResponse.getStatusCode());
Expand Down Expand Up @@ -4053,6 +4060,14 @@ public void testDownloadFileWithGuestbookResponse() throws IOException, JsonPars
signedUrl = UtilIT.getSignedUrlFromResponse(downloadResponse);
signedUrlResponse = get(signedUrl);
assertEquals(OK.getStatusCode(), signedUrlResponse.getStatusCode());

// Verify that the guestbook has proper stats
Response guestbookListResponse = UtilIT.getGuestbooks(dataverseAlias, ownerApiToken, true);
guestbookListResponse.prettyPrint();
guestbookListResponse.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].usageCount", is(1))
.body("data[0].responseCount", is(16));
}

@Test
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,13 @@ static Response getGuestbook(Long guestbookId, String apiToken) {
}

static Response getGuestbooks(String dataverseAlias, String apiToken) {
return getGuestbooks(dataverseAlias, apiToken, false);
}
static Response getGuestbooks(String dataverseAlias, String apiToken, boolean includeStats) {
String params = "?includeStats=" + includeStats;
RequestSpecification requestSpec = given()
.header(API_TOKEN_HTTP_HEADER, apiToken);
return requestSpec.get("/api/guestbooks/" + dataverseAlias + "/list" );
return requestSpec.get("/api/guestbooks/" + dataverseAlias + "/list" + params );
}

static Response enableGuestbook(String dataverseAlias, Long guestbookId, String apiToken, String enable) {
Expand Down Expand Up @@ -1267,7 +1271,9 @@ static Response downloadFileOriginal(Integer fileId, String apiToken) {

static Response getDownloadFileUrlWithGuestbookResponse(Integer fileId, String apiToken, String body) {
RequestSpecification requestSpecification = given();
requestSpecification.header(API_TOKEN_HTTP_HEADER, apiToken);
if (apiToken != null) {
requestSpecification.header(API_TOKEN_HTTP_HEADER, apiToken);
}
if (body != null) {
requestSpecification.body(body);
}
Expand Down
Loading