-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
72 lines (66 loc) · 1.95 KB
/
index.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
require('dotenv').config();
const rp = require('request-promise');
const logger = require('winston');
const Planning = require('./lib/planning');
const User = require('./lib/user');
const Projects = require('./lib/projects');
const Units = require('./lib/units.js');
const Events = require('./lib/events.js');
logger.level = process.env.LOG_LEVEL || 'info';
class Intranet {
constructor(autologinToken, login) {
this.login = login;
this.autologinToken = autologinToken;
this.baseUrl = `https://intra.epitech.eu/${autologinToken}`;
this.planning = new Planning(this);
this.user = new User(this);
this.projects = new Projects(this);
this.units = new Units(this);
this.events = new Events(this);
// Instances list
// This is not an exhaustive list as there is no known endpoints to feth instances
this.instances = [
'FR', // global instance
'FR/LYN', // Lyon
'FR/MAR', // Marseille
'FR/PAR', // Paris
'FR/NCY', // Nancy
'FR/LIL', // Lille
];
}
fetch(endpoint, data = {}) {
logger.info(`GET ${endpoint} - ${JSON.stringify(data)}`);
return new Promise((resolve, reject) => {
rp({
uri: `${this.baseUrl}${endpoint}?format=json`,
method: 'GET',
qs: data,
json: true,
}).then((body) => {
logger.debug(body);
resolve(body);
}).catch((err) => {
logger.error(`${err.name} - ${err.message}`);
reject(err);
});
});
}
submit(endpoint, data = {}) {
logger.info(`POST ${endpoint} - ${JSON.stringify(data)}`);
return new Promise((resolve, reject) => {
rp({
url: `${this.baseUrl}${endpoint}?format=json`,
method: 'POST',
body: data,
json: true,
}).then((body) => {
logger.debug(body);
resolve(body);
}).catch((err) => {
logger.error(`${err.name} - ${err.message}`);
reject(err);
});
});
}
}
module.exports = Intranet;