Skip to content

Commit

Permalink
Extend the API for dataverse facets retrieval (#10727)
Browse files Browse the repository at this point in the history
* Added: list dataverse facets API endpoint extended to include details

* Added: docs for listFacets dataverses API endpoint

* Added: API endpoint for getting all facetable field types

* Changed: API endpoint route for obtaining facetables dataset fields

* Changed: DatasetFieldServiceApi format reset

* Added: docs for #10726

* Added: release notes for #10726

* Added: IT test case for listing Dataverse facets
  • Loading branch information
GPortas authored Aug 15, 2024
1 parent 53f9b45 commit 8205f13
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 59 deletions.
3 changes: 3 additions & 0 deletions doc/release-notes/10726-dataverse-facets-api-extension.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
New optional query parameter "returnDetails" added to "dataverses/{identifier}/facets/" endpoint to include detailed information of each DataverseFacet.

New endpoint "datasetfields/facetables" that lists all facetable dataset fields defined in the installation.
38 changes: 38 additions & 0 deletions doc/sphinx-guides/source/api/native-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,22 @@ 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/dataverses/root/facets"
By default, this endpoint will return an array including the facet names. If more detailed information is needed, we can set the query parameter ``returnDetails`` to ``true``, which will return the display name and id in addition to the name for each facet:

.. code-block:: bash
export API_TOKEN=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
export SERVER_URL=https://demo.dataverse.org
export ID=root
curl -H "X-Dataverse-key:$API_TOKEN" "$SERVER_URL/api/dataverses/$ID/facets?returnDetails=true"
The fully expanded example above (without environment variables) looks like this:

.. code-block:: bash
curl -H "X-Dataverse-key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" "https://demo.dataverse.org/api/dataverses/root/facets?returnDetails=true"
Set Facets for a Dataverse Collection
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -4717,6 +4733,28 @@ The fully expanded example above (without environment variables) looks like this
curl "https://demo.dataverse.org/api/metadatablocks/citation"
.. _dataset-fields-api:
Dataset Fields
--------------
List All Facetable Dataset Fields
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
List all facetable dataset fields defined in the installation.
.. code-block:: bash
export SERVER_URL=https://demo.dataverse.org
curl "$SERVER_URL/api/datasetfields/facetables"
The fully expanded example above (without environment variables) looks like this:
.. code-block:: bash
curl "https://demo.dataverse.org/api/datasetfields/facetables"
.. _Notifications:
Notifications
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/api/DatasetFields.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package edu.harvard.iq.dataverse.api;

import edu.harvard.iq.dataverse.DatasetFieldServiceBean;
import edu.harvard.iq.dataverse.DatasetFieldType;
import jakarta.ejb.EJB;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Response;

import java.util.List;

import static edu.harvard.iq.dataverse.util.json.JsonPrinter.jsonDatasetFieldTypes;

/**
* Api bean for managing dataset fields.
*/
@Path("datasetfields")
@Produces("application/json")
public class DatasetFields extends AbstractApiBean {

@EJB
DatasetFieldServiceBean datasetFieldService;

@GET
@Path("facetables")
public Response listAllFacetableDatasetFields() {
List<DatasetFieldType> datasetFieldTypes = datasetFieldService.findAllFacetableFieldTypes();
return ok(jsonDatasetFieldTypes(datasetFieldTypes));
}
}
23 changes: 15 additions & 8 deletions src/main/java/edu/harvard/iq/dataverse/api/Dataverses.java
Original file line number Diff line number Diff line change
Expand Up @@ -849,22 +849,29 @@ public Response setMetadataRoot(@Context ContainerRequestContext crc, @PathParam
/**
* return list of facets for the dataverse with alias `dvIdtf`
*/
public Response listFacets(@Context ContainerRequestContext crc, @PathParam("identifier") String dvIdtf) {
public Response listFacets(@Context ContainerRequestContext crc,
@PathParam("identifier") String dvIdtf,
@QueryParam("returnDetails") boolean returnDetails) {
try {
User u = getRequestUser(crc);
DataverseRequest r = createDataverseRequest(u);
User user = getRequestUser(crc);
DataverseRequest request = createDataverseRequest(user);
Dataverse dataverse = findDataverseOrDie(dvIdtf);
JsonArrayBuilder fs = Json.createArrayBuilder();
for (DataverseFacet f : execCommand(new ListFacetsCommand(r, dataverse))) {
fs.add(f.getDatasetFieldType().getName());
List<DataverseFacet> dataverseFacets = execCommand(new ListFacetsCommand(request, dataverse));

if (returnDetails) {
return ok(jsonDataverseFacets(dataverseFacets));
} else {
JsonArrayBuilder facetsBuilder = Json.createArrayBuilder();
for (DataverseFacet facet : dataverseFacets) {
facetsBuilder.add(facet.getDatasetFieldType().getName());
}
return ok(facetsBuilder);
}
return ok(fs);
} catch (WrappedResponse e) {
return e.getResponse();
}
}


@GET
@AuthRequired
@Path("{identifier}/featured")
Expand Down
Loading

0 comments on commit 8205f13

Please sign in to comment.