john990
V2EX  ›  问与答

flask-restful 写接口,每个接口都有相同的处理逻辑,怎么写比较好?

  •  
  •   john990 · Dec 10, 2014 · 3325 views
    This topic created in 4364 days ago, the information mentioned may be changed or developed.
    每次请求都要验证下app的签名等信息,如果不合法就禁止访问,这个怎么写?
    现在是没个函数里都加判断....
    6 replies  •  2014-12-10 18:09:51 +08:00
    virusdefender
    virusdefender
    Dec 10, 2014❤️1
    1
    middleware
    virusdefender
    virusdefender
    Dec 10, 2014❤️1
    2
    或者每个函数之前都加一个修饰器 比如@permission_required(["edit", "add"])
    xujialiang
    xujialiang
    Dec 10, 2014❤️1
    3
    flask 有 before_request() 这种回调吧
    clino
    clino
    Dec 10, 2014 via Android❤️1
    4
    装饰器
    crazyxin1988
    crazyxin1988
    Dec 10, 2014❤️1
    5
    装饰器哇
    john990
    john990
    OP
    Dec 10, 2014
    6
    谢谢~ 搞定
    @clino
    @crazyxin1988
    用装饰器解决,要去看下python基础...

    @xujialiang
    before_request() 好像不能打断接下来的操作,我想检测不通过就返回一个错误码
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1836 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:05 · PVG 19:05 · LAX 03:05 · JFK 06:05
    ♥ Do have faith in what you're doing.