-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollectiveTimesApiClient.js
57 lines (48 loc) · 1.61 KB
/
CollectiveTimesApiClient.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
import axios from 'axios';
export default class CollectiveTimesApiClient {
static get API_ENDPOINT() { return "https://collective-times-api.herokuapp.com"; };
async login(username, password, callback) {
const res = await axios.post(`${CollectiveTimesApiClient.API_ENDPOINT}/oauth/token`,
{
grant_type: 'password',
client_id: 1,
client_secret: 'your_secret',
username: username,
password: password
});
if (res.status === 200) {
callback(res.data);
} else {
callback({});
}
}
async getArticles(page, callback) {
const res = await axios.get(`${CollectiveTimesApiClient.API_ENDPOINT}/v1/articles?page=${page}`);
if (res.status === 200) {
callback(res.data.articles);
} else {
callback([]);
}
};
async saveVisitedArticleBy(token, articleId) {
let headers = {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
};
if(token){
headers['headers']['Authorization'] = `Bearer ${token}`;
}
const res = await axios.post(`${CollectiveTimesApiClient.API_ENDPOINT}/v1/histories`,
{
article_id: articleId
},
headers);
if (res.status === 200) {
console.log('success');
} else {
console.log('fail');
}
};
}