From dad54db7f4ea1c8fcf680629b2006bdf9274aad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonne=20Ha=C3=9F?= Date: Sun, 28 Apr 2019 15:44:30 +0200 Subject: [PATCH] Return API pagination links as headers rather than in the response body itself --- app/controllers/api/v1/aspects_controller.rb | 2 +- app/controllers/api/v1/base_controller.rb | 9 +++++++++ app/controllers/api/v1/comments_controller.rb | 2 +- app/controllers/api/v1/contacts_controller.rb | 2 +- .../api/v1/conversations_controller.rb | 2 +- app/controllers/api/v1/likes_controller.rb | 2 +- app/controllers/api/v1/messages_controller.rb | 2 +- .../api/v1/notifications_controller.rb | 2 +- app/controllers/api/v1/photos_controller.rb | 2 +- app/controllers/api/v1/reshares_controller.rb | 2 +- app/controllers/api/v1/search_controller.rb | 4 ++-- app/controllers/api/v1/streams_controller.rb | 2 +- .../api/v1/tag_followings_controller.rb | 2 +- app/controllers/api/v1/users_controller.rb | 6 +++--- .../integration/api/aspects_controller_spec.rb | 2 +- .../api/comments_controller_spec.rb | 6 +----- .../api/contacts_controller_spec.rb | 2 +- .../api/conversations_controller_spec.rb | 10 +++------- spec/integration/api/likes_controller_spec.rb | 8 ++------ .../api/messages_controller_spec.rb | 2 +- .../api/notifications_controller_spec.rb | 2 +- spec/integration/api/photos_controller_spec.rb | 2 +- .../api/reshares_controller_spec.rb | 2 +- spec/integration/api/search_controller_spec.rb | 2 +- .../integration/api/streams_controller_spec.rb | 2 +- spec/integration/api/users_controller_spec.rb | 18 +++++++----------- 26 files changed, 46 insertions(+), 53 deletions(-) diff --git a/app/controllers/api/v1/aspects_controller.rb b/app/controllers/api/v1/aspects_controller.rb index 6ce5b9a47..a604c5eb4 100644 --- a/app/controllers/api/v1/aspects_controller.rb +++ b/app/controllers/api/v1/aspects_controller.rb @@ -15,7 +15,7 @@ module Api aspects_query = current_user.aspects aspects_page = index_pager(aspects_query).response aspects_page[:data] = aspects_page[:data].map {|a| aspect_as_json(a, false) } - render json: aspects_page + render_paged_api_response aspects_page end def show diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index d7c5f350c..e112b06f1 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -49,6 +49,15 @@ module Api Api::Paging::RestPaginatorBuilder.new(query, request).index_pager(params) end + def render_paged_api_response(page) + link_header = [] + link_header << %(<#{page[:links][:next]}>; rel="next") if page[:links][:next] + link_header << %(<#{page[:links][:previous]}>; rel="previous") if page[:links][:previous] + response.set_header("Link", link_header.join(", ")) if link_header.present? + + render json: page[:data] + end + def time_pager(query) Api::Paging::RestPaginatorBuilder.new(query, request).time_pager(params) end diff --git a/app/controllers/api/v1/comments_controller.rb b/app/controllers/api/v1/comments_controller.rb index 1a26212a3..be00f9e7e 100644 --- a/app/controllers/api/v1/comments_controller.rb +++ b/app/controllers/api/v1/comments_controller.rb @@ -35,7 +35,7 @@ module Api comments_page = time_pager(comments_query).response comments_page[:data] = comments_page[:data].map {|x| comment_as_json(x) } - render json: comments_page + render_paged_api_response comments_page end def destroy diff --git a/app/controllers/api/v1/contacts_controller.rb b/app/controllers/api/v1/contacts_controller.rb index 7d3bac3c9..50760d0ea 100644 --- a/app/controllers/api/v1/contacts_controller.rb +++ b/app/controllers/api/v1/contacts_controller.rb @@ -23,7 +23,7 @@ module Api contacts_page[:data] = contacts_page[:data].map do |c| ContactPresenter.new(c, current_user).as_api_json_without_contact end - render json: contacts_page + render_paged_api_response contacts_page end def create diff --git a/app/controllers/api/v1/conversations_controller.rb b/app/controllers/api/v1/conversations_controller.rb index 2a8f58bd7..23bdcf210 100644 --- a/app/controllers/api/v1/conversations_controller.rb +++ b/app/controllers/api/v1/conversations_controller.rb @@ -22,7 +22,7 @@ module Api conversations_query = conversation_service.all_for_user(mapped_params) conversations_page = pager(conversations_query, "conversations.created_at").response conversations_page[:data] = conversations_page[:data].map {|x| conversation_as_json(x) } - render json: conversations_page + render_paged_api_response conversations_page end def show diff --git a/app/controllers/api/v1/likes_controller.rb b/app/controllers/api/v1/likes_controller.rb index d71bc9e0d..ba524c236 100644 --- a/app/controllers/api/v1/likes_controller.rb +++ b/app/controllers/api/v1/likes_controller.rb @@ -26,7 +26,7 @@ module Api likes_query = like_service.find_for_post(params[:post_id]) likes_page = index_pager(likes_query).response likes_page[:data] = likes_page[:data].map {|x| like_json(x) } - render json: likes_page + render_paged_api_response likes_page end def create diff --git a/app/controllers/api/v1/messages_controller.rb b/app/controllers/api/v1/messages_controller.rb index 309c1c8cf..dc2839447 100644 --- a/app/controllers/api/v1/messages_controller.rb +++ b/app/controllers/api/v1/messages_controller.rb @@ -27,7 +27,7 @@ module Api conversation.set_read(current_user) messages_page = index_pager(conversation.messages).response messages_page[:data] = messages_page[:data].map {|x| message_json(x) } - render json: messages_page + render_paged_api_response messages_page end private diff --git a/app/controllers/api/v1/notifications_controller.rb b/app/controllers/api/v1/notifications_controller.rb index 3003cf05c..a00063b4e 100644 --- a/app/controllers/api/v1/notifications_controller.rb +++ b/app/controllers/api/v1/notifications_controller.rb @@ -29,7 +29,7 @@ module Api notifications_page[:data] = notifications_page[:data].map do |note| NotificationPresenter.new(note, default_serializer_options).as_api_json end - render json: notifications_page + render_paged_api_response notifications_page rescue ArgumentError render json: I18n.t("api.endpoint_errors.notifications.cant_process"), status: :unprocessable_entity end diff --git a/app/controllers/api/v1/photos_controller.rb b/app/controllers/api/v1/photos_controller.rb index b4790bc5c..d56012311 100644 --- a/app/controllers/api/v1/photos_controller.rb +++ b/app/controllers/api/v1/photos_controller.rb @@ -23,7 +23,7 @@ module Api end photos_page = time_pager(query).response photos_page[:data] = photos_page[:data].map {|photo| photo_json(photo) } - render json: photos_page + render_paged_api_response photos_page end def show diff --git a/app/controllers/api/v1/reshares_controller.rb b/app/controllers/api/v1/reshares_controller.rb index da3c3d881..48f9edf81 100644 --- a/app/controllers/api/v1/reshares_controller.rb +++ b/app/controllers/api/v1/reshares_controller.rb @@ -28,7 +28,7 @@ module Api author: PersonPresenter.new(r.author).as_api_json } end - render json: reshares_page + render_paged_api_response reshares_page end def create diff --git a/app/controllers/api/v1/search_controller.rb b/app/controllers/api/v1/search_controller.rb index e9663ff75..512e9dba4 100644 --- a/app/controllers/api/v1/search_controller.rb +++ b/app/controllers/api/v1/search_controller.rb @@ -14,13 +14,13 @@ module Api def user_index user_page = index_pager(people_query).response user_page[:data] = user_page[:data].map {|p| PersonPresenter.new(p).as_api_json } - render json: user_page + render_paged_api_response user_page end def post_index posts_page = time_pager(posts_query, "posts.created_at", "created_at").response posts_page[:data] = posts_page[:data].map {|post| PostPresenter.new(post).as_api_response } - render json: posts_page + render_paged_api_response posts_page end private diff --git a/app/controllers/api/v1/streams_controller.rb b/app/controllers/api/v1/streams_controller.rb index efc5e4ec6..81f56e409 100644 --- a/app/controllers/api/v1/streams_controller.rb +++ b/app/controllers/api/v1/streams_controller.rb @@ -54,7 +54,7 @@ module Api posts_page = pager(query, query_time_field, data_time_field).response posts_page[:data] = posts_page[:data].map {|post| PostPresenter.new(post, current_user).as_api_response } posts_page[:links].delete(:previous) - render json: posts_page + render_paged_api_response posts_page end def stream_max_time diff --git a/app/controllers/api/v1/tag_followings_controller.rb b/app/controllers/api/v1/tag_followings_controller.rb index d1a714586..ab51c70fa 100755 --- a/app/controllers/api/v1/tag_followings_controller.rb +++ b/app/controllers/api/v1/tag_followings_controller.rb @@ -12,7 +12,7 @@ module Api end def index - render json: tag_followings_service.index.map(&:name) + render json: tag_followings_service.index.pluck(:name) end def create diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 4789f4cc2..cdf0db426 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -57,7 +57,7 @@ module Api contacts_query = aspects_service.all_contacts contacts_page = index_pager(contacts_query).response contacts_page[:data] = contacts_page[:data].map {|c| PersonPresenter.new(c.person).as_api_json } - render json: contacts_page + render_paged_api_response contacts_page end def photos @@ -66,7 +66,7 @@ module Api photos_query = Photo.visible(user_for_query, person, :all, Time.current) photos_page = time_pager(photos_query).response photos_page[:data] = photos_page[:data].map {|photo| PhotoPresenter.new(photo).as_api_json(true) } - render json: photos_page + render_paged_api_response photos_page end def posts @@ -78,7 +78,7 @@ module Api end posts_page = time_pager(posts_query).response posts_page[:data] = posts_page[:data].map {|post| PostPresenter.new(post, current_user).as_api_response } - render json: posts_page + render_paged_api_response posts_page end private diff --git a/spec/integration/api/aspects_controller_spec.rb b/spec/integration/api/aspects_controller_spec.rb index 2a948b2c4..c255f7a76 100644 --- a/spec/integration/api/aspects_controller_spec.rb +++ b/spec/integration/api/aspects_controller_spec.rb @@ -342,6 +342,6 @@ describe Api::V1::AspectsController do end def response_body_data(response) - JSON.parse(response.body)["data"] + JSON.parse(response.body) end end diff --git a/spec/integration/api/comments_controller_spec.rb b/spec/integration/api/comments_controller_spec.rb index fd4eb2b1a..41befd84c 100644 --- a/spec/integration/api/comments_controller_spec.rb +++ b/spec/integration/api/comments_controller_spec.rb @@ -139,7 +139,7 @@ describe Api::V1::CommentsController do params: {access_token: access_token_minimum_scopes} ) expect(response.status).to eq(200) - comments = response_body_data(response) + comments = response_body(response) expect(comments.length).to eq(2) confirm_comment_format(comments[0], auth.user, @comment_text1) confirm_comment_format(comments[1], auth.user, @comment_text2) @@ -435,10 +435,6 @@ describe Api::V1::CommentsController do JSON.parse(response.body) end - def response_body_data(response) - JSON.parse(response.body)["data"] - end - private # rubocop:disable Metrics/AbcSize diff --git a/spec/integration/api/contacts_controller_spec.rb b/spec/integration/api/contacts_controller_spec.rb index cef791c03..faabb41d0 100644 --- a/spec/integration/api/contacts_controller_spec.rb +++ b/spec/integration/api/contacts_controller_spec.rb @@ -252,7 +252,7 @@ describe Api::V1::ContactsController do end def response_body_data(response) - JSON.parse(response.body)["data"] + JSON.parse(response.body) end def aspects_membership_service(user=auth.user) diff --git a/spec/integration/api/conversations_controller_spec.rb b/spec/integration/api/conversations_controller_spec.rb index 5addd1dc3..c88d53cda 100644 --- a/spec/integration/api/conversations_controller_spec.rb +++ b/spec/integration/api/conversations_controller_spec.rb @@ -165,7 +165,7 @@ describe Api::V1::ConversationsController do it "returns all the user conversations" do get api_v1_conversations_path, params: {access_token: access_token} expect(response.status).to eq(200) - returned_conversations = response_body_data(response) + returned_conversations = response_body(response) expect(returned_conversations.length).to eq(3) actual_conversation = returned_conversations.select {|c| c["guid"] == @read_conversation_guid }[0] confirm_conversation_format(actual_conversation, @read_conversation, [auth.user, alice]) @@ -179,7 +179,7 @@ describe Api::V1::ConversationsController do params: {only_unread: true, access_token: access_token} ) expect(response.status).to eq(200) - expect(response_body_data(response).length).to eq(2) + expect(response_body(response).length).to eq(2) end it "returns all the user conversations after a given date" do @@ -188,7 +188,7 @@ describe Api::V1::ConversationsController do params: {only_after: @date, access_token: access_token} ) expect(response.status).to eq(200) - expect(response_body_data(response).length).to eq(1) + expect(response_body(response).length).to eq(1) end context "with improper credentials" do @@ -366,10 +366,6 @@ describe Api::V1::ConversationsController do JSON.parse(response.body) end - def response_body_data(response) - JSON.parse(response.body)["data"] - end - # rubocop:disable Metrics/AbcSize def confirm_conversation_format(conversation, ref_conversation, ref_participants) expect(conversation["guid"]).to_not be_nil diff --git a/spec/integration/api/likes_controller_spec.rb b/spec/integration/api/likes_controller_spec.rb index 615a24b34..b462ef27b 100644 --- a/spec/integration/api/likes_controller_spec.rb +++ b/spec/integration/api/likes_controller_spec.rb @@ -56,7 +56,7 @@ describe Api::V1::LikesController do params: {access_token: access_token_minimum_scopes} ) expect(response.status).to eq(200) - likes = response_body_data(response) + likes = response_body(response) expect(likes.length).to eq(0) end @@ -69,7 +69,7 @@ describe Api::V1::LikesController do params: {access_token: access_token_minimum_scopes} ) expect(response.status).to eq(200) - likes = response_body_data(response) + likes = response_body(response) expect(likes.length).to eq(3) confirm_like_format(likes, alice) confirm_like_format(likes, bob) @@ -278,8 +278,4 @@ describe Api::V1::LikesController do def response_body(response) JSON.parse(response.body) end - - def response_body_data(response) - JSON.parse(response.body)["data"] - end end diff --git a/spec/integration/api/messages_controller_spec.rb b/spec/integration/api/messages_controller_spec.rb index 72524c6cc..3fc365146 100644 --- a/spec/integration/api/messages_controller_spec.rb +++ b/spec/integration/api/messages_controller_spec.rb @@ -161,7 +161,7 @@ describe Api::V1::MessagesController do private def response_body_data(response) - JSON.parse(response.body)["data"] + JSON.parse(response.body) end def get_conversation(conversation_id) diff --git a/spec/integration/api/notifications_controller_spec.rb b/spec/integration/api/notifications_controller_spec.rb index 999c6515b..6c685537c 100644 --- a/spec/integration/api/notifications_controller_spec.rb +++ b/spec/integration/api/notifications_controller_spec.rb @@ -227,7 +227,7 @@ describe Api::V1::NotificationsController do private def response_body_data(response) - JSON.parse(response.body)["data"] + JSON.parse(response.body) end # rubocop:disable Metrics/AbcSize diff --git a/spec/integration/api/photos_controller_spec.rb b/spec/integration/api/photos_controller_spec.rb index c945e7dfe..48f5837c7 100644 --- a/spec/integration/api/photos_controller_spec.rb +++ b/spec/integration/api/photos_controller_spec.rb @@ -363,7 +363,7 @@ describe Api::V1::PhotosController do end def response_body_data(response) - response_body(response)["data"] + response_body(response) end # rubocop:disable Metrics/AbcSize diff --git a/spec/integration/api/reshares_controller_spec.rb b/spec/integration/api/reshares_controller_spec.rb index 2bf324f6c..5f3cd7c79 100644 --- a/spec/integration/api/reshares_controller_spec.rb +++ b/spec/integration/api/reshares_controller_spec.rb @@ -220,7 +220,7 @@ describe Api::V1::ResharesController do end def response_body_data(response) - JSON.parse(response.body)["data"] + JSON.parse(response.body) end # rubocop:disable Metrics/AbcSize diff --git a/spec/integration/api/search_controller_spec.rb b/spec/integration/api/search_controller_spec.rb index f8301637f..cc22f457a 100644 --- a/spec/integration/api/search_controller_spec.rb +++ b/spec/integration/api/search_controller_spec.rb @@ -224,6 +224,6 @@ describe Api::V1::SearchController do end def response_body_data(response) - JSON.parse(response.body)["data"] + JSON.parse(response.body) end end diff --git a/spec/integration/api/streams_controller_spec.rb b/spec/integration/api/streams_controller_spec.rb index 750a56954..5a88b53fb 100644 --- a/spec/integration/api/streams_controller_spec.rb +++ b/spec/integration/api/streams_controller_spec.rb @@ -474,7 +474,7 @@ describe Api::V1::StreamsController do # rubocop:enable Metrics/AbcSize def response_body_data(response) - JSON.parse(response.body)["data"] + JSON.parse(response.body) end def add_tag(name, user) diff --git a/spec/integration/api/users_controller_spec.rb b/spec/integration/api/users_controller_spec.rb index 18bd15c01..23a2ae32d 100644 --- a/spec/integration/api/users_controller_spec.rb +++ b/spec/integration/api/users_controller_spec.rb @@ -316,7 +316,7 @@ describe Api::V1::UsersController do params: {access_token: access_token} ) expect(response.status).to eq(200) - contacts = response_body_data(response) + contacts = response_body(response) expect(contacts.length).to eq(0) aspect = auth.user.aspects.create(name: "first") @@ -326,7 +326,7 @@ describe Api::V1::UsersController do params: {access_token: access_token} ) expect(response.status).to eq(200) - contacts = response_body_data(response) + contacts = response_body(response) expect(contacts.length).to eq(1) confirm_person_format(contacts[0], alice) @@ -391,7 +391,7 @@ describe Api::V1::UsersController do params: {access_token: access_token} ) expect(response.status).to eq(200) - photos = response_body_data(response) + photos = response_body(response) expect(photos.length).to eq(3) guids = photos.map {|photo| photo["guid"] } expect(guids).to include(@public_photo1.guid, @public_photo2.guid, @shared_photo1.guid) @@ -407,7 +407,7 @@ describe Api::V1::UsersController do params: {access_token: access_token_public_only_read_only} ) expect(response.status).to eq(200) - photos = response_body_data(response) + photos = response_body(response) expect(photos.length).to eq(2) guids = photos.map {|photo| photo["guid"] } expect(guids).to include(@public_photo1.guid, @public_photo2.guid) @@ -467,7 +467,7 @@ describe Api::V1::UsersController do params: {access_token: access_token} ) expect(response.status).to eq(200) - posts = response_body_data(response) + posts = response_body(response) expect(posts.length).to eq(3) guids = posts.map {|post| post["guid"] } expect(guids).to include(@public_post1.guid, @public_post2.guid, @shared_post1.guid) @@ -484,7 +484,7 @@ describe Api::V1::UsersController do params: {access_token: access_token} ) expect(response.status).to eq(200) - posts = response_body_data(response) + posts = response_body(response) expect(posts.length).to eq(3) end @@ -494,7 +494,7 @@ describe Api::V1::UsersController do params: {access_token: access_token_public_only_read_only} ) expect(response.status).to eq(200) - posts = response_body_data(response) + posts = response_body(response) expect(posts.length).to eq(2) end end @@ -605,8 +605,4 @@ describe Api::V1::UsersController do def response_body(response) JSON.parse(response.body) end - - def response_body_data(response) - response_body(response)["data"] - end end