-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (115 loc) · 4.51 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
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
126
127
128
129
130
131
132
133
134
135
'use strict';
const http = require('http'),
https = require('https'),
GET = require("lodash.get");
class shaidkit {
constructor(options = {}){
this.entities = {
"application": "application",
"application/approval/vendor": "application/approval/vendor",
"application/approval/sdlc": "application/approval/sdlc",
"category": "category",
"country": "country",
"permission": "permission",
"service": "service",
"vendor": "vendor",
"vendor/key": "vendor/key"
};
if (options.base_url !== null && options.base_url !== undefined) {
this.base_url = new URL(options.base_url);
this.hostname = this.base_url.hostname || "shaid.smartdevicelink.com";
this.port = this.base_url.port;
this.protocol = this.base_url.protocol || "https:";
} else {
this.hostname = "shaid.smartdevicelink.com";
this.protocol = "https:";
}
this.version = "v" + options.version;
this.secret_key = options.secret_key || null;
this.public_key = options.public_key || null;
this.sdl_server_version = options.sdl_server_version || null;
if(isNaN(options.version)){
throw new Error("'version' must be a valid integer. e.g. 2");
}
if(!(this.secret_key && this.public_key)){
throw new Error("'public_key' and 'secret_key' must be provided");
}
return this;
}
get entity() {
return this.entities;
}
read(entity, params, callback){
this.makeRequest("GET", entity, params, callback).end();
}
create(entity, params, callback){
this.makeRequest("POST", entity, params, callback).end(JSON.stringify(params), 'utf8');
}
update(entity, params, callback){
this.makeRequest("PUT", entity, params, callback).end(JSON.stringify(params), 'utf8');
}
delete(entity, params, callback){
this.makeRequest("DELETE", entity, params, callback).end(JSON.stringify(params), 'utf8');
}
validateEntity(entity){
var is_valid = this.entities[entity] ? true : false;
if(!is_valid){
throw new Error(entity + " is not a valid entity.");
}
return is_valid;
}
parseQueries(params) {
return Object.keys(params).length === 0 ? "" : "?" + Object.entries(params).map((kvPair) => {
return kvPair[0] + "=" + kvPair[1];
}).join("&");
}
makeRequest(verb, entity, params, callback){
this.validateEntity(entity);
const queryString = (verb == "GET") ? this.parseQueries(params) : ''
const path = "/api/" + this.version + "/" + entity + queryString;
const options = {
"method": verb,
"host": this.hostname,
"port": this.port,
"path": path,
"timeout": 10000,
"headers": {
"public_key": this.public_key,
"secret_key": this.secret_key,
"sdl_server_version": this.sdl_server_version,
},
}
if (verb != "GET") {
options.headers["Content-Type"] = "application/json; charset=UTF-8";
}
function responseCallback (response) {
let aggregateResponse = '';
response.setEncoding('utf8');
response.on('data', (chunk) => {
// getting response back from SHAID
aggregateResponse += chunk;
});
response.on('end', () => {
if (response.statusCode >= 200 && response.statusCode < 300) {
// success
callback(null, JSON.parse(aggregateResponse));
} else if (response.statusCode >= 400 && response.statusCode <= 403) {
// handled error
callback(response.statusCode, JSON.parse(aggregateResponse));
} else {
// unexpected API error
callback(GET(aggregateResponse, "meta.message") || "Unexpected SHAID error", aggregateResponse);
}
});
}
function errorCallback (err) {
callback("Network Error", err);
}
if (this.protocol === "https:") {
return https.request(options, responseCallback).on("error", errorCallback);
} else {
return http.request(options, responseCallback).on("error", errorCallback);
}
}
}
module.exports = shaidkit;