Created
February 26, 2013 19:16
-
-
Save dreid/5041206 to your computer and use it in GitHub Desktop.
klein implicit branches and werkzeug route ordering.
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
(klein)thomasina dreid:klein (master=)> python itemstore1.py | |
Map([<Rule '/items/' -> items>, | |
<Rule '/items/<name>/<__rest__>' (PUT) -> save_item_branch>, | |
<Rule '/items/<name>/<__rest__>' (HEAD, GET) -> get_item_branch>, | |
<Rule '/items/<name>/' (PUT) -> save_item>, | |
<Rule '/items/<name>/' (HEAD, GET) -> get_item>, | |
<Rule '/items/<__rest__>' -> items_branch>]) | |
2013-02-26 11:14:26-0800 [-] Log opened. | |
2013-02-26 11:14:26-0800 [-] Site starting on 8080 | |
2013-02-26 11:14:26-0800 [-] Starting factory <twisted.web.server.Site instance at 0x10ab59488> | |
^C2013-02-26 11:14:43-0800 [-] Received SIGINT, shutting down. | |
2013-02-26 11:14:43-0800 [twisted.web.server.Site] (TCP Port 8080 Closed) | |
2013-02-26 11:14:43-0800 [twisted.web.server.Site] Stopping factory <twisted.web.server.Site instance at 0x10ab59488> | |
2013-02-26 11:14:43-0800 [-] Main loop terminated. |
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
import json | |
from klein import Klein | |
app = Klein() | |
_items = {'foo': 'bar'} | |
@app.route('/items/<string:name>/', methods=['PUT']) | |
def save_item(request, name): | |
request.setHeader('Content-Type', 'application/json') | |
body = json.loads(request.content.read()) | |
_items[name] = body | |
return json.dumps({'success': True}) | |
@app.route('/items/<string:name>/', methods=['GET']) | |
def get_item(request, name): | |
request.setHeader('Content-Type', 'application/json') | |
return json.dumps(_items.get(name)) | |
@app.route('/items/') | |
def items(request): | |
request.setHeader('Content-Type', 'application/json') | |
return json.dumps(_items) | |
if __name__ == '__main__': | |
print app.url_map | |
app.run('localhost', 8080) |
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
(klein)thomasina dreid:klein (master=)> python itemstore2.py | |
Map([<Rule '/items/' -> items>, | |
<Rule '/items/<name>/<__rest__>' (PUT) -> save_item_branch>, | |
<Rule '/items/<name>/<__rest__>' (HEAD, GET) -> get_item_branch>, | |
<Rule '/items/<name>/' (PUT) -> save_item>, | |
<Rule '/items/<name>/' (HEAD, GET) -> get_item>, | |
<Rule '/items/<__rest__>' -> items_branch>]) | |
2013-02-26 11:15:42-0800 [-] Log opened. | |
2013-02-26 11:15:42-0800 [-] Site starting on 8080 | |
2013-02-26 11:15:42-0800 [-] Starting factory <twisted.web.server.Site instance at 0x1040993b0> | |
^C2013-02-26 11:15:43-0800 [-] Received SIGINT, shutting down. | |
2013-02-26 11:15:43-0800 [twisted.web.server.Site] (TCP Port 8080 Closed) | |
2013-02-26 11:15:43-0800 [twisted.web.server.Site] Stopping factory <twisted.web.server.Site instance at 0x1040993b0> | |
2013-02-26 11:15:43-0800 [-] Main loop terminated. |
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
import json | |
from klein import Klein | |
app = Klein() | |
_items = {'foo': 'bar'} | |
@app.route('/items/') | |
def items(request): | |
request.setHeader('Content-Type', 'application/json') | |
return json.dumps(_items) | |
@app.route('/items/<string:name>/', methods=['PUT']) | |
def save_item(request, name): | |
request.setHeader('Content-Type', 'application/json') | |
body = json.loads(request.content.read()) | |
_items[name] = body | |
return json.dumps({'success': True}) | |
@app.route('/items/<string:name>/', methods=['GET']) | |
def get_item(request, name): | |
request.setHeader('Content-Type', 'application/json') | |
return json.dumps(_items.get(name)) | |
if __name__ == '__main__': | |
print app.url_map | |
app.run('localhost', 8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment