[英]Where to put current user context data in response JSON?
考慮一個社交網絡。 它有職位。 對於提要,您請求/feed
並獲取帖子列表。
在UI中,有一些要顯示的內容,例如用戶是否喜歡該帖子,用戶是否對它加注星標等等。這些東西看起來並不像它們屬於post對象內部。
另一種情況是您獲取喜歡的商品。 前端需要知道是否關注每個“喜歡”對象中的用戶。
將此信息放在響應JSON中的哪里?
它取決於您的應用程序以及要顯示給用戶的數據。 例如,請考慮列出用戶的供稿。 在該供稿中,您想顯示
等等..
在上面的列表中,
有些數據需要兩次api提取才能獲取完整的信息,而另一些則不需要。 例如,“喜歡的用戶列表”,“共享用戶列表”。 這通常是一個動態數據模塊。 您必須在單獨的api中獲得這些詳細信息,以提高服務器的性能以及數據完整性。
在某些情況下,某些應用需要在列表頁面中偷窺喜歡的共享用戶信息。 在這種情況下,您可以在同一列表/feeds
響應本身中包含一些固定的少量用戶詳細信息,並在UI中包含“查看更多(如Facebook)”選項。
一些靜態的奇異數據(單列數據)可以在初始get /feeds
本身中列出。
我不知道您為什么不遵循相同的Twitter列表推文樣式,
https://dev.twitter.com/rest/reference/get/search/tweets
{
"coordinates": null,
"favorited": false,
"truncated": false,
"created_at": "Fri Sep 21 23:40:54 +0000 2012",
"id_str": "249292149810667520",
"entities": {
"urls": [
],
"hashtags": [
{
"text": "FreeBandNames",
"indices": [
20,
34
]
}
],
"user_mentions": [
]
},
"in_reply_to_user_id_str": null,
"contributors": null,
"text": "Thee Namaste Nerdz. #FreeBandNames",
"metadata": {
"iso_language_code": "pl",
"result_type": "recent"
},
"retweet_count": 0,
"in_reply_to_status_id_str": null,
"id": 249292149810667520,
"geo": null,
"retweeted": false,
"in_reply_to_user_id": null,
"place": null,
"user":
{
"profile_sidebar_fill_color": "DDFFCC",
"profile_sidebar_border_color": "BDDCAD",
"profile_background_tile": true,
"name": "Chaz Martenstein",
"profile_image_url": "http://a0.twimg.com/profile_images/447958234/Lichtenstein_normal.jpg",
"created_at": "Tue Apr 07 19:05:07 +0000 2009",
"location": "Durham, NC",
"follow_request_sent": null,
"profile_link_color": "0084B4",
"is_translator": false,
"id_str": "29516238",
"entities": {
"url": {
"urls": [
{
"expanded_url": null,
"url": "http://bullcityrecords.com/wnng/",
"indices": [
0,
32
]
}
]
},
"description": {
"urls": [
]
}
},
"default_profile": false,
"contributors_enabled": false,
"favourites_count": 8,
"url": "http://bullcityrecords.com/wnng/",
"profile_image_url_https": "https://si0.twimg.com/profile_images/447958234/Lichtenstein_normal.jpg",
"utc_offset": -18000,
"id": 29516238,
"profile_use_background_image": true,
"listed_count": 118,
"profile_text_color": "333333",
"lang": "en",
"followers_count": 2052,
"protected": false,
"notifications": null,
"profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/9423277/background_tile.bmp",
"profile_background_color": "9AE4E8",
"verified": false,
"geo_enabled": false,
"time_zone": "Eastern Time (US & Canada)",
"description": "You will come to Durham, North Carolina. I will sell you some records then, here in Durham, North Carolina. Fun will happen.",
"default_profile_image": false,
"profile_background_image_url": "http://a0.twimg.com/profile_background_images/9423277/background_tile.bmp",
"statuses_count": 7579,
"friends_count": 348,
"following": null,
"show_all_inline_media": true,
"screen_name": "bullcityrecords"
},
"in_reply_to_screen_name": null,
"source": "web",
"in_reply_to_status_id": null
}
您有兩種選擇:
制作一個單獨的API方法來獲取有關用戶上下文數據的信息- /api/users/1/feeds/1
請注意,此選項將強制您按每個提要發送請求。 因此,如果您有1000個供稿-您將有1000個+1個請求(即所謂的N + 1問題)。
對於我來說-這不是一個好主意。
您可以將用戶數據存儲在json中,例如,通過這種方式:
{ "feedName": "feed1", ... "currentUser": { "liked": true, "starred": true } }
通過使用此選項,您將避免RESTful服務中出現N + 1個請求問題
對於所有用戶,帖子資源應相同。 在其中添加特定的用戶上下文信息似乎對它造成污染
我可以看到您來自哪里,我也很同意。
Ivan的第一個解決方案不應該像他已經提到的那樣使用,他的第二個解決方案更好,但是如果您獲取應該僅包含發布對象的發布JSON,則還有一個currentUser並不真正屬於該發布對象。
我的建議是,對於每個帖子,您都要跟蹤哪些用戶喜歡和/或為其加注了星標,等等。然后,您可以保持結構簡潔,同時在相同的請求/響應中仍可以獲取所需的信息。
例
GET /feed HTTP/1.1
[
{
"text": "hello world, im a post!",
"author": "Jack",
"likes": 3,
"likedBy": [
"John",
"James",
"Jessica"
],
"stars": 2,
"starredBy": [
"John",
"Mary"
]
},
{
"text": "hello world, im also a post! :D",
"author": "Mary",
"likes": 1,
"likedBy": [
"James"
],
"stars": 0,
"starredBy": [
]
},
]
其中每個{}
對象代表一個發布對象。
然后,在客戶端上,您可以檢查likedBy
列表是否包含當前登錄的用戶,並根據需要繼續處理結果。 對於星標以及帖子可能具有的任何其他這些屬性都相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.