@handler.authenticated(allow_prospective=True)
  @handler.asynchronous(datastore=True, obj_store=True)
  def post(self, method_name):
    """Parses the JSON request body, validates it, and invokes the
    method as specified in the request URI. On completion, the
    response is returned as a JSON-encoded HTTP response body.
    """
    def _OnSuccess(method, start_time, user, device_id, response_dict):
      self.set_status(200)
      self.set_header('Content-Type', 'application/json; charset=UTF-8')
      # self.write() accepts a dict directly, but tornado's json_encode
      # does bytes-to-unicode conversions that are unnecessary on py2
      # and surprisingly expensive.
      self.write(json.dumps(response_dict))

      request_time = time.time() - start_time
      _avg_req_time.add(request_time)
      logging.debug('serviced %s request in %.4fs: %s' %
                    (method_name, request_time, response_dict))
      self.finish()

    # Set api_name for use in any error response.
    self.api_name = method_name
    _req_per_min.increment()
    start_time = time.time()

    # Check service method; (501: Not Implemented).
    if not ServiceHandler.SERVICE_MAP.has_key(method_name):
      _fail_per_min.increment()
      self.send_error(status_code=501)
      return

    json_request = self._LoadJSONRequest()
    if json_request is None:
      _fail_per_min.increment()
      return

    # Get current user and device id (both of which were originally derived from the user cookie).
    user = base.ViewfinderContext.current().user
    device_id = base.ViewfinderContext.current().device_id

    method = ServiceHandler.SERVICE_MAP[method_name]
    ServiceHandler.InvokeService(self._client, self._obj_store, method_name, user.user_id, device_id,
                                 json_request, callback=partial(_OnSuccess, method, start_time, user, device_id))
dataset/ETHPy150Open viewfinderco/viewfinder/backend/www/service.py/ServiceHandler.post/original