-
Notifications
You must be signed in to change notification settings - Fork 0
/
toggl.js
144 lines (136 loc) · 4.33 KB
/
toggl.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const axios = require('axios');
// Validate the provided apiToken by ensuring we can
// authenticate a request to the Toggl api.
const validateApiToken = (apiToken) => {
// Make a request to the 'me' endpoint to get the
// profile of the user owning the provided apiToken.
return axios({
method: 'get',
url: 'https://www.toggl.com/api/v8/me',
auth: {
// Per Toggl API docs supply the apiToken as the
// username, and a string of 'api_token' for the password.
// Reference: https://github.com/toggl/toggl_api_docs/blob/master/chapters/authentication.md
username: apiToken,
password: 'api_token',
},
})
.then((response) => {
let validatedApiToken = response.data.data.api_token;
if (validatedApiToken === apiToken) {
return {success: true};
} else {
// This implies that the request was authenticated
// but for some reason the api_tokens don't match.
// This should be highly unlikely.
return {success: false};
}
})
.catch((error) => {
return {error: error.message};
});
};
const getUserWorkspaces = (apiToken) => {
return axios({
method: 'get',
url: 'https://www.toggl.com/api/v8/workspaces',
auth: {
// Per Toggl API docs supply the apiToken as the
// username, and a string of 'api_token' for the password.
// Reference: https://github.com/toggl/toggl_api_docs/blob/master/chapters/authentication.md
username: apiToken,
password: 'api_token',
},
})
.then((response) => {
return response.data;
})
.catch((error) => {
return {error: error.message};
});
};
const getWorkspaceProjects = (apiToken, workspaceId) => {
return axios({
method: 'get',
url: `https://www.toggl.com/api/v8/workspaces/${workspaceId}/projects`,
auth: {
// Per Toggl API docs supply the apiToken as the
// username, and a string of 'api_token' for the password.
// Reference: https://github.com/toggl/toggl_api_docs/blob/master/chapters/authentication.md
username: apiToken,
password: 'api_token',
},
})
.then((response) => {
return response.data;
})
.catch((error) => {
return {error: error.message};
});
};
const createProject = (apiToken, projectName, workspaceId) => {
return axios({
method: 'post',
url: 'https://www.toggl.com/api/v8/projects',
auth: {
// Per Toggl API docs supply the apiToken as the
// username, and a string of 'api_token' for the password.
// Reference: https://github.com/toggl/toggl_api_docs/blob/master/chapters/authentication.md
username: apiToken,
password: 'api_token',
},
headers: {'Content-Type': 'application/json'},
data: {
project: {
name: projectName,
wid: workspaceId,
},
},
}).then((response) => {
return response.data.data;
});
};
const startTimer = (apiToken, projectId) => {
return axios({
method: 'post',
url: 'https://www.toggl.com/api/v8/time_entries/start',
auth: {
// Per Toggl API docs supply the apiToken as the
// username, and a string of 'api_token' for the password.
// Reference: https://github.com/toggl/toggl_api_docs/blob/master/chapters/authentication.md
username: apiToken,
password: 'api_token',
},
headers: {'Content-Type': 'application/json'},
data: {
time_entry: {
pid: projectId,
created_with: 'Autoggl Code Extension',
},
},
}).then((response) => {
return response.data.data;
});
};
const stopTimer = (apiToken, timeEntryId) => {
return axios({
method: 'put',
url: `https://www.toggl.com/api/v8/time_entries/${timeEntryId}/stop`,
auth: {
// Per Toggl API docs supply the apiToken as the
// username, and a string of 'api_token' for the password.
// Reference: https://github.com/toggl/toggl_api_docs/blob/master/chapters/authentication.md
username: apiToken,
password: 'api_token',
},
headers: {'Content-Type': 'application/json'},
}).then((response) => {
return response.data.data;
});
};
exports.validateApiToken = validateApiToken;
exports.getUserWorkspaces = getUserWorkspaces;
exports.getWorkspaceProjects = getWorkspaceProjects;
exports.createProject = createProject;
exports.startTimer = startTimer;
exports.stopTimer = stopTimer;