title: flask-restful - resource class for current request
body: <p><strong>THE QUESTION</strong></p>
  
  <p>All of my app's routes are defined via flask-restful Resources.
  How can I find the resource object/class that is processing current request?</p>
  
  <p><strong>WHY I WANT THIS</strong></p>
  
  <p>I wanted to log all exceptions raised while processing requests. I connect to <code>flask.got_request_exception</code>, as described in <a href="http://flask.pocoo.org/docs/1.0/api/#signals" rel="noreferrer">http://flask.pocoo.org/docs/1.0/api/#signals</a> and something like this works well:</p>
  
  <pre><code>from flask import got_request_exception, request
  
  def log_exception(sender, exception, **extra):
      logger.info("URL: {}, Exception: {}".format(request.url, type(exception).__name__))
  
  got_request_exception.connect(log_exception, app)
  </code></pre>
  
  <p>The only problem is that I want to log some of the request data, but not all the data - e.g. I'd like to hide passwords. I think it would be a good idea to have logging-data-logic together with request processing logic, like this:</p>
  
  <pre><code>from flask import request
  import flask_restful
  
  class SomeResource(flask_restful.Resource):
      def get(self):
          # ... GET processing
      def log_data(self):
          # log all body params
          return request.get_json()
  
  class Login(flask_restful.Resource):
     def post(self):
         # ... authentication
     def log_data(self):
         # log selected body params
         return {'login': request.get_json()['login'], 'password': 'HIDDEN!'}
  </code></pre>
  
  <p>and than use it in my <code>log_exception</code>:</p>
  
  <pre><code>from flask import got_request_exception, request
  
  def log_exception(sender, exception, **extra):
      resource_class = # THIS IS THE THING I'M MISSING
      logger.info("URL: {}, Exception: {}, Data: {}".format(request.url, type(exception).__name__), 
                  resource_class.log_data())
  
  got_request_exception.connect(log_exception, app) 
  </code></pre>
  
  <p>But maybe this should be done other way?</p>