在将所有需要的数据一起或单独发送之间设计API哪个更好?

时间:2015-01-20 15:59:24

标签: api

我正在尝试为移动应用程序设计API(想想Facebook Feed,我们正在尝试为这种类型的视图创建API),但我不知道每个人的利弊是什么。 API的类型以及您更喜欢哪种类型?

类型1:我们只需要单个API调用

网址:/product/list

[{
    "id": "1",
    "name": "Product1",
    "summary": "Lorem ipsum",
    "feature_image": "http://www.example.com/picture.jpg",
    "owner": {
        "id": "10",
        "name": "John Dev",
        "profile_image": "http://wwww.example.com/john.jpg",
        "user_level": "Expert"
    }
}, ... and 20 items here ...]

类型2:首先获取产品列表,然后提取所有者的数据

网址:/product/list

[{
    "id": "1",
    "name": "Product1",
    "summary": "Lorem ipsum",
    "feature_image": "http://www.example.com/picture.jpg",
    "owner_id": "10"
}, ... and 20 items here ...]

网址:/user/public_data?user_id=10

{
    "id": "10",
    "name": "John Dev",
    "profile_image": "http://wwww.example.com/john.jpg",
    "user_level": "Expert"
}

类型3:获取产品ID和所有者的列表,然后单独拉出

网址:/product/list

[{
    "product_id": "1",
    "owner_id": "10"
}, ... and 20 items here ...]

网址:/product/detail?product_id=1

{
    "name": "Product1",
    "summary": "Lorem ipsum",
    "feature_image": "http://www.example.com/picture.jpg",
}

网址:/user/public_data?user_id=10

{
    "id": "10",
    "name": "John Dev",
    "profile_image": "http://wwww.example.com/john.jpg",
    "user_level": "Expert"
}

1 个答案:

答案 0 :(得分:1)

我肯定会使用Type 1.进行多次api调用可能会变得昂贵。如果您可以一次性提供所有数据,这是最好的,除非你要回来的数据集太大,你想要在服务器端分页。要排序的20个对象只需要很少的时间。

相关问题