Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set directoryLabel (File Path) via API #5678

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/sphinx-guides/source/api/native-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ Add a file to an existing Dataset. Description and tags are optional::

A more detailed "add" example using curl::

curl -H "X-Dataverse-key:$API_TOKEN" -X POST -F 'file=@data.tsv' -F 'jsonData={"description":"My description.","categories":["Data"], "restrict":"true"}' "https://example.dataverse.edu/api/datasets/:persistentId/add?persistentId=$PERSISTENT_ID"
curl -H "X-Dataverse-key:$API_TOKEN" -X POST -F 'file=@data.tsv' -F 'jsonData={"description":"My description.","directoryLabel":"data/subdir1","categories":["Data"], "restrict":"true"}' "https://example.dataverse.edu/api/datasets/:persistentId/add?persistentId=$PERSISTENT_ID"

Example python code to add a file. This may be run by changing these parameters in the sample code:

Expand Down Expand Up @@ -704,12 +704,12 @@ A curl example using a ``pid``::
Replacing Files
~~~~~~~~~~~~~~~

Replace an existing file where ``id`` is the database id of the file to replace or ``pid`` is the persistent id (DOI or Handle) of the file. Note that metadata such as description and tags are not carried over from the file being replaced
Replace an existing file where ``id`` is the database id of the file to replace or ``pid`` is the persistent id (DOI or Handle) of the file. Note that metadata such as description, directoryLabel (File Path) and tags are not carried over from the file being replaced

.. code-block:: bash

curl -H "X-Dataverse-key:$API_TOKEN" -X POST -F 'file=@data.tsv' \
-F 'jsonData={"description":"My description.","categories":["Data"],"forceReplace":false}'\
-F 'jsonData={"description":"My description.","directoryLabel":"data/subdir1","categories":["Data"],"forceReplace":false}'\
"https://demo.dataverse.org/api/files/$FILE_ID/replace"

Example python code to replace a file. This may be run by changing these parameters in the sample code:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class OptionalFileParams {
private String description;
public static final String DESCRIPTION_ATTR_NAME = "description";

private String directoryLabel;
public static final String DIRECTORY_LABEL_ATTR_NAME = "directoryLabel";

private List<String> categories;
public static final String CATEGORIES_ATTR_NAME = "categories";

Expand Down Expand Up @@ -99,7 +102,15 @@ public void setDescription(String description){
public String getDescription(){
return this.description;
}


public String getDirectoryLabel() {
return directoryLabel;
}

public void setDirectoryLabel(String directoryLabel) {
this.directoryLabel = directoryLabel;
}

public void setRestriction(boolean restrict){
this.restrict = restrict;
}
Expand Down Expand Up @@ -128,6 +139,13 @@ public boolean hasDescription(){
}
return true;
}

public boolean hasDirectoryLabel(){
if ((directoryLabel == null)||(this.directoryLabel.isEmpty())){
return false;
}
return true;
}

/**
* Set tags
Expand Down Expand Up @@ -192,7 +210,15 @@ private void loadParamsFromJson(String jsonData) throws DataFileTagException{

this.description = jsonObj.get(DESCRIPTION_ATTR_NAME).getAsString();
}


// -------------------------------
// get directory label as string
// -------------------------------
if ((jsonObj.has(DIRECTORY_LABEL_ATTR_NAME)) && (!jsonObj.get(DIRECTORY_LABEL_ATTR_NAME).isJsonNull())){

this.directoryLabel = jsonObj.get(DIRECTORY_LABEL_ATTR_NAME).getAsString();
}

// -------------------------------
// get restriction as boolean
// -------------------------------
Expand Down Expand Up @@ -295,8 +321,14 @@ public void addOptionalParams(DataFile df) throws DataFileTagException{
if (hasDescription()){
fm.setDescription(this.getDescription());
}



// ---------------------------
// Add directory label (path)
// ---------------------------
if (hasDirectoryLabel()){
fm.setDirectoryLabel(this.getDirectoryLabel());
}

// ---------------------------
// Add categories
// ---------------------------
Expand Down
4 changes: 4 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 @@ -108,6 +108,7 @@ public void test_001_AddFileGood() {

JsonObjectBuilder json = Json.createObjectBuilder()
.add("description", "my description")
.add("directoryLabel", "data/subdir1")
.add("categories", Json.createArrayBuilder()
.add("Data")
);
Expand All @@ -129,6 +130,7 @@ public void test_001_AddFileGood() {
.body("data.files[0].categories[0]", equalTo("Data"))
.body("data.files[0].dataFile.contentType", equalTo("image/png"))
.body("data.files[0].dataFile.description", equalTo("my description"))
.body("data.files[0].directoryLabel", equalTo("data/subdir1"))
// .body("data.files[0].dataFile.tags", nullValue())
.body("data.files[0].dataFile.tabularTags", nullValue())
.body("data.files[0].label", equalTo("favicondataverse.png"))
Expand Down Expand Up @@ -368,6 +370,7 @@ public void test_006_ReplaceFileGood() throws InterruptedException {
String pathToFile2 = "scripts/search/data/replace_test/004.txt";
JsonObjectBuilder json = Json.createObjectBuilder()
.add("description", "My Text File")
.add("directoryLabel", "data/subdir1")
.add("categories", Json.createArrayBuilder()
.add("Data")
);
Expand All @@ -386,6 +389,7 @@ public void test_006_ReplaceFileGood() throws InterruptedException {
.body("data.files[0].label", equalTo("004.txt"))
.body("data.files[0].dataFile.contentType", startsWith("text/plain"))
.body("data.files[0].description", equalTo("My Text File"))
.body("data.files[0].directoryLabel", equalTo("data/subdir1"))
.body("data.files[0].categories[0]", equalTo("Data"))
//.body("data.rootDataFileId", equalTo(origFileId))
.statusCode(OK.getStatusCode());
Expand Down