Skip to content

Commit c0cde94

Browse files
committed
Merge branch 'master' of github.com:IQSS/dataverse-client-javascript
2 parents bb0f55d + 5aac40f commit c0cde94

4 files changed

Lines changed: 95 additions & 318 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ 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+
66+
`public async deleteDataset(datasetId: string): Promise<AxiosResponse> {`
67+
6468
## Build project
6569

6670
In order to build the project, we need to run the following command:
@@ -108,4 +112,4 @@ Automated publishing of versions could be automated when merging to master. Belo
108112
5. Publish, `npm publish`
109113

110114
## Contributing
111-
[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: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,23 @@ 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+
188+
public async deleteDataset(datasetId: string): Promise<AxiosResponse> {
189+
const url = `${this.host}/api/datasets/:persistentId/destroy/?persistentId=${datasetId}`
190+
return this.deleteRequest(url)
191+
}
192+
176193
private async getRequest(url: string, options: { params?: object, headers?: DataverseHeaders, responseType?: ResponseType } = { headers: this.getHeaders() }): Promise<AxiosResponse> {
177194
return await axios.get(url, options).catch(error => {
178195
throw new DataverseException(error.response.status, error.response.data ? error.response.data.message : '')
@@ -185,6 +202,18 @@ export class DataverseClient {
185202
})
186203
}
187204

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+
211+
private async deleteRequest(url: string, options: { params?: object, headers?: DataverseHeaders, responseType?: ResponseType } = { headers: this.getHeaders() }): Promise<AxiosResponse> {
212+
return await axios.delete(url, options).catch(error => {
213+
throw new DataverseException(error.response.status, error.response.data ? error.response.data.message : '')
214+
})
215+
}
216+
188217
private getHeaders(): DataverseHeaders {
189218
return {
190219
'X-Dataverse-key': this.apiToken ? this.apiToken : ''
@@ -205,4 +234,4 @@ export class DataverseClient {
205234
fq: options.fq
206235
}
207236
}
208-
}
237+
}

test/dataverseClient.spec.ts

Lines changed: 47 additions & 2 deletions
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,7 +27,9 @@ describe('DataverseClient', () => {
2727

2828
let axiosGetStub: SinonStub
2929
let axiosPostStub: SinonStub
30+
let axiosPutStub: SinonStub
3031
let requestPostStub: SinonStub
32+
let axiosDeleteStub: SinonStub
3133

3234
let mapBasicDatasetInformationStub: SinonStub
3335
let getErrorMessageStub: SinonStub
@@ -62,7 +64,9 @@ describe('DataverseClient', () => {
6264

6365
axiosGetStub = sandbox.stub(axios, 'get').resolves(mockResponse)
6466
axiosPostStub = sandbox.stub(axios, 'post').resolves(mockResponse)
67+
axiosPutStub = sandbox.stub(axios, 'put').resolves(mockResponse)
6568
requestPostStub = sandbox.stub(request, 'post').resolves(mockResponse)
69+
axiosDeleteStub = sandbox.stub(axios, 'delete').resolves(mockResponse)
6670

6771
mapBasicDatasetInformationStub = sandbox.stub(DatasetUtil, 'mapBasicDatasetInformation').returns(mockDatasetInformation)
6872
getErrorMessageStub = sandbox.stub(ResponseUtil, 'getErrorMessage').returns(mockErrorMessage)
@@ -1480,5 +1484,46 @@ describe('DataverseClient', () => {
14801484
expect(error.errorCode).to.be.equal(errorCode)
14811485
})
14821486
})
1487+
14831488
})
1484-
})
1489+
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+
1518+
describe('deleteDataset()', () => {
1519+
it('should call axios with expected url', async () => {
1520+
const datasetId: string = random.number().toString()
1521+
1522+
await client.deleteDataset(datasetId)
1523+
1524+
assert.calledOnce(axiosDeleteStub)
1525+
assert.calledWithExactly(axiosDeleteStub, `${host}/api/datasets/:persistentId/destroy/?persistentId=${datasetId}`, { headers: { 'X-Dataverse-key': apiToken } })
1526+
})
1527+
})
1528+
1529+
})

0 commit comments

Comments
 (0)