-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (67 loc) · 2.25 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
73
74
75
76
77
78
79
80
81
82
83
84
85
const axios = require('axios');
const querystring = require('querystring');
class Bkam {
constructor(config){
(this.currency_key = config.currency_key),
(this.bonds_key = config.bonds_key),
(this.basePath = 'https://api.centralbankofmorocco.ma');
}
request(service = '', options = {}) {
const url = this.basePath + service;
if (options.api_type === 'BDT') {
if (this.bonds_key === undefined) this.bonds_key = "";
var headers = {headers: {
'Ocp-Apim-Subscription-Key': this.bonds_key,
'Content-type': 'application/json'
}}
} else {
if (this.currency_key === undefined) this.currency_key = "";
var headers = {headers: {
'Ocp-Apim-Subscription-Key': this.currency_key,
'Content-type': 'application/json'
}}
}
const config = {
url,
...options,
...headers
};
return axios.request(config).then(r => {
if (r.status === 200) {
return r.data
}
}).catch(err => {
if (err.response.status === 401){
return {message: 'Make sure you added a correct API key for each service'}
} else {
return {message: 'error occurred'}
}
})
};
getCoursBBE(options) {
const qs = options ? '?' + querystring.stringify(options) : '';
const url = '/cours/Version1/api/CoursBBE' + qs;
const config = {
method: 'GET'
};
return this.request(url, config);
}
getCoursVIR(options) {
const qs = options ? '?' + querystring.stringify(options) : '';
const url = '/cours/Version1/api/CoursVirement' + qs;
const config = {
method: 'GET'
};
return this.request(url, config);
}
getCourbeBDT(options) {
const qs = options ? '?' + querystring.stringify(options) : '';
const url = '/mo/Version1/api/CourbeBDT' + qs;
const config = {
method: 'GET',
api_type: 'BDT'
};
return this.request(url, config);
}
}
module.exports = Bkam;