Guest User

Untitled

a guest
Feb 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. from klein.app import KleinRequest, Klein
  2. from klein.interfaces import IKleinRequest
  3. from twisted.python.components import registerAdapter
  4. from twisted.web.server import Request, Site
  5.  
  6.  
  7. class CustomRequest(Request):
  8. def get_something_else(self):
  9. return 'I am something else'
  10.  
  11.  
  12. class CustomApp(object):
  13. app = Klein()
  14.  
  15. def __init__(self):
  16. """
  17. I am not sure this is the best way
  18. But I couldn't find any other way without code duplication
  19. """
  20. Site.requestFactory = CustomRequest
  21. registerAdapter(KleinRequest, CustomRequest, IKleinRequest)
  22.  
  23. @app.route('/something_else', methods=['GET'])
  24. def something_else(self, request):
  25. """
  26. :param CustomRequest request:
  27. """
  28. return request.get_something_else()
  29.  
  30.  
  31. if __name__ == '__main__':
  32. my_app = CustomApp()
  33. my_app.app.run(host='localhost', port=8000)
Add Comment
Please, Sign In to add comment