Пример #1
0
    def dispatch(self, *args, **kwargs):
        """Do setup for cron handlers.

        - Jsons output
        - logs exception traces
        - Initializes Api-like Cron object
        """
        # There's no true sense of a current user making a request in cron
        # jobs, so invent an admin to run them. Don't get whether they've ever
        # been created before in unique index of users, because crons run all
        # the time. The user won't be saved to the datastore anyway.
        admin_user = User.create(check_uniqueness=False,
                                 email='',
                                 auth_id='',
                                 first_name='Cron',
                                 last_name='Job',
                                 is_admin=True)

        # The testing flag allows us to use unsaved user entities to create an
        # api. Normal operation requires that the user be saved to the
        # datastore. This is the only effect; e.g. a testing api still affects
        # the datastore.
        self.api = Api(admin_user, testing=True)

        self.cron = Cron(self.api)

        self.response.headers['Content-Type'] = (
            'application/json; charset=utf-8')

        try:
            # Call the descendant handler.
            BaseHandler.dispatch(self)
            # self.write_json(self.do(*args, **kwargs))
        except Exception as error:
            trace = traceback.format_exc()
            logging.error("{}\n{}".format(error, trace))
            response = {
                'error': True,
                'message': '{}: {}'.format(error.__class__.__name__, error),
                'trace': trace,
            }
            self.response.write(json.dumps(response))

        else:
            # If everything about the request worked out, but no data was
            # returned, put out a standard empty response.
            if not self.response.body:
                self.write(None)
Пример #2
0
    def dispatch(self):
        try:
            logging.info("ViewHandler.dispatch()")
            # Call the overridden dispatch(), which has the effect of running
            # the get() or post() etc. of the inheriting class.
            BaseHandler.dispatch(self)

        except Exception as error:
            trace = traceback.format_exc()
            # We don't want to tell the public about our exception messages.
            # Just provide the exception type to the client, but log the full
            # details on the server.
            logging.error("{}\n{}".format(error, trace))
            response = {
                'success': False,
                'message': error.__class__.__name__,
            }
            if debug:
                self.response.write('<pre>{}</pre>'.format(
                    traceback.format_exc()))
            else:
                self.response.write("We are having technical difficulties.")
            return
Пример #3
0
    def dispatch(self, *args, **kwargs):
        """Wrap all api calls in a try/catch so the server never breaks when
        the client hits an api URL."""

        self.response.headers['Content-Type'] = (
            'application/json; charset=utf-8')

        # https://stackoverflow.com/questions/45643161/how-to-return-status-code-418-in-webapp2
        webob_util.status_reasons[429] = "Too Many Requests"
        httplib.responses[429] = "Too Many Requests"

        try:
            # Call the descendant handler.
            # super(ApiHandler, self).dispatch()
            BaseHandler.dispatch(self)

        except Exception as error:
            trace = traceback.format_exc()
            # We don't want to tell the public about our exception messages.
            # Just provide the exception type to the client, but log the full
            # details on the server.
            logging.error("{}\n{}".format(error, trace))
            response = {
                'error': True,
                'message': error.__class__.__name__,
            }
            if debug:
                response['message'] = "{}: {}".format(error.__class__.__name__,
                                                      error)
                response['trace'] = trace
            self.response.write(json.dumps(response))

        else:
            # If everything about the request worked out, but no data was
            # returned, put out a standard empty response.
            if not self.response.body:
                self.write(None)