def wrapper(cls, *args, **kwds): """ Wrap http_api function. """ ctx = build_context( testing=current_app.config["TESTING"], request_id=str(uuid.uuid4( )), # Generates an unique ID on this request so we can track it. ) return f(cls, ctx, *args, **kwds)
def wrapper(cls, ctx, *args, **kwds): """ Wrap http_api function. """ # TODO: Remove dep from SQLAlchemy? if ctx.get(CTX_SQL_SESSION): return f(cls, ctx, *args, **kwds) s = Db.get_db().get_session() ctx = build_context(session=s, ctx=ctx) try: result = f(cls, ctx, *args, **kwds) # It makes things clearer and less error-prone. assert isinstance( result, tuple), "Please always pass the result AND the HTTP code." assert len( result) > 1, "Please always pass the result AND the HTTP code." status_code = result[1] msg = result[0] if result[0] == NoContent: msg = None if status_code and 200 <= status_code <= 299: s.commit() else: LOG.info("rollback_sql_transaction_non_200_http_code", extra=log_extra(ctx, code=status_code, message=msg)) s.rollback() return result except Exception as e: LOG.error("rollback_sql_transaction_exception_caught", extra=log_extra(ctx, exception=str(e), traceback=traceback.format_exc())) s.rollback() raise finally: # When running unit tests, we don't close the session so tests can actually perform some work on that # session. if not ctx.get(CTX_TESTING): s.close()
def wrapper(cls, ctx, *args, **kwargs): admin = _find_admin(ctx.get(CTX_SQL_SESSION), TESTING_CLIENT) ctx = build_context(ctx=ctx, admin=Admin(login=admin.login)) return f(cls, ctx, *args, **kwargs)
def ctx(): return build_context( admin=Admin(login='******'), testing=True, )