Skip to content

Commit 5aac40f

Browse files
authored
Merge pull request #32 from joenio/dataset-update
added `updateDataset(...)` to update metadata dataset
2 parents 8b224a4 + 2015691 commit 5aac40f

3 files changed

Lines changed: 53 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
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
`public async deleteDataset(datasetId: string): Promise<AxiosResponse> {`
6567

6668
## Build project
@@ -110,4 +112,4 @@ Automated publishing of versions could be automated when merging to master. Belo
110112
5. Publish, `npm publish`
111113

112114
## Contributing
113-
[If you are interested in contributing, please click here](/CONTRIBUTING.md)
115+
[If you are interested in contributing, please click here](/CONTRIBUTING.md)

src/dataverseClient.ts

Lines changed: 19 additions & 1 deletion
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
public async deleteDataset(datasetId: string): Promise<AxiosResponse> {
177189
const url = `${this.host}/api/datasets/:persistentId/destroy/?persistentId=${datasetId}`
178190
return this.deleteRequest(url)
@@ -190,6 +202,12 @@ export class DataverseClient {
190202
})
191203
}
192204

205+
private async putRequest(url: string, data: string | object, options: { params?: object, headers?: DataverseHeaders } = { headers: this.getHeaders() }): Promise<AxiosResponse> {
206+
return await axios.put(url, JSON.stringify(data), options).catch(error => {
207+
throw new DataverseException(error.response.status, error.response.data ? error.response.data.message : '')
208+
})
209+
}
210+
193211
private async deleteRequest(url: string, options: { params?: object, headers?: DataverseHeaders, responseType?: ResponseType } = { headers: this.getHeaders() }): Promise<AxiosResponse> {
194212
return await axios.delete(url, options).catch(error => {
195213
throw new DataverseException(error.response.status, error.response.data ? error.response.data.message : '')
@@ -216,4 +234,4 @@ export class DataverseClient {
216234
fq: options.fq
217235
}
218236
}
219-
}
237+
}

test/dataverseClient.spec.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect } from 'chai'
44
import axios from 'axios'
55
import request from 'request-promise'
66
import path from 'path'
7-
import { internet, random, system } from 'faker'
7+
import { internet, random, system, name, date } from 'faker'
88
import { DataverseException } from '../src/exceptions/dataverseException'
99
import { DataverseMetricType } from '../src/@types/dataverseMetricType'
1010
import { DatasetSubjects } from '../src/@types/datasetSubjects'
@@ -27,6 +27,7 @@ describe('DataverseClient', () => {
2727

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

@@ -63,6 +64,7 @@ describe('DataverseClient', () => {
6364

6465
axiosGetStub = sandbox.stub(axios, 'get').resolves(mockResponse)
6566
axiosPostStub = sandbox.stub(axios, 'post').resolves(mockResponse)
67+
axiosPutStub = sandbox.stub(axios, 'put').resolves(mockResponse)
6668
requestPostStub = sandbox.stub(request, 'post').resolves(mockResponse)
6769
axiosDeleteStub = sandbox.stub(axios, 'delete').resolves(mockResponse)
6870

@@ -1485,6 +1487,34 @@ describe('DataverseClient', () => {
14851487

14861488
})
14871489

1490+
describe('updateDataset()', () => {
1491+
it('should call axios with expected url', async () => {
1492+
const datasetId = random.number().toString()
1493+
const datasetInformation: BasicDatasetInformation = {
1494+
title: random.words(),
1495+
descriptions: [{ text: random.words(), date: date.recent().toString() }],
1496+
authors: [
1497+
{
1498+
fullname: name.findName()
1499+
}
1500+
],
1501+
contact: [{ email: internet.email(), fullname: name.findName() }],
1502+
subject: [DatasetSubjects.AGRICULTURAL_SCIENCE]
1503+
}
1504+
1505+
await client.updateDataset(datasetId, datasetInformation)
1506+
1507+
assert.calledOnce(axiosPutStub)
1508+
const payload: any = DatasetUtil.mapBasicDatasetInformation(datasetInformation)
1509+
assert.calledWithExactly(axiosPutStub, `${host}/api/datasets/:persistentId/versions/:draft?persistentId=${datasetId}`, JSON.stringify(payload.datasetVersion), {
1510+
headers: {
1511+
'X-Dataverse-key': apiToken,
1512+
'Content-Type': 'application/json'
1513+
}
1514+
})
1515+
})
1516+
})
1517+
14881518
describe('deleteDataset()', () => {
14891519
it('should call axios with expected url', async () => {
14901520
const datasetId: string = random.number().toString()

0 commit comments

Comments
 (0)