Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow optional POST or query string data.

  • Loading branch information...
commit a62184f60ed6844d3d449358d38f2d674a87ee77 1 parent ea1eb2b
Patrice Neff authored
8 generator/Process.py
View
@@ -132,7 +132,13 @@ def walk_REQUEST_POSTPARAMS(self, tree):
self.__currentVar['postparams'].append(tree.getChild(0).getText() + '*')
else:
for i in range(tree.getChildCount()):
- self.__currentVar['postparams'].append(tree.getChild(i).getText())
+ if tree.getChild(i).getText() == '?':
+ pass
+ else:
+ txt = tree.getChild(i).getText()
+ if i+1 < tree.getChildCount() and tree.getChild(i+1).getText() == '?':
+ txt += '?'
+ self.__currentVar['postparams'].append(txt)
def walk_REQUEST_NAME(self, tree):
self.__currentVar['name'] = tree.getChild(0).getText()
2  parser/servicegen.g
View
@@ -148,7 +148,7 @@ HTTP_METHOD
requestPath
: StringLiteral -> ^(REQUEST_PATH StringLiteral);
requestPostParams
- : '<-'! '('! IDENTIFIER (','! IDENTIFIER)* ')'!
+ : '<-'! '('! IDENTIFIER '?'? (','! IDENTIFIER '?'?)* ')'!
| '<-'! '('! IDENTIFIER '*' ')'!
;
requestName
11 templates/html/index.html.tmpl
View
@@ -108,8 +108,8 @@
# Copy documentation for POST params to separate dictionary
postparams_doc = copy.copy(req['docs']['params'])
for var in req['postparams']:
- if var in req['docs']['params']:
- del req['docs']['params'][var]
+ if var.replace('?', '') in req['docs']['params']:
+ del req['docs']['params'][var.replace('?', '')]
%>
${docTable(req['docs'])}
@@ -127,8 +127,11 @@
%for var in req['postparams']:
<dt>${var | h}</dt>
<dd>
- %if var in postparams_doc:
- ${postparams_doc[var] | h}
+ %if var[-1] == '?':
+ <em>Optional.</em>
+ %endif
+ %if var.replace('?', '') in postparams_doc:
+ ${postparams_doc[var.replace('?', '')] | h}
%endif
</dd>
%endfor
2  templates/python-webpy/_servicename_/_request_.py.tmpl
View
@@ -127,7 +127,7 @@ class ${req['name']}:
try:
%for var in method_req['postparams']:
- ${var.replace('*', '')} = servicegen.input.get('${var}')
+ ${var.replace('*', '').replace('?', '')} = servicegen.input.get('${var}')
%endfor
%for stm in method_req['statements']:
9 templates/python-webpy/servicegen/input.py
View
@@ -29,9 +29,14 @@ def get(key):
else:
# Get POST data. Can't use web.input() as it
# only works for POST method, not PUT
+ fetchkey = key
+ if fetchkey[-1] == '?': fetchkey = fetchkey[0:-1]
data = input()
- if key in data:
- return data[key]
+ if fetchkey in data:
+ return data[fetchkey]
+ elif key[-1] == '?':
+ # Optional param, return None-object
+ return None
else:
web.ctx.status = '400 Bad Request'
web.ctx.outputter.write_exception(('missing value', 'Missing value "' + key + '"'))
8 tests/parser/SimpleParserTest.py
View
@@ -442,6 +442,14 @@ def testPostDataWildcard(self):
self.assertEqual(postparams.getChild(0).getText(), 'indoc')
self.assertEqual(postparams.getChild(1).getText(), '*')
+ def testPostDataOptional(self):
+ """Tests the declaration of POST parameters with a question mark (optional)."""
+ request = self._parseService("post.txt").tree.getChild(3)
+ postparams = request.getChild(2)
+ self.assertEqual(postparams.getChildCount(), 2)
+ self.assertEqual(postparams.getChild(0).getText(), 'password')
+ self.assertEqual(postparams.getChild(1).getText(), '?')
+
def _parseService(self, service):
"""Parses the service from the given file."""
s = Service()
4 tests/services/post.txt
View
@@ -8,3 +8,7 @@ POST "/anotherpath" <- (key, value) {
POST "/anotherpath" <- (indoc*) {
}
+
+POST "/optional" <- (password?) {
+ # Password is optional in this example
+}
Please sign in to comment.
Something went wrong with that request. Please try again.