From: Andy Allan Date: Wed, 26 Jun 2019 14:38:20 +0000 (+0200) Subject: Add support for an unversioned 'versions' API call X-Git-Tag: live~2537^2 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/2398614349e3ff5605868fea82e013d2a9a16ef9 Add support for an unversioned 'versions' API call This will eventually replace the unversioned 'capabilities' call. Client applications can use this versions call to find out what versions are available, if they support multiple versions. The capabilities of each version, and the format of the capabilities call, is then able to change between versions. If a client only supports one version they can jump straight to the versioned capabilities call. Additionally, supported versions are now a list of version identifiers, without any implications surrounding min/max values (e.g. guesswork for supported intermediate versions). Fixes #2162 --- diff --git a/app/abilities/api_ability.rb b/app/abilities/api_ability.rb index 9fc3cdd9c..80245eeaa 100644 --- a/app/abilities/api_ability.rb +++ b/app/abilities/api_ability.rb @@ -9,6 +9,7 @@ class ApiAbility can :index, :map can :show, :permission can [:search_all, :search_nodes, :search_ways, :search_relations], :search + can :show, :version if Settings.status != "database_offline" can [:show, :download, :query], Changeset diff --git a/app/controllers/api/versions_controller.rb b/app/controllers/api/versions_controller.rb new file mode 100644 index 000000000..7de335091 --- /dev/null +++ b/app/controllers/api/versions_controller.rb @@ -0,0 +1,15 @@ +module Api + class VersionsController < ApiController + authorize_resource :class => false + + around_action :api_call_handle_error, :api_call_timeout + + # Show the list of available API versions. This will replace the global + # unversioned capabilities call in due course. + # Currently we only support deploying one version at a time, but this will + # hopefully change soon. + def show + @versions = [Settings.api_version] + end + end +end diff --git a/app/views/api/versions/show.builder b/app/views/api/versions/show.builder new file mode 100644 index 000000000..ed20915ee --- /dev/null +++ b/app/views/api/versions/show.builder @@ -0,0 +1,8 @@ +xml.instruct! :xml, :version => "1.0" +xml.osm(OSM::API.new.xml_root_attributes.except("version")) do |osm| + osm.api do |api| + @versions.each do |version| + api.version version + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 7689b506d..002ee58ea 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,8 @@ OpenStreetMap::Application.routes.draw do # API namespace :api do - get "capabilities" => "capabilities#show" + get "capabilities" => "capabilities#show" # Deprecated, remove when 0.6 support is removed + get "versions" => "versions#show" end scope "api/0.6" do diff --git a/test/controllers/api/versions_controller_test.rb b/test/controllers/api/versions_controller_test.rb new file mode 100644 index 000000000..f7f514035 --- /dev/null +++ b/test/controllers/api/versions_controller_test.rb @@ -0,0 +1,34 @@ +require "test_helper" + +module Api + class VersionsControllerTest < ActionController::TestCase + ## + # test all routes which lead to this controller + def test_routes + assert_routing( + { :path => "/api/versions", :method => :get }, + { :controller => "api/versions", :action => "show" } + ) + assert_recognizes( + { :controller => "api/versions", :action => "show" }, + { :path => "/api/versions", :method => :get } + ) + end + + def test_versions + get :show + assert_response :success + assert_select "osm[generator='#{Settings.generator}']", :count => 1 do + assert_select "api", :count => 1 do + assert_select "version", Settings.api_version + end + end + end + + def test_no_version_in_root_element + get :show + assert_response :success + assert_select "osm[version]", :count => 0 + end + end +end