Last active
May 11, 2017 21:44
-
-
Save uda/b63b841ea7851da8b2084b830437fbc7 to your computer and use it in GitHub Desktop.
Klein controller concept
This file contains 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
"""A basic controller concept for Twisted based micro web framework Klein""" |
This file contains 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
from klein import Klein | |
import user | |
app = Klein() | |
user.UserController(app, namespace='user') | |
app.run('localhost', 8080) |
This file contains 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
class Controller(object): | |
_routes = {} | |
def __init__(self, app, namespace=None): | |
""" | |
:param klein.Klein app: | |
:param str or None namespace: | |
""" | |
self.namespace = namespace or self.__class__.__name__ | |
self.app = app | |
with app.subroute('/' + self.namespace) as app: | |
for url, route_kwargs in getattr(self, '_routes', {}).items(): | |
action = route_kwargs.pop('action') | |
if not action: | |
continue | |
func = getattr(self, action) | |
endpoint = ':'.join([self.namespace, action]) | |
route_kwargs['endpoint'] = endpoint | |
new_func = app.route(url, **route_kwargs)(func) | |
setattr(self, action, new_func) |
This file contains 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 bson import json_util | |
from twisted.internet import defer | |
from controller import Controller | |
class UserController(Controller): | |
_routes = { | |
'/': { | |
'action': 'index', | |
'methods': ['GET'], | |
}, | |
'/create': { | |
'action': 'create', | |
'methods': ['POST', 'PUT'], | |
}, | |
'/get/<int:id>': { | |
'action': 'get_by_id', | |
'methods': ['GET'], | |
}, | |
'/get/<string:username>': { | |
'action': 'get_by_username', | |
'methods': ['GET'], | |
}, | |
} | |
def index(self, request): | |
"""List all users | |
:param twisted.web.http.Request request: | |
:rtype: str | |
""" | |
return 'User list' | |
def create(self, request): | |
"""Create a user | |
Notice: do not accept user input as is, I would use jsonschema here, but that is just me... | |
:param twisted.web.http.Request request: | |
""" | |
username = request.args.get('username') | |
return 'Creating user with name {}'.format(username) | |
def get_by_id(self, request, id): | |
"""Get user by its ID | |
:param twisted.web.http.Request request: | |
:param int id: | |
:rtype: twisted.internet.defer.Deferred | |
""" | |
d = self.app.mongo.users.find({'id': id}) | |
d.addCallback(self._get_user, request) | |
return d | |
def get_by_username(self, request, username): | |
"""Get user by its ID | |
:param twisted.web.http.Request request: | |
:param str username: | |
:rtype: twisted.internet.defer.Deferred | |
""" | |
d = self.app.mongo.users.find({'username': username}) | |
d.addCallback(self._get_user, request) | |
return d | |
def _get_user(self, user, request): | |
if not user: | |
return defer.fail() | |
request.setHeader(b'content-type', b'application/json') | |
return defer.succeed(json.dumps(user, default=json_util.default)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment