Last active
January 22, 2016 17:19
-
-
Save Mozu-CS/18867de8245af42195d6 to your computer and use it in GitHub Desktop.
Transform existing ApiContext with Mozu Event Request object header values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Additional headers not present in the Mozu Node SDK constants.js. | |
//These headers are present on some Mozu Event Request objects, but are used mainly for metadata purposes. | |
module.exports = { | |
TENANTDOMAIN: 'tenant-domain', | |
LOCALE: 'locale', | |
CURRENCY: 'currency', | |
CORRELATION: 'correlation' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
POST /mozu.events HTTP/1.1 | |
x-vol-correlation: 45117118f1fb49af9fa0e1a26c5226cf | |
x-vol-tenant: 12080 | |
x-vol-currency: | |
x-vol-locale: | |
x-vol-tenant-domain: t12080.sandbox.mozu.com | |
x-vol-site: 16771 | |
x-vol-catalog: 2 | |
x-vol-master-catalog: 1 | |
Date: Fri, 22 Jan 2016 17:18:27 GMT | |
x-vol-hmac-sha256: 7usmeWKHe4BP35ZXZEHv+IU5SAh662qov3J13s2WaLg= | |
Content-Type: application/json; charset=utf-8 | |
Host: f7fdfe78.ngrok.io | |
Content-Length: 208 | |
X-Forwarded-Proto: https | |
X-Forwarded-For: | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//We'll use the constants enum provided by the Mozu Node SDK to handle headers from Mozu event requests. | |
var constants = require('mozu-node-sdk/constants'); | |
//Here, we alias the portion of the constants we'll use. | |
var headerPrefix = constants.headerPrefix; | |
var headers = constants.headers; | |
//We add additional header constants for optional values not needed by the Mozu Node SDK | |
var eventHeaderConstants = require('./event-header-constants'); | |
//And using one of the utility functions in the Mozu Node SDK, | |
//we expand the headers in constants.js to include additional event headers sent by Mozu events. | |
var extend = require('mozu-node-sdk/utils/tiny-extend'); | |
headers = extend(headers, eventHeaderConstants); | |
module.exports = function transformHeaders(requestHeaders, apiContext, cb) { | |
//You can optionally choose to throw an error if there are no request headers. | |
//This should be a redundant check as you will have already run your SHA validation against the request. | |
if(requestHeaders) { | |
for(var header in requestHeaders) { | |
var headerValue = requestHeaders[header]; | |
//You can optionally choose to ignore all headers with empty string values. | |
if(headerValue !== '') { | |
switch(header) { | |
//The cases covered by the Mozu Constants file -- | |
//these values are sent as HTTP request headers when present on an apiContext | |
//when creating requests with the Mozu Node SDK | |
case headerPrefix + headers.TENANT: | |
apiContext.context[headers.TENANT] = headerValue; | |
break; | |
case headerPrefix + headers.SITE: | |
apiContext.context[headers.SITE] = headerValue; | |
break; | |
case headerPrefix + headers.MASTERCATALOG: | |
apiContext.context[headers.MASTERCATALOG] = headerValue; | |
break; | |
case headerPrefix + headers.CATALOG: | |
apiContext.context[headers.CATALOG] = headerValue; | |
break; | |
//These headers are optional and more for your usage if needed. | |
//The values here will not be used in construction of request headers sent by the Mozu Node SDK, | |
//but you can utilize these values if needed later in your application. | |
case headerPrefix + headers.DATAVIEWMODE: | |
apiContext.context[headers.DATAVIEWMODE] = headerValue; | |
break; | |
case headerPrefix + headers.VERSION: | |
apiContext.context[headers.VERSION] = headerValue; | |
break; | |
//Now we'll handle the headers we added to the enum -- | |
//these headers are also optional and we're only storing these | |
//in case our application needs any of these values later. | |
case headerPrefix + headers.TENANTDOMAIN: | |
apiContext.context[headers.TENANTDOMAIN] = headerValue; | |
break; | |
case headerPrefix + headers.LOCALE: | |
apiContext.context[headers.LOCALE] = headerValue; | |
break; | |
case headerPrefix + headers.CURRENCY: | |
apiContext.context[headers.CURRENCY] = headerValue; | |
break; | |
case headerPrefix + headers.CORRELATION: | |
apiContext.context[headers.CORRELATION] = headerValue; | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
return cb(null, apiContext); | |
} else { | |
return cb(new Error("Insufficient request headers")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var router = express.Router(); | |
var apiContext = require('mozu-node-sdk/clients/platform/application')(); | |
var isRequestValid = require('mozu-node-sdk/security/is-request-valid'); | |
var transformContext = require('../util/modify-apiContext-with-event-headers'); | |
/* GET /mozu.events */ | |
router.get('/', function(req, res, next) { | |
res.render('events', { title: 'Eventing' }); | |
}); | |
router.use(function(req, res, next) { | |
isRequestValid(apiContext.context, req, function(err) { | |
if(err) { | |
res.status(401); | |
res.render('error', {message: err.message, error: err}); | |
} else { | |
console.log("Validated request."); | |
req.mozu = {isValid: true}; | |
next(); | |
} | |
}); | |
}); | |
/* POST against /mozu.events */ | |
router.post('/', function(req, res, next) { | |
transformContext(req.headers, apiContext, function(err, newApiContext) { | |
if(err) { | |
console.error(err); | |
res.sendStatus(500); | |
} else { | |
console.log("Received POST request..."); | |
console.log(req.headers); | |
console.log(req.body); | |
console.log("Is Valid? " + (req.mozu.isValid ? "\u2713" : "x")); | |
res.sendStatus(200); | |
} | |
}); | |
}); | |
module.exports = router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function transformHeaders(requestHeaders, apiContext, cb) { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//We'll use the constants enum provided by the Mozu Node SDK to handle headers from Mozu event requests. | |
var constants = require('mozu-node-sdk/constants'); | |
//Here, we alias the portion of the constants we'll use. | |
var headerPrefix = constants.headerPrefix; | |
var headers = constants.headers; | |
//We add additional header constants for optional values not needed by the Mozu Node SDK | |
var eventHeaderConstants = require('./event-header-constants'); | |
//And using one of the utility functions in the Mozu Node SDK, | |
//we expand the headers in constants.js to include additional event headers sent by Mozu events. | |
var extend = require('mozu-node-sdk/utils/tiny-extend'); | |
headers = extend(headers, eventHeaderConstants); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function transformHeaders(requestHeaders, apiContext, cb) { | |
//You can optionally choose to throw an error if there are no request headers. | |
//This should be a redundant check as you will have already run your SHA validation against the request. | |
if(requestHeaders) { | |
for(var header in requestHeaders) { | |
var headerValue = requestHeaders[header]; | |
//You can optionally choose to ignore all headers with empty string values. | |
if(headerValue !== '') { | |
switch(header) { | |
//The cases covered by the Mozu Constants file -- | |
//these values are sent as HTTP request headers when present on an apiContext | |
//when creating requests with the Mozu Node SDK | |
case headerPrefix + headers.TENANT: | |
apiContext.context[headers.TENANT] = headerValue; | |
break; | |
case headerPrefix + headers.SITE: | |
apiContext.context[headers.SITE] = headerValue; | |
break; | |
case headerPrefix + headers.MASTERCATALOG: | |
apiContext.context[headers.MASTERCATALOG] = headerValue; | |
break; | |
case headerPrefix + headers.CATALOG: | |
apiContext.context[headers.CATALOG] = headerValue; | |
break; | |
//These headers are optional and more for your usage if needed. | |
//The values here will not be used in construction of request headers sent by the Mozu Node SDK, | |
//but you can utilize these values if needed later in your application. | |
case headerPrefix + headers.DATAVIEWMODE: | |
apiContext.context[headers.DATAVIEWMODE] = headerValue; | |
break; | |
case headerPrefix + headers.VERSION: | |
apiContext.context[headers.VERSION] = headerValue; | |
break; | |
//Now we'll handle the headers we added to the enum -- | |
//these headers are also optional and we're only storing these | |
//in case our application needs any of these values later. | |
case headerPrefix + headers.TENANTDOMAIN: | |
apiContext.context[headers.TENANTDOMAIN] = headerValue; | |
break; | |
case headerPrefix + headers.LOCALE: | |
apiContext.context[headers.LOCALE] = headerValue; | |
break; | |
case headerPrefix + headers.CURRENCY: | |
apiContext.context[headers.CURRENCY] = headerValue; | |
break; | |
case headerPrefix + headers.CORRELATION: | |
apiContext.context[headers.CORRELATION] = headerValue; | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
return cb(null, apiContext); | |
} else { | |
return cb(new Error("Insufficient request headers")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment