forked from macc704/kf6apijs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kf6api.js
90 lines (90 loc) · 2.98 KB
/
kf6api.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
var kf6 = {
token: null,
baseURL: null,
communityId: null,
setBaseURL: function(baseURL) {
this.baseURL = baseURL;
},
setCommunity: function(communityId) {
this.communityId = communityId;
},
login: function(uname, password, success, failure) {
var self = this;
this.connect('POST', 'auth/local/', {
userName: uname,
password: password
}, function(data) {
self.token = data.token;
if (success) {
success();
}
}, failure);
},
getCommunities: function(success, failure) {
this.connect('GET', 'api/users/myRegistrations', {}, success, failure);
},
getCommunity: function(success, failure) {
this.connect('GET', 'api/communities/' + this.communityId, null, success, failure);
},
getMe: function(success, failure) {
this.connect('GET', 'api/users/me', null, success, failure);
},
getMeAsAuthor: function(success, failure) {
this.connect('GET', 'api/authors/' + this.communityId + '/me', null, success, failure);
},
getViews: function(success, failure) {
this.connect('GET', 'api/communities/' + this.communityId + '/views',
null, success, failure);
},
getAuthors: function(success, failure) {
this.connect('GET', 'api/communities/' + this.communityId + '/authors',
null, success, failure);
},
getGroups: function(success, failure) {
this.connect('GET', 'api/communities/' + this.communityId + '/groups',
null, success, failure);
},
getObjectsCount: function(query, success, failure) {
this.connect('POST', 'api/contributions/' + this.communityId + '/search/count', {
query: query
}, success, failure);
},
getObjects: function(query, success, failure) {
this.connect('POST', 'api/contributions/' + this.communityId + '/search', {
query: query
}, success, failure);
},
getRecords: function(query, success, failure) {
this.connect('POST', 'api/records/search/' + this.communityId, {
query: query
}, success, failure);
},
getLinksFrom: function(fromId, success, failure) {
this.connect('GET', 'api/links/from/' + fromId, null, success, failure);
},
connect: function(method, path, data, success, failure) {
var url = this.getURL(path);
var req = {
url: url,
type: method,
headers: {
'authorization': 'Bearer ' + this.token
}
};
if (data) {
req.contentType = 'application/json';
req.dataType = 'json';
req.data = JSON.stringify(data);
}
req.cache = false;
$.ajax(req)
.done(success)
.fail(failure);
},
getURL: function(path) {
if (!this.baseURL) {
throw 'error';
}
return this.baseURL + path;
},
}