Permalink
Browse files

Merge pull request #56 from MarceloEmmerich/master

Custom headers per method
  • Loading branch information...
2 parents a077c96 + 082ecb8 commit 8bab421edbe22b70c9ac3ae8baf6b11e6601c0a1 @mansilladev mansilladev committed Dec 7, 2012
Showing with 88 additions and 1 deletion.
  1. +21 −1 app.js
  2. +6 −0 public/data/apiconfig.json
  3. +57 −0 public/data/requestbin.json
  4. +4 −0 views/api.jade
View
@@ -276,7 +276,9 @@ function processRequest(req, res, next) {
};
var reqQuery = req.body,
+ customHeaders = {},
params = reqQuery.params || {},
+ locations = reqQuery.locations || {},
methodURL = reqQuery.methodUri,
httpMethod = reqQuery.httpMethod,
apiKey = reqQuery.apiKey,
@@ -285,6 +287,19 @@ function processRequest(req, res, next) {
apiConfig = apisConfig[apiName],
key = req.sessionID + ':' + apiName;
+ // Extract custom headers from the params
+ for( var param in params )
+ {
+ if (params.hasOwnProperty(param))
+ {
+ if (params[param] !== '' && locations[param] == 'header' )
+ {
+ customHeaders[param] = params[param];
+ delete params[param];
+ }
+ }
+ }
+
// Replace placeholders in the methodURL with matching params
for (var param in params) {
if (params.hasOwnProperty(param)) {
@@ -306,11 +321,16 @@ function processRequest(req, res, next) {
var baseHostInfo = apiConfig.baseURL.split(':');
var baseHostUrl = baseHostInfo[0],
baseHostPort = (baseHostInfo.length > 1) ? baseHostInfo[1] : "";
+ var headers = {};
+ for( header in apiConfig.headers )
+ headers[header] = apiConfig.headers[header];
+ for( header in customHeaders )
+ headers[header] = customHeaders[header];
var paramString = query.stringify(params),
privateReqURL = apiConfig.protocol + '://' + apiConfig.baseURL + apiConfig.privatePath + methodURL + ((paramString.length > 0) ? '?' + paramString : ""),
options = {
- headers: apiConfig.headers,
+ headers: headers,
protocol: apiConfig.protocol + ':',
host: baseHostUrl,
port: baseHostPort,
@@ -32,5 +32,11 @@
"crypt": "HMAC-SHA1"
},
"keyParam":""
+ },
+ "requestbin": {
+ "name": "Requestb.in",
+ "protocol": "http",
+ "baseURL": "requestb.in",
+ "publicPath": "/"
}
}
@@ -0,0 +1,57 @@
+{
+ "endpoints":[
+ {
+ "name":"Requestb.in",
+ "methods":[
+ {
+ "MethodName":"Method with custom headers",
+ "HTTPMethod":"GET",
+ "URI":":request_bin",
+ "RequiresOAuth":"N",
+ "parameters":[
+ {
+ "Name":"request_bin",
+ "Required":"Y",
+ "Default":"",
+ "Location":"query",
+ "Type":"string",
+ "Description":"The request bin id to use. Go to http://requestb.in to get one for free."
+ },
+ {
+ "Name":"Cache-Control",
+ "Required":"Y",
+ "Default":"no-cache",
+ "Location":"header",
+ "Type":"string",
+ "Description":""
+ },
+ {
+ "Name":"Max-Forwards",
+ "Required":"Y",
+ "Default":"10",
+ "Location":"header",
+ "Type":"string",
+ "Description":""
+ },
+ {
+ "Name":"param1",
+ "Required":"Y",
+ "Default":"this is ithe first param",
+ "Location":"query",
+ "Type":"string",
+ "Description":"First param, with explicitly set location property."
+ },
+ {
+ "Name":"param2",
+ "Required":"Y",
+ "Default":"this is the second param",
+ "Type":"string",
+ "Description":"Second param, with no location property set. Should default to 'query'."
+ }
+ ]
+ }
+ ]
+ }
+ ]
+}
+
View
@@ -84,6 +84,7 @@ ul
th Parameter
th Value
th Type
+ th Location
th Description
tbody
- var paramCount =0
@@ -119,7 +120,10 @@ ul
option(value=choice) #{choice}
- else
input(name='params[' + parameter.Name + ']', value=parameter.Default, placeholder=className)
+ input(type='hidden', name='locations['+parameter.Name+']', value=parameter.Location || 'query')
td.type=parameter.Type
+ td.location
+ p=parameter.Location || 'query'
td.description
p=parameter.Description || 'No description'
- if (parameter.Type =='enumerated' && parameter.EnumeratedDescription)

0 comments on commit 8bab421

Please sign in to comment.