Skip to content
This repository has been archived by the owner on Aug 17, 2019. It is now read-only.

Commit

Permalink
Merge pull request mashery#33 from martintajur/master
Browse files Browse the repository at this point in the history
Support for supplying POST, DELETE and PUT parameters in the HTTP request body instead of GET parameters; enumerated descriptions feature.
  • Loading branch information
mansilladev committed Aug 15, 2012
2 parents 0c01913 + 3f1e4c8 commit 7ac39c7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 17 deletions.
11 changes: 9 additions & 2 deletions README.md
Expand Up @@ -379,6 +379,11 @@ You should look at the *./public/data/* directory for examples.
"sugarbombs",
"frostedteeth"
],
"EnumeratedDescription": {
"fruitscoops": "Fruit Scoops (packed with fruit goodness)",
"sugarbombs": "Sugar Bombs (filled with sugar)",
"frostedteeth": "Frosted Teeth (sugar coating)"
},
"Description":"The type of cereal desired"
},
{
Expand Down Expand Up @@ -425,9 +430,11 @@ Line:

24. "EnumeratedList" key value is an array of enumerated values that will render a drop-down (select box) on the form.

25. Each value in the list is a string.
25. "EnumeratedDescription" key value is an object of enumerated values as keys, and their descriptions as values that will be displayed below the Description.

35. "Type" key value is *boolean* that will render a drop-down (select box) on the form for *true* and *false*.
26. Each value in the list is a string.

27. "Type" key value is *boolean* that will render a drop-down (select box) on the form for *true* and *false*.

SUPPORT
=======
Expand Down
53 changes: 39 additions & 14 deletions app.js
Expand Up @@ -315,9 +315,13 @@ function processRequest(req, res, next) {
host: baseHostUrl,
port: baseHostPort,
method: httpMethod,
path: apiConfig.publicPath + methodURL + ((paramString.length > 0) ? '?' + paramString : "")
path: apiConfig.publicPath + methodURL// + ((paramString.length > 0) ? '?' + paramString : "")
};

if (['POST','DELETE','PUT'].indexOf(httpMethod) !== -1) {
var requestBody = query.stringify(params);
}

if (apiConfig.oauth) {
console.log('Using OAuth');

Expand Down Expand Up @@ -468,6 +472,10 @@ function processRequest(req, res, next) {
function unsecuredCall() {
console.log('Unsecured Call');

if (['POST','PUT','DELETE'].indexOf(httpMethod) === -1) {
options.path += ((paramString.length > 0) ? '?' + paramString : "");
}

// Add API Key to params, if any.
if (apiKey != '' && apiKey != 'undefined' && apiKey != undefined) {
if (options.path.indexOf('?') !== -1) {
Expand Down Expand Up @@ -515,7 +523,16 @@ function processRequest(req, res, next) {
}

if (!options.headers['Content-Length']) {
options.headers['Content-Length'] = 0;
if (requestBody) {
options.headers['Content-Length'] = requestBody.length;
}
else {
options.headers['Content-Length'] = 0;
}
}

if (requestBody) {
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
}

if (config.debug) {
Expand All @@ -535,6 +552,7 @@ function processRequest(req, res, next) {
// API Call. response is the response from the API, res is the response we will send back to the user.
var apiCall = doRequest(options, function(response) {
response.setEncoding('utf-8');

if (config.debug) {
console.log('HEADERS: ' + JSON.stringify(response.headers));
console.log('STATUS CODE: ' + response.statusCode);
Expand Down Expand Up @@ -584,27 +602,33 @@ function processRequest(req, res, next) {
};
});

apiCall.end();
if (requestBody) {
apiCall.end(requestBody, 'utf-8');
}
else {
apiCall.end();
}
}
}


// Dynamic Helpers
// Passes variables to the view
app.dynamicHelpers({
session: function(req, res) {
// If api wasn't passed in as a parameter, check the path to see if it's there
if (!req.params.api) {
pathName = req.url.replace('/','');
// Is it a valid API - if there's a config file we can assume so
fs.stat('public/data/' + pathName + '.json', function (error, stats) {
if (stats) {
req.params.api = pathName;
}
});
}
// If the cookie says we're authed for this particular API, set the session to authed as well
if (!req.params.api) {
pathName = req.url.replace('/','');
// Is it a valid API - if there's a config file we can assume so
fs.stat('public/data/' + pathName + '.json', function (error, stats) {
if (stats) {
req.params.api = pathName;
}
});
}
// If the cookie says we're authed for this particular API, set the session to authed as well
if (req.params.api && req.session[req.params.api] && req.session[req.params.api]['authed']) {
req.session['authed'] = true;
req.session['authed'] = true;
}

return req.session;
Expand All @@ -629,6 +653,7 @@ app.dynamicHelpers({
}
})


//
// Routes
//
Expand Down
22 changes: 22 additions & 0 deletions public/stylesheets/style.css
Expand Up @@ -737,6 +737,28 @@ li {
zoom: 1;
}

dl {
display: block;
margin-bottom: 10px;
font-weight: normal;
}
dt {
width: 30%;
border-top: 1px rgba(0,0,0,.2) solid;
padding-top: 5px;
margin-top: 5px;
display: block;
float: left;
}
dd {
width: 70%;
border-top: 1px rgba(0,0,0,.2) solid;
padding-top: 5px;
margin-top: 5px;
display: block;
float: left;
}

@media print {
* {
background: transparent !important;
Expand Down
8 changes: 7 additions & 1 deletion views/api.jade
Expand Up @@ -120,7 +120,13 @@ ul
- else
input(name='params[' + parameter.Name + ']', value=parameter.Default, placeholder=className)
td.type=parameter.Type
td.description=parameter.Description || 'No description'
td.description
p=parameter.Description || 'No description'
- if (parameter.Type =='enumerated' && parameter.EnumeratedDescription)
dl.clearfix
- each description, choice in parameter.EnumeratedDescription
dt #{choice}
dd #{description}
- if (method.headers && method.headers.length > 0)
div.headers
h4.title
Expand Down

0 comments on commit 7ac39c7

Please sign in to comment.