Skip to content

Commit df8c622

Browse files
committed
added updateDataset(...) to update metadata dataset
This new method make a request to the following address: * PUT /api/datasets/:persistentId/versions/:draft?persistentId=$datasetId * The body request should be a json with dataset metadata like as in addBasicDataset() This Dataverse API endpoint let ordinary users to update or replace dataset metadata. If the dataset is in a DRAFT state then the metadata will be replaced, if the dataset already in a published state then a new DRAFT version will be created. See Dataverse API specs for details: * https://guides.dataverse.org/en/latest/api/native-api.html#update-metadata-for-a-dataset
1 parent b3184db commit df8c622

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ http://demo.dataverse.org/api/datasets/389608/versions/1
6161

6262
`public async publishDataset(datasetId: string, versionUpgradeType: DatasetVersionUpgradeType = DatasetVersionUpgradeType.MAJOR): Promise<AxiosResponse> {`
6363

64+
`public async updateDataset(datasetId: string, datasetInformation: BasicDatasetInformation): Promise<AxiosResponse> {`
65+
6466
## Build project
6567

6668
In order to build the project, we need to run the following command:

src/dataverseClient.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ export class DataverseClient {
173173
return this.postRequest(url, '')
174174
}
175175

176+
public async updateDataset(datasetId: string, datasetInformation: BasicDatasetInformation): Promise<AxiosResponse> {
177+
const url = `${this.host}/api/datasets/:persistentId/versions/:draft?persistentId=${datasetId}`
178+
const payload: any = DatasetUtil.mapBasicDatasetInformation(datasetInformation)
179+
180+
return this.putRequest(url, payload.datasetVersion, {
181+
headers: {
182+
...this.getHeaders(),
183+
'Content-Type': 'application/json'
184+
}
185+
})
186+
}
187+
176188
private async getRequest(url: string, options: { params?: object, headers?: DataverseHeaders, responseType?: ResponseType } = { headers: this.getHeaders() }): Promise<AxiosResponse> {
177189
return await axios.get(url, options).catch(error => {
178190
throw new DataverseException(error.response.status, error.response.data ? error.response.data.message : '')
@@ -185,6 +197,12 @@ export class DataverseClient {
185197
})
186198
}
187199

200+
private async putRequest(url: string, data: string | object, options: { params?: object, headers?: DataverseHeaders } = { headers: this.getHeaders() }): Promise<AxiosResponse> {
201+
return await axios.put(url, JSON.stringify(data), options).catch(error => {
202+
throw new DataverseException(error.response.status, error.response.data ? error.response.data.message : '')
203+
})
204+
}
205+
188206
private getHeaders(): DataverseHeaders {
189207
return {
190208
'X-Dataverse-key': this.apiToken ? this.apiToken : ''

test/dataverseClient.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('DataverseClient', () => {
2727

2828
let axiosGetStub: SinonStub
2929
let axiosPostStub: SinonStub
30+
let axiosPutStub: SinonStub
3031
let requestPostStub: SinonStub
3132

3233
let mapBasicDatasetInformationStub: SinonStub
@@ -62,6 +63,7 @@ describe('DataverseClient', () => {
6263

6364
axiosGetStub = sandbox.stub(axios, 'get').resolves(mockResponse)
6465
axiosPostStub = sandbox.stub(axios, 'post').resolves(mockResponse)
66+
axiosPutStub = sandbox.stub(axios, 'put').resolves(mockResponse)
6567
requestPostStub = sandbox.stub(request, 'post').resolves(mockResponse)
6668

6769
mapBasicDatasetInformationStub = sandbox.stub(DatasetUtil, 'mapBasicDatasetInformation').returns(mockDatasetInformation)
@@ -1481,4 +1483,31 @@ describe('DataverseClient', () => {
14811483
})
14821484
})
14831485
})
1484-
})
1486+
1487+
describe('updateDataset()', () => {
1488+
it('should call axios with expected url', async () => {
1489+
const datasetId = random.number().toString()
1490+
const datasetInformation: BasicDatasetInformation = {
1491+
title: 's',
1492+
descriptions: [{ text: 'some ', date: '2019-09-09' }],
1493+
authors: [
1494+
{
1495+
fullname: 'tester tests'
1496+
}
1497+
],
1498+
contact: [{ email: 'tai@theagilemonkeys.com', fullname: 'Tai Nguyen' }],
1499+
subject: [DatasetSubjects.AGRICULTURAL_SCIENCE]
1500+
}
1501+
1502+
await client.updateDataset(datasetId, datasetInformation)
1503+
1504+
assert.calledOnce(axiosPutStub)
1505+
assert.calledWithExactly(axiosPutStub, `${host}/api/datasets/:persistentId/versions/:draft?persistentId=${datasetId}`, {
1506+
headers: {
1507+
'X-Dataverse-key': apiToken,
1508+
'Content-Type': 'application/json'
1509+
}
1510+
})
1511+
})
1512+
})
1513+
})

0 commit comments

Comments
 (0)