-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathact.web.api.js
97 lines (89 loc) · 2.15 KB
/
act.web.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
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
import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http';
class ActWebAPI {
/**
* Act! Web API Interface
* @constructor
*/
constructor() {
const { username, password, database, endpoint } = Meteor.settings.act;
this.username = username;
this.password = password;
this.database = database;
this.endpoint = endpoint;
this.token = this.authorize();
this.methods = {
contacts: {
findOne(params) {
return {
type: 'GET',
endpoint: `api/Contacts/${params.id}`
}
},
find() {
return {
type: 'GET',
endpoint: 'api/Contacts'
}
},
insert() {
return {
type: 'POST',
endpoint: 'api/Contacts/'
}
},
update(params) {
return {
type: 'PUT',
endpoint: `api/Contacts/${params.id}`
}
},
remove(params) {
return {
type: 'DELETE',
endpoint: `api/Contacts/${params.id}`
}
}
}
};
}
request(action, args) {
const url = `https://${this.endpoint}/act.web.api/${action.endpoint}`;
try {
return HTTP.call(action.type, url, args);
} catch (error) {
return error;
}
}
authorize() {
var opts = {
auth: `${this.username}:${this.password}`,
headers: {
'Act-Database-Name': this.database
}
}
const response = this.request({type :'GET', endpoint: 'authorize'}, opts);
if (response.content) {
return response.content;
} else {
throw new Meteor.Error(500, 'Act! Token not received.');
}
}
action(action, method, params) {
const methodToCall = this.methods[action][method](params);
var opts = {
headers: {
'Authorization': `Bearer ${this.token}`
}
};
return this.request(methodToCall, opts);
}
/**
* Interact with the Contacts endpoint
* @param {Function} method find, findOne, insert, update, remove
*/
contacts(method, params) {
return this.action('contacts', method, params);
}
}
export { ActWebAPI };