Skip to content

AsyncRequestHandler Class

Innocent Bystander edited this page Aug 17, 2015 · 6 revisions

This is the method documentation for AsyncRequestHandler class, which is the base class used by sinks in bot version 2.7 and above. This class heavily resembles the original BaseBotRequestHandler class, as it was designed as a drop-in replacement.

Some familiarity with aiohttp.web is recommended

Methods

addroutes

def addroutes(self, router):
    router.add_route("POST", "/{convid}", self.adapter_do_POST)
    router.add_route("POST", "/{convid}/", self.adapter_do_POST)
  • receives incoming POST request matching the specified pattern and routes it to self.adapter_do_POST
  • override this class to provide more incoming request routes, such as supporting GET request, or special paths
  • no equivalent method in BaseBotRequestHandler

adapter_do_POST

@asyncio.coroutine
def adapter_do_POST()
  • if overridden, decorate with @asyncio.coroutine
  • processes incoming POST request
  • pseudo-code:
    • breaks request into:
      • path - the URL component following the domain name
      • query_string - the URL component following the path
      • content - string payload in the body of the request
    • calls AsyncRequestHandler.process_request
      • if the function returns a non-empty string sequence, it will be output to the client as html, otherwise text OK will be sent back as the response
  • emulates the original behaviour of BaseBotRequestHandler.do_POST() with the added capability of being able to send back a different response

process_request

@asyncio.coroutine
def process_request( path, 
                     query_string, 
                     content )
  • if overridden, decorate with @asyncio.coroutine
  • pseudo-code:
    • receives path, query_string and content
      • path is split by "/" and the first element of the resulting list is assumed to be a target conversation_id (to send a message)
      • query_string is ignored
      • content is assumed to be valid JSON and parsed into dict payload:
        • payload["echo"] will be the html-formatted message to send
        • payload["image"] can consist of two subkeys:
          • payload["image"]["base64encoded"] is a base64-encoded string of an image to send, it is decoded into a io.BytesIO object image_data
          • payload["image"]["filename"] is the filename of the image and assigned to image_filename
            • if no filename is provided, will attempt to determine image type and assign the correct extension with a name based on the current date and time
    • calls send_data(conversation_id, html, image_data=None, image_filename=None)
    • return any results from send_data()
  • emulates the original behaviour of BaseBotRequestHandler.process_request(), with added return

send_data

@asyncio.coroutine
def send_data( conversation_id, 
               html, 
               image_data=None, 
               image_filename=None,
               context=None )
  • sends a message containing html-formatted text and/or an image to a conversation
  • if overridden, decorate with @asyncio.coroutine
  • pseudo-code:
    • if image_data is provided, the image will be automatically uploaded for use by the bot
      • image_filename is optional but recommended - since process_request determines the type of image uploaded. if the method was overridden and no image_filename provided, it will fallback to <datetime>.jpg and output a warning
    • html is parsed by the available parser in the bot - as of 2.4 the parser supports simplified Markdown and HTML
    • sends the message to the appropriate conversation
    • returns "OK"
  • emulates the original behaviour of BaseBotRequestHandler.send_data(), with extra return

# ## ###

Clone this wiki locally