-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
125 lines (108 loc) · 2.95 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
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
import axios from 'axios';
import Namespaces from './modules/namespaces.js';
import Accounts from './modules/accounts.js';
import Health from './modules/health.js';
import ServicesProviders from './modules/servicesProviders.js';
import Services from './modules/services.js';
import dns from './modules/dns.js';
import Settings from './modules/settings.js';
import Instances from './modules/instances.js';
import Plans from './modules/plans.js';
import Transactions from './modules/transactions.js';
import Events from "./modules/events";
import InstanceGroupService from "./modules/instancesGroupService";
import Logging from "./modules/logging.js";
import Showcases from './modules/showcases.js';
import Reports from './modules/reports.js';
class Api{
constructor(host = '/', port = undefined){
this.axios = axios.create({})
if(host[0] != '/'){
const _host = new URL(host);
if(port){
_host.port = port;
}
this.axios.defaults.baseURL = _host.href;
} else {
this.axios.defaults.baseURL = host;
}
this.namespaces = new Namespaces(this);
this.accounts = new Accounts(this);
this.health = new Health(this);
this.servicesProviders = new ServicesProviders(this);
this.services = new Services(this);
this.dns = new dns(this);
this.settings = new Settings(this);
this.instances = new Instances(this);
this.plans = new Plans(this);
this.transactions = new Transactions(this);
this.events = new Events(this);
this.instanceGroupService = new InstanceGroupService(this);
this.logging = new Logging(this);
this.showcases = new Showcases(this);
this.reports = new Reports(this);
}
request(type, url, data = {}){
return new Promise((resolve, reject) => {
this.axios[type](url, data)
.then(res => resolve(res.data))
.catch(reject)
})
}
post(url, data){
return this.request('post', url, data);
}
put(url, data){
return this.request('put', url, data);
}
patch(url, data){
return this.request('patch', url, data);
}
get(url, params = {}){
return this.request('get', url, params);
}
delete(url, params = {}){
return this.request('delete', url, params);
}
authorizeCustom(auth){
return this.post('/token', auth)
}
authorizeStandard(username, password, root_claim){
return this.authorizeCustom({
"auth": {
"type": "standard",
"data": [username, password]
},
root_claim
})
}
authorizeWithType(username, password, type, root_claim){
return this.authorizeCustom({
"auth": {
type,
"data": [username, password]
},
root_claim
})
}
auth(username, password){
return new Promise((resolve, reject) => {
this.authorizeStandard(username, password)
.then(res => {
this.applyToken(res.token)
resolve(res)
})
.catch(reject)
})
}
applyToken(token){
this.axios = this.axios.create({
headers: {'Authorization': 'Bearer ' + token}
})
}
health(){
console.warn('method depricated. use instance.health.probe() function');
return null
}
}
export default Api;