-
Notifications
You must be signed in to change notification settings - Fork 1
/
RequestLogger.js
39 lines (33 loc) · 1.16 KB
/
RequestLogger.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
const useragent = require('useragent');
function getRequestLogObject(req, status, functionName) {
const agent = useragent.parse(req.headers['user-agent']);
const requestLog = {
date: new Date().toISOString(),
device: agent.device.toString(),
platform_version: agent.os.toString(),
version: agent.toString(),
locale: req.headers['accept-language'],
user_ip: req.headers['x-forwarded-for'],
podcast_type: 'itunes',
function: functionName,
status: status,
};
// let keys = [...Object.keys(req.headers)];
// console.log('Request headers: ', keys);
return requestLog;
};
exports.logRequestCall = (req, status, functionName, injectedDatastore) => {
const requestCallEntitry = getRequestLogObject(req, status, functionName);
const entityKey = injectedDatastore.key('podcast_analytics');
const entity = {
key: entityKey,
data: requestCallEntitry,
};
return injectedDatastore.insert(entity).then(() => {
console.log('RequestCall logged successfully');
return 0;
}).catch(err => {
console.error(err);
return 1;
});
}