From 921942feb3fac1d8c162da94cb8036aaa39c4105 Mon Sep 17 00:00:00 2001 From: Bruce Bolt Date: Tue, 18 Jul 2023 10:52:50 +0100 Subject: [PATCH] Change source for `GdsApi.worldwide.world_locations` method This method currently returns data retrieved from the Whitehall API, which is being deprecated. Therefore updating the method to get the data from the World Content Item. This is a breaking change as we have removed the pagination from the response. This is to avoid multiple requests to the content item, when all the results are included anyway. --- CHANGELOG.md | 1 + lib/gds_api/test_helpers/worldwide.rb | 74 +-------------------------- lib/gds_api/worldwide.rb | 2 +- test/pacts/worldwide_api_pact_test.rb | 42 --------------- test/worldwide_api_test.rb | 21 +------- 5 files changed, 5 insertions(+), 135 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 554ea376..9b2aa10f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Unreleased +* BREAKING: Change source for `GdsApi.worldwide.world_locations` method and remove pagination. * Change source for `GdsApi.worldwide.world_location` method (the response is backwards compatible). # 88.2.0 diff --git a/lib/gds_api/test_helpers/worldwide.rb b/lib/gds_api/test_helpers/worldwide.rb index b411e8f3..10baefc3 100644 --- a/lib/gds_api/test_helpers/worldwide.rb +++ b/lib/gds_api/test_helpers/worldwide.rb @@ -8,11 +8,6 @@ module Worldwide WORLDWIDE_API_ENDPOINT = Plek.new.website_root - # Sets up the index endpoints for the given country slugs - # The stubs are setup to paginate in chunks of 20 - # - # This also sets up the individual endpoints for each slug - # by calling stub_worldwide_api_has_location below def stub_worldwide_api_has_locations(location_slugs) international_delegation_slugs = location_slugs.select do |slug| slug =~ /(delegation|mission)/ @@ -51,48 +46,10 @@ def stub_worldwide_api_has_locations(location_slugs) stub_request(:get, "#{WORLDWIDE_API_ENDPOINT}/api/content/world") .to_return(status: 200, body: content_item.to_json) - - location_slugs.each { |s| stub_worldwide_api_has_location(s) } - pages = [] - location_slugs.each_slice(20) do |slugs| - pages << slugs.map { |s| stub_worldwide_api_has_locations(s) } - end - - pages.each_with_index do |page, i| - page_details = plural_response_base.merge( - "results" => page, - "total" => location_slugs.size, - "pages" => pages.size, - "current_page" => i + 1, - "page_size" => 20, - "start_index" => i * 20 + 1, - ) - - links = { self: "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i + 1}" } - links[:next] = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i + 2}" if pages[i + 1] - links[:previous] = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i}" unless i.zero? - page_details["_response_info"]["links"] = [] - link_headers = [] - links.each do |rel, href| - page_details["_response_info"]["links"] << { "rel" => rel, "href" => href } - link_headers << "<#{href}>; rel=\"#{rel}\"" - end - - stub_request(:get, links[:self]) - .to_return(status: 200, body: page_details.to_json, headers: { "Link" => link_headers.join(", ") }) - - next unless i.zero? - - # First page exists at URL with and without page param - stub_request(:get, links[:self].sub(/\?page=1/, "")) - .to_return(status: 200, body: page_details.to_json, headers: { "Link" => link_headers.join(", ") }) - end end - def stub_worldwide_api_has_location(location_slug, details = nil) - details ||= world_location_for_slug(location_slug) - stub_request(:get, "#{WORLDWIDE_API_ENDPOINT}/api/world-locations/#{location_slug}") - .to_return(status: 200, body: details.to_json) + def stub_worldwide_api_has_location(location_slug) + stub_worldwide_api_has_locations([location_slug]) end def stub_worldwide_api_has_organisations_for_location(location_slug, json_or_hash) @@ -108,33 +65,6 @@ def stub_worldwide_api_has_no_organisations_for_location(location_slug) stub_request(:get, url) .to_return(status: 200, body: details.to_json, headers: { "Link" => "<#{url}; rel\"self\"" }) end - - def world_location_for_slug(slug) - singular_response_base.merge(stub_worldwide_api_has_locations(slug)) - end - - # Constructs a sample world_location - # - # if the slug contains 'delegation' or 'mission' the format will be set to 'International delegation' - # othersiwe it will be set to 'World location' - def stub_worldwide_api_has_locations(slug) - { - "id" => "https://www.gov.uk/api/world-locations/#{slug}", - "title" => titleize_slug(slug, title_case: true), - "format" => (slug =~ /(delegation|mission)/ ? "International delegation" : "World location"), - "updated_at" => "2013-03-25T13:06:42+00:00", - "web_url" => "https://www.gov.uk/government/world/#{slug}", - "details" => { - "slug" => slug, - "iso2" => slug[0..1].upcase, - }, - "organisations" => { - "id" => "https://www.gov.uk/api/world-locations/#{slug}/organisations", - "web_url" => "https://www.gov.uk/government/world/#{slug}#organisations", - }, - "content_id" => "content_id_for_#{slug}", - } - end end end end diff --git a/lib/gds_api/worldwide.rb b/lib/gds_api/worldwide.rb index c5ff1abe..d6bf274b 100644 --- a/lib/gds_api/worldwide.rb +++ b/lib/gds_api/worldwide.rb @@ -2,7 +2,7 @@ class GdsApi::Worldwide < GdsApi::Base def world_locations - get_list("#{base_url}/world-locations") + all_world_locations end def world_location(location_slug) diff --git a/test/pacts/worldwide_api_pact_test.rb b/test/pacts/worldwide_api_pact_test.rb index 561844c1..1735e3d2 100644 --- a/test/pacts/worldwide_api_pact_test.rb +++ b/test/pacts/worldwide_api_pact_test.rb @@ -6,48 +6,6 @@ let(:api_client) { GdsApi::Worldwide.new(whitehall_api_host) } - describe "#world_locations" do - it "responds with 200 and all world locations" do - whitehall_api - .given("a world location exists") - .upon_receiving("a request to return all world locations") - .with( - method: :get, - path: "/api/world-locations", - headers: GdsApi::JsonClient.default_request_headers, - ) - .will_respond_with( - status: 200, - body: { - results: [ - Pact.like( - id: "https://www.gov.uk/api/world-locations/france", - title: "France", - format: "World location", - updated_at: "2020-09-02T06:47:34.000+01:00", - web_url: "https://www.gov.uk/world/france", - analytics_identifier: "WL1", - details: { - slug: "france", - iso2: nil, - }, - organisations: { - id: "https://www.gov.uk/api/world-locations/france/organisations", - web_url: "https://www.gov.uk/world/france#organisations", - }, - content_id: "5e9ecbce-7706-11e4-a3cb-005056011aef", - ), - ], - }, - headers: { - "Content-Type" => "application/json; charset=utf-8", - }, - ) - - api_client.world_locations - end - end - describe "#world_location" do describe "#organisations_for_world_location" do it "responds with 200 and all worldwide organisations" do diff --git a/test/worldwide_api_test.rb b/test/worldwide_api_test.rb index 90007cb9..7f38d69d 100644 --- a/test/worldwide_api_test.rb +++ b/test/worldwide_api_test.rb @@ -17,26 +17,7 @@ stub_worldwide_api_has_locations(country_slugs) response = @api.world_locations - assert_equal(country_slugs, response.map { |r| r["details"]["slug"] }) - assert_equal "Rohan", response["results"][2]["title"] - end - - it "should handle the pagination" do - country_slugs = (1..50).map { |n| "country-#{n}" } - stub_worldwide_api_has_locations(country_slugs) - - response = @api.world_locations - assert_equal( - country_slugs, - response.with_subsequent_pages.map { |r| r["details"]["slug"] }, - ) - end - - it "should raise error if endpoint 404s" do - stub_request(:get, "#{@base_api_url}/api/world-locations").to_return(status: 404) - assert_raises GdsApi::HTTPNotFound do - @api.world_locations - end + assert_equal(country_slugs, response.map { |r| r.dig("details", "slug") }) end end