推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
mocxe2vwww
V2EX  ›  Python

restful api 该怎么取舍?

  •  
  •   mocxe2vwww · Dec 10, 2018 · 3223 views
    This topic created in 2847 days ago, the information mentioned may be changed or developed.

    首先 client 认证获取到 token 带着 token 访问接口

    现在有这样的一个接口,查看用户的基本信息

    /user/info 默认不传参是查看自己的, 自己的 uid 是加密到 token 里面的,他看不到自己的 uid.

    /user/info?uid=xxx 要查看其他用户的信息,就必须传 uid 查询参数了。

    如果为了清晰统一 使用 /user/info/xxx 这样层级方式, 那必须要把当前用户自己的 uid 返回去了,这样做合适吗?

    srx1982
        1
    srx1982  
       Dec 10, 2018
    restful 应该是这样吧:/user/xxx/info
    /user/info 就是自己的
    /user/xxx/info 就是别人的
    hbpeng
        2
    hbpeng  
       Dec 10, 2018 via iPhone
    查看自己 /myself 查看他人 /users/id/info
    mocxe2vwww
        3
    mocxe2vwww  
    OP
       Dec 10, 2018
    @srx1982 这么做,得写 2 个 view function 了吧。
    mocxe2vwww
        4
    mocxe2vwww  
    OP
       Dec 10, 2018
    @hbpeng 用户信息的页面是共用的,我想的是能最好用一个接口方便了,前端不用做判断是看自己还是看别人
    srx1982
        5
    srx1982  
       Dec 10, 2018
    @mocxe2vwww 是的,这么写是两个入口
    我觉得如果返回的内容一样,就用一样的形式
    srx1982
        6
    srx1982  
       Dec 10, 2018
    @mocxe2vwww 都用 /user/id/info 就好
    jingyulong
        7
    jingyulong  
       Dec 11, 2018 via iPhone
    一致性原则,要么都隐藏在 token 里,要么都显示出来。看不到自己的 UID,别人的 UID 就能看到,这个逻辑要来干什么大事吗。。。
    cstome
        8
    cstome  
       Dec 11, 2018
    我是这样设计的:

    自己的信息:
    ```
    GET /profile
    ```

    单个用户信息:
    ```
    GET /users/:id
    ```
    hwding
        9
    hwding  
       Dec 11, 2018
    应该统一 /user/{uId}/info,是不是自己由后面取 token 做判断,貌似好多站都是这样的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2546 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 11:01 · PVG 19:01 · LAX 04:01 · JFK 07:01
    ♥ Do have faith in what you're doing.