-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.js
66 lines (61 loc) · 2.04 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import axios from 'axios'
const url = Drupal.settings.campaignion_email_to_target.endpoints['e2t-api'].url + '/'
const datasetQuery = Drupal.settings.campaignion_email_to_target.datasetQuery
const headers = {
Authorization: 'Bearer ' + Drupal.settings.campaignion_email_to_target.endpoints['e2t-api'].token
}
export default {
/**
* Get the available datasets.
* @return {Promise} axios response.
*/
getDatasets () {
return axios.get(url + datasetQuery, {headers})
},
/**
* Get the list of contacts for a given dataset.
* @param {string} datasetKey - Unique identifier for the dataset.
* @return {Promise} axios response.
*/
getContacts (datasetKey) {
return axios.get(url + datasetKey + '/contact', {headers})
},
/**
* Persist a dataset: Create a new one or update an existing one.
* @param {Object} dataset - The dataset to persist.
* @param {boolean} createNew - Flag indicating that a new dataset should be created.
* @return {Promise} axios response.
*/
saveDataset (dataset, createNew) {
return axios({
method: createNew ? 'post' : 'put',
url: createNew ? url : url + dataset.key,
data: JSON.stringify(dataset),
headers,
transformRequest: [function (data, headers) {
headers.post = {'Content-Type': 'application/json'}
headers.put = {'Content-Type': 'application/json'}
return data
}]
})
},
/**
* Persist a list of contacts with a given dataset, replacing the existing list.
* @param {string} datasetKey - Unique identifier of the dataset.
* @param {Object[]} contacts - The array of contact objects.
* @return {Promise} axios response.
*/
saveContacts (datasetKey, contacts) {
return axios({
method: 'put',
url: url + datasetKey + '/contact',
data: JSON.stringify(contacts),
headers,
transformRequest: [function (data, headers) {
headers.post = {'Content-Type': 'application/json'}
headers.put = {'Content-Type': 'application/json'}
return data
}]
})
}
}