-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (83 loc) · 3.18 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
console.log("Hello there!");
const express = require('express');
var fetch = require('node-fetch');
require('dotenv').config();
const app = express();
app.listen(3000,() => console.log('listening at 3000 rn')); // works on port 3000, http://localhost:3000/
app.use(express.static('public')); // serving what's in "public" folder
app.get('/brands', async (request, response) => { // method get_brands
const fetchResponse = await fetch('https://app.socialinsider.io/api', { // fetch getbrands data from API
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + process.env.API_KEY,
},
body: JSON.stringify({"jsonrpc": "2.0", "id": 0, "method": "partner_api.get_brands", "params": {"projectname" : "Automotive"}})
});
const jsonrpc = await fetchResponse.json();
// console.log(jsonrpc);
response.json(jsonrpc);
});
app.get('/brandData/:startDate/:stopDate/:brandName',async (request, response) => { // method get_brand_data
console.log("Received brandname test in backend from client-side: " + request.params.brandName); //sending parameters through url
const fetchResponse = await fetch('https://app.socialinsider.io/api', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + process.env.API_KEY,
},
body: JSON.stringify(getBrandDataString(request.params.brandName.toString(),{"startDate":request.params.startDate,"stopDate":request.params.stopDate})),
});
const jsonrpc = await fetchResponse.json();
// console.log(jsonrpc);
response.json(jsonrpc);
});
function getBrandDataString(brandname,timestamps) { // return string for API depending on start/stop dates
if(timestamps.startDate != -1 && timestamps.stopDate != -1) // if(start||stop == -1) we return call with "date" param, else without
{
return {
"jsonrpc": "2.0",
"id": 0,
"method": "partner_api.get_brand_data",
"params":{
"projectname":"Automotive",
"brandname": brandname,
"date": {
"start": parseInt(timestamps.startDate),
"end" : parseInt(timestamps.stopDate),
}
}
}
} else {
return {
"jsonrpc": "2.0",
"id": 0,
"method": "partner_api.get_brand_data",
"params":{
"projectname":"Automotive",
"brandname": brandname,
}
}
}
}
/////////////////////////////////////////
/*var request = require('request');
var headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer interviu'
};
var dataString = '{"jsonrpc": "2.0", "id": 0, "method": "partner_api.get_brands", "params": {"projectname" : "Automotive"}}';
var options = {
url: 'https://app.socialinsider.io/api',
method: 'POST',
headers: headers,
body: dataString
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
*/
//request(options, callback);
//////////////////////////////////////////