Search code examples
pythonhooktwisteddecorator

creating hooks in python twisted


i am using twisted to make a simple server . i have several resources (endpoints) to which i can make a http call. i need to add hooks like in falcon or flask to get query parameters for every call . how can i achieve that .

from twisted.web.resource import Resource, NoResource
import json
import redis
from twisted.internet import threads, defer
from twisted.web.server import NOT_DONE_YET
import time

class JobListener(Resource):

    isLeaf = True

    def render_POST(self, request):
        # i want query params here
        return NOT_DONE_YET


class Home(Resource):

    def getChild(self, name, request):
        uri =  request.uri
        print uri
        if uri == '/':
            return self
        if uri == '/api/v1/dump':
            return JobListener()
        else:
            return NoResource()

    def render_GET(self, request):
        return "<html><body>Welcome to the server!</body></html>"

whenever i hit /api/v1/dump it calls JobListener i want somehow to have add a hook to all endpoints that populates the query params when an api is called


Solution

  • Query arguments are available on the request object. See args on:

    http://twistedmatrix.com/documents/current/api/twisted.web.iweb.IRequest.html