john990
V2EX  ›  问与答

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

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

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