API 将所有需要的数据一起发送或单独发送哪个更适合设计?
Which is better for designing API between send all needed data together or separately?
我正在尝试为移动应用程序设计 API(想想 Facebook 提要,我们正在尝试为这种类型的视图创建 API),但我想知道有什么优缺点对于每种类型 API,您更喜欢哪种类型?
类型 1:我们只需要一个 API 电话
Url: /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 ...]
方式二:先获取商品列表,后拉取店主数据
Url: /product/list
[{
"id": "1",
"name": "Product1",
"summary": "Lorem ipsum",
"feature_image": "http://www.example.com/picture.jpg",
"owner_id": "10"
}, ... and 20 items here ...]
Url: /user/public_data?user_id=10
{
"id": "10",
"name": "John Dev",
"profile_image": "http://wwww.example.com/john.jpg",
"user_level": "Expert"
}
方式三:获取product id和owner列表,然后分别拉取
Url: /product/list
[{
"product_id": "1",
"owner_id": "10"
}, ... and 20 items here ...]
Url: /product/detail?product_id=1
{
"name": "Product1",
"summary": "Lorem ipsum",
"feature_image": "http://www.example.com/picture.jpg",
}
Url: /user/public_data?user_id=10
{
"id": "10",
"name": "John Dev",
"profile_image": "http://wwww.example.com/john.jpg",
"user_level": "Expert"
}
我肯定会选择类型 1。进行多次 api 调用可能会很昂贵。如果您可以一次提供所有数据,那是更可取的,除非您将返回的数据集太大以至于您想在服务器端对其进行分页。整理 20 个对象只需要很少的时间。
我正在尝试为移动应用程序设计 API(想想 Facebook 提要,我们正在尝试为这种类型的视图创建 API),但我想知道有什么优缺点对于每种类型 API,您更喜欢哪种类型?
类型 1:我们只需要一个 API 电话
Url: /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 ...]
方式二:先获取商品列表,后拉取店主数据
Url: /product/list
[{
"id": "1",
"name": "Product1",
"summary": "Lorem ipsum",
"feature_image": "http://www.example.com/picture.jpg",
"owner_id": "10"
}, ... and 20 items here ...]
Url: /user/public_data?user_id=10
{
"id": "10",
"name": "John Dev",
"profile_image": "http://wwww.example.com/john.jpg",
"user_level": "Expert"
}
方式三:获取product id和owner列表,然后分别拉取
Url: /product/list
[{
"product_id": "1",
"owner_id": "10"
}, ... and 20 items here ...]
Url: /product/detail?product_id=1
{
"name": "Product1",
"summary": "Lorem ipsum",
"feature_image": "http://www.example.com/picture.jpg",
}
Url: /user/public_data?user_id=10
{
"id": "10",
"name": "John Dev",
"profile_image": "http://wwww.example.com/john.jpg",
"user_level": "Expert"
}
我肯定会选择类型 1。进行多次 api 调用可能会很昂贵。如果您可以一次提供所有数据,那是更可取的,除非您将返回的数据集太大以至于您想在服务器端对其进行分页。整理 20 个对象只需要很少的时间。