예제 #1
0
    def handle_post(self, api_endpoint, api_version):
        """Takes care of validation of input and execution of POST methods."""
        REQUEST_COUNTS.labels('post', type(api_endpoint).__name__).inc()
        code = 400
        data = self.get_post_data()
        if data:
            try:
                res = api_endpoint.process_list(api_version, data)
                code = 200
            except ValidationError as validerr:
                if validerr.absolute_path:
                    res = '%s : %s' % (validerr.absolute_path.pop(),
                                       validerr.message)
                else:
                    res = '%s' % validerr.message
                LOGGER.error('ValidationError: %s', res)
            except ValueError as valuerr:
                res = str(valuerr)
                LOGGER.error('ValueError: %s', res)
            except sre_constants.error as sre_err:
                res = 'Regular expression error: ' + str(sre_err)
                LOGGER.error('sre_constants.error: %s', res)
            except Exception as err:  # pylint: disable=broad-except
                err_id = err.__hash__()
                res = 'Internal server error <%s>: please include this error id in bug report.' % err_id
                code = 500
                LOGGER.exception(res)
                LOGGER.info("Input data for <%s>: %s", err_id, data)
        else:
            res = 'Error: malformed input JSON.'
            LOGGER.error(res)

        self.set_status(code)
        self.write(res)
예제 #2
0
    async def timing_middleware(request, handler, **kwargs):
        """ Middleware that handles timing of requests"""
        start_time = time.time()
        if asyncio.iscoroutinefunction(handler):
            res = await handler(request, **kwargs)
        else:
            res = handler(request, **kwargs)

        duration = (time.time() - start_time)
        # (0)  /(1) /(2) /(3)
        #     /api /v1  /updates
        const_path = '/'.join(request.path.split('/')[:4])
        REQUEST_TIME.labels(request.method, const_path).observe(duration)
        REQUEST_COUNTS.labels(request.method, const_path, res.status).inc()

        return res
예제 #3
0
 def handle_get(self, api_endpoint, api_version, param_name, param):
     """Takes care of validation of input and execution of GET methods."""
     REQUEST_COUNTS.labels('get', type(api_endpoint).__name__).inc()
     code = 400
     try:
         result = api_endpoint.process_list(api_version,
                                            {param_name: [param]})
         code = 200
     except ValueError as valuerr:
         result = str(valuerr)
         LOGGER.error('ValueError: %s', result)
     except sre_constants.error as sre_err:
         result = 'Regular expression error: ' + str(sre_err)
         LOGGER.warning('sre_constants.error: %s', result)
     except Exception as err:  # pylint: disable=broad-except
         err_id = err.__hash__()
         result = 'Internal server error <%s>: please include this error id in bug report.' % err_id
         code = 500
         LOGGER.exception(result)
     self.set_status(code)
     self.write(result)