Merge pull request #2485 from mmd-osm/patch/json2
JSON output nodes, ways, relations, map
This commit is contained in:
commit
73c95847a6
43 changed files with 566 additions and 13 deletions
|
@ -5,6 +5,8 @@ module Api
|
|||
before_action :check_api_readable
|
||||
around_action :api_call_handle_error, :api_call_timeout
|
||||
|
||||
before_action :set_default_request_format
|
||||
|
||||
# This is probably the most common call of all. It is used for getting the
|
||||
# OSM data for a specified bounding box, usually for editing. First the
|
||||
# bounding box (bbox) is checked to make sure that it is sane. All nodes
|
||||
|
@ -90,7 +92,10 @@ module Api
|
|||
|
||||
response.headers["Content-Disposition"] = "attachment; filename=\"map.osm\""
|
||||
# Render the result
|
||||
render :formats => [:xml]
|
||||
respond_to do |format|
|
||||
format.xml
|
||||
format.json
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -13,6 +13,8 @@ module Api
|
|||
before_action :check_api_readable, :except => [:create, :update, :delete]
|
||||
around_action :api_call_handle_error, :api_call_timeout
|
||||
|
||||
before_action :set_default_request_format, :except => [:create, :update, :delete]
|
||||
|
||||
# Create a node from XML.
|
||||
def create
|
||||
assert_method :put
|
||||
|
@ -32,7 +34,10 @@ module Api
|
|||
|
||||
if @node.visible
|
||||
# Render the result
|
||||
render :formats => [:xml]
|
||||
respond_to do |format|
|
||||
format.xml
|
||||
format.json
|
||||
end
|
||||
else
|
||||
head :gone
|
||||
end
|
||||
|
@ -73,7 +78,10 @@ module Api
|
|||
@nodes = Node.find(ids)
|
||||
|
||||
# Render the result
|
||||
render :formats => [:xml]
|
||||
respond_to do |format|
|
||||
format.xml
|
||||
format.json
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,6 +16,8 @@ module Api
|
|||
before_action :lookup_old_element, :except => [:history]
|
||||
before_action :lookup_old_element_versions, :only => [:history]
|
||||
|
||||
before_action :set_default_request_format, :except => [:redact]
|
||||
|
||||
def history
|
||||
# the .where() method used in the lookup_old_element_versions
|
||||
# call won't throw an error if no records are found, so we have
|
||||
|
@ -30,7 +32,10 @@ module Api
|
|||
end
|
||||
|
||||
# Render the result
|
||||
render :formats => [:xml]
|
||||
respond_to do |format|
|
||||
format.xml
|
||||
format.json
|
||||
end
|
||||
end
|
||||
|
||||
def version
|
||||
|
@ -41,7 +46,10 @@ module Api
|
|||
response.last_modified = @old_element.timestamp
|
||||
|
||||
# Render the result
|
||||
render :formats => [:xml]
|
||||
respond_to do |format|
|
||||
format.xml
|
||||
format.json
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -11,6 +11,8 @@ module Api
|
|||
before_action :check_api_readable, :except => [:create, :update, :delete]
|
||||
around_action :api_call_handle_error, :api_call_timeout
|
||||
|
||||
before_action :set_default_request_format, :except => [:create, :update, :delete]
|
||||
|
||||
def create
|
||||
assert_method :put
|
||||
|
||||
|
@ -26,7 +28,10 @@ module Api
|
|||
response.last_modified = @relation.timestamp
|
||||
if @relation.visible
|
||||
# Render the result
|
||||
render :formats => [:xml]
|
||||
respond_to do |format|
|
||||
format.xml
|
||||
format.json
|
||||
end
|
||||
else
|
||||
head :gone
|
||||
end
|
||||
|
@ -117,7 +122,10 @@ module Api
|
|||
@relations << relation
|
||||
|
||||
# Render the result
|
||||
render :formats => [:xml]
|
||||
respond_to do |format|
|
||||
format.xml
|
||||
format.json
|
||||
end
|
||||
else
|
||||
head :gone
|
||||
end
|
||||
|
@ -133,7 +141,10 @@ module Api
|
|||
@relations = Relation.find(ids)
|
||||
|
||||
# Render the result
|
||||
render :formats => [:xml]
|
||||
respond_to do |format|
|
||||
format.xml
|
||||
format.json
|
||||
end
|
||||
end
|
||||
|
||||
def relations_for_way
|
||||
|
@ -160,7 +171,10 @@ module Api
|
|||
end
|
||||
|
||||
# Render the result
|
||||
render :formats => [:xml]
|
||||
respond_to do |format|
|
||||
format.xml
|
||||
format.json
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,6 +11,8 @@ module Api
|
|||
before_action :check_api_readable, :except => [:create, :update, :delete]
|
||||
around_action :api_call_handle_error, :api_call_timeout
|
||||
|
||||
before_action :set_default_request_format, :except => [:create, :update, :delete]
|
||||
|
||||
def create
|
||||
assert_method :put
|
||||
|
||||
|
@ -28,7 +30,10 @@ module Api
|
|||
|
||||
if @way.visible
|
||||
# Render the result
|
||||
render :formats => [:xml]
|
||||
respond_to do |format|
|
||||
format.xml
|
||||
format.json
|
||||
end
|
||||
else
|
||||
head :gone
|
||||
end
|
||||
|
@ -75,7 +80,10 @@ module Api
|
|||
end
|
||||
|
||||
# Render the result
|
||||
render :formats => [:xml]
|
||||
respond_to do |format|
|
||||
format.xml
|
||||
format.json
|
||||
end
|
||||
else
|
||||
head :gone
|
||||
end
|
||||
|
@ -93,7 +101,10 @@ module Api
|
|||
@ways = Way.find(ids)
|
||||
|
||||
# Render the result
|
||||
render :formats => [:xml]
|
||||
respond_to do |format|
|
||||
format.xml
|
||||
format.json
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
|
@ -106,7 +117,10 @@ module Api
|
|||
@ways = Way.where(:id => wayids, :visible => true)
|
||||
|
||||
# Render the result
|
||||
render :formats => [:xml]
|
||||
respond_to do |format|
|
||||
format.xml
|
||||
format.json
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,6 +3,56 @@ class ApiController < ApplicationController
|
|||
|
||||
private
|
||||
|
||||
##
|
||||
# Set default request format to xml unless a client requests a specific format,
|
||||
# which can be done via (a) URL suffix and/or (b) HTTP Accept header, where
|
||||
# the URL suffix always takes precedence over the Accept header.
|
||||
def set_default_request_format
|
||||
unless params[:format]
|
||||
accept_header = request.headers["HTTP_ACCEPT"]
|
||||
if accept_header.nil?
|
||||
# e.g. unit tests don't set an Accept: header by default, force XML in this case
|
||||
request.format = "xml"
|
||||
return
|
||||
end
|
||||
|
||||
req_mimetypes = []
|
||||
|
||||
# Some clients (JOSM) send Accept headers which cannot be parsed by Rails, example: *; q=.2
|
||||
# To be fair, JOSM's Accept header doesn't adhere to RFC 7231, section 5.3.1, et al. either
|
||||
# As a workaround for backwards compatibility, we're assuming XML format
|
||||
begin
|
||||
req_mimetypes = Mime::Type.parse(accept_header)
|
||||
rescue Mime::Type::InvalidMimeType
|
||||
request.format = "xml"
|
||||
return
|
||||
end
|
||||
|
||||
# req_mimetypes contains all Accept header MIME types with descending priority
|
||||
req_mimetypes.each do |mime|
|
||||
if mime.symbol == :xml
|
||||
request.format = "xml"
|
||||
break
|
||||
end
|
||||
|
||||
if mime.symbol == :json
|
||||
request.format = "json"
|
||||
break
|
||||
end
|
||||
|
||||
# Any format, not explicitly requesting XML or JSON -> assume XML as default
|
||||
if mime == "*/*"
|
||||
request.format = "xml"
|
||||
break
|
||||
end
|
||||
|
||||
# In case the client requests some other format besides XML, JSON and */*,
|
||||
# we deliberately don't set request.format. The framework will return an
|
||||
# ActionController::UnknownFormat error to the client later on in this case.
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def authorize(realm = "Web Password", errormessage = "Couldn't authenticate you")
|
||||
# make the current_user object from any auth sources we have
|
||||
setup_user_auth
|
||||
|
|
|
@ -185,6 +185,8 @@ class ApplicationController < ActionController::Base
|
|||
|
||||
def api_call_handle_error
|
||||
yield
|
||||
rescue ActionController::UnknownFormat
|
||||
head :not_acceptable
|
||||
rescue ActiveRecord::RecordNotFound => e
|
||||
head :not_found
|
||||
rescue LibXML::XML::Error, ArgumentError => e
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue