V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2018-12-10 23:33:44 +08:00 · 2319 次点击
    这是一个创建于 1956 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

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

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

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

    单个用户信息:
    ```
    GET /users/:id
    ```
    hwding
        9
    hwding  
       2018-12-11 10:33:39 +08:00
    应该统一 /user/{uId}/info,是不是自己由后面取 token 做判断,貌似好多站都是这样的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5497 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 03:16 · PVG 11:16 · LAX 20:16 · JFK 23:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.