如何在REST API中管理3种资源之间的关系 [英] How to manage relationship between 3 resources in REST API

查看:50
本文介绍了如何在REST API中管理3种资源之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于REST概念创建API,但在谈论相关资源时仍然有些困惑.

I'm creating an API based on REST concept but I'm still a bit confused talking about relating resources.

我有一个网站,人们可以在其中注册多个组并选择多个角色.例如,让我们以在公司中注册的人员作为场景:

I've a website where people can signup in multiple groups and choose multiple roles. For example let's take people that signup in companies as scenario:

公司

  1. Facebook
  2. Google
  3. 苹果

角色

  • 营销
  • 销售
  • 发展
  • 客户支持

因此,当我想在具有特定角色的新公司中创建用户时,我会将类似的内容传递到/users端点的POST请求中

So, when I want to create a user in a new company with certain roles, I would pass something like this into a POST request to /users endpoint

{
       "username" : "raffaele.izzia",
        "email"   : "example@email.com",
        "groups"  : [{
          "id" : 1,
          "roles" : ["Sales","Customer support"]
        },
        {
          "id" : 2,
          "roles" : ["Sales","Marketing"]
        }]
}

使用这种方法,一旦我从API中获得了一些用户,我就始终知道他们属于哪个组/角色.

With this approach, once I get some users from the API I always know in which groups/roles they are.

但是/groups端点上的请求呢?

But what about requests on /groups endpoint?

如果我获得/groups/google的信息,我也应该收到有关用户/角色的信息.所以可能是这样的

If I GET /groups/google I should receive info about users/roles too. So it could be something like this

{ 
    groups: [{
        "id" : 2,
        "name"   : "Google",
        "users"  : [2,3,4,10,35,50] //role will be included in the single resource once you expand this collection
    }]
}

或者也许是这样:

{ 
    groups: [{
        "id" : 2,
        "name"   : "Google",
        "roles"  : [{
           "name"  : "Sales"
           "users" : [2,3,4,10]
        },{
           "name"  : "Marketing"
           "users" : [4,10,8,57]
        }]
    }]
}

您认为这种关系的最佳解决方案是什么?

What do you think is the best solution for this kind of relationships?

推荐答案

这是一个很好的问题.绝对是一个棘手的情况.

This is a good question. Definitely a tricky situation.

我认为典型的RESTful答案是根据名词,AKA资源进行思考.即使您将此三向连接视为名词之间的关系,该连接本身也是一个名词. 这就是您希望您的REST API公开的内容.

I think the typical RESTful answer would be to think in terms of nouns, AKA resources. Even though you view this 3-way connection as a relationship between nouns, the connection itself is a noun. That's what you want your REST API to expose.

由于缺乏更好的术语,您的连接名称可能是分配(或分配,或其他名称).例如. 在市场营销部门为Google工作的爱丽丝"是一项任务.

For lack of a better term, perhaps the name for your connection is an assignment (or an allocation, or whatever). E.g. "Alice working for Google in Marketing" is an assignment.

这现在打开了新的可能性,应该可以满足您的需求.

This now opens up new possibilities that should get you what you need.

例如分配对象:

{
    "id": "...",
    "user": {...},  // e.g. Alice
    "group": {...}, // e.g. Google
    "role": "Marketing"
}

获取/users/alice/assignments会返回她所有作业的列表.

And fetching /users/alice/assignments returns a list of all of her assignments.

类似地,获取/groups/google/assignments会为在Google工作的人返回所有分配对象的列表.

Similarly, fetching /groups/google/assignments returns a list of all the assignment objects for people working at Google.

这样做的好处是现在您的作业确实是一流的.现在,您可以做一些事情,例如跟踪旧的 分配,同时让您的主要API仅返回最新的.等等(此处提示此答案以获取灵感).

The advantage to this is that now your assignments are truly first-class. You can now do things like keep track of old assignments, while having your main APIs return only current ones. Etc. (Props to this answer for inspiration here.)

希望这会有所帮助!

这篇关于如何在REST API中管理3种资源之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆