require "fiona7/controllers/rest_api/obj_controller" require "fiona7/controllers/rest_api/blob_controller" require "fiona7/controllers/rest_api/workspace_controller" class Scrivito::CmsRestApi class << self alias_method :original_task_unaware_request, :task_unaware_request def task_unaware_request(method, resource_path, payload = nil) raise "Unexpected method #{method}" unless [:delete, :get, :post, :put].include?(method) self.send(method, resource_path, payload) end alias_method :original_get, :get def get(resource_path, payload = nil, options = nil) @number_of_requests += 1 if resource_path == @count_requests action = :get ActiveSupport::Notifications.instrumenter.instrument( "backend_request.scrivito", { :path => resource_path, :verb => action, :params => action == :get ? payload : nil } ) do if !resource_path.start_with?('/') resource_path = '/' + resource_path end $stdout.puts "DEBUG GET: #{resource_path}" pp payload case resource_path when /\A\/?revisions\/[a-zA-Z0-9_-]+\/objs\/mget\Z/ _, _, revision_id, _, _ = *resource_path.split("/") Fiona7::Controllers::RestAPI::ObjController.new.fetch_multiple(revision_id, payload) when /\A\/?revisions\/[a-zA-Z0-9_-]+\/objs\/[a-zA-Z0-9_-]+\Z/ _, _, revision_id, _, obj_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::ObjController.new.fetch_by_id_from_revision(revision_id, obj_id) when /\A\/?workspaces\Z/ Fiona7::Controllers::RestAPI::WorkspaceController.new.all when /\A\/?workspaces\/[a-zA-Z0-9_-]+\/objs\/find_by_path/ _, _, workspace_id = *resource_path.split("/") path = resource_path.match(/path=(.*)\Z/)[1] Fiona7::Controllers::RestAPI::ObjController.new.fetch_by_path(workspace_id, path) when /\A\/?workspaces\/[a-zA-Z0-9_-]+\/objs\/search/ _, _, workspace_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::ObjController.new.search(workspace_id, payload) when /\A\/?workspaces\/[a-zA-Z0-9_-]+\Z/ _, _, workspace_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::WorkspaceController.new.fetch(workspace_id) when /\A\/?workspaces\/[a-zA-Z0-9_-]+\/changes\Z/ _, _, workspace_id,_ = *resource_path.split("/") Fiona7::Controllers::RestAPI::WorkspaceController.new.changes(workspace_id) when /\A\/?workspaces\/[a-zA-Z0-9_-]+\/objs\/[a-zA-Z0-9_-]+\Z/ _, _, workspace_id, _, obj_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::ObjController.new.fetch_by_id(workspace_id, obj_id) when /\A\/?blobs\/[a-zA-Z0-9_-]+\Z/ _, _, blob_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::BlobController.new.fetch(blob_id) when /\A\/?blobs\/[a-zA-Z0-9_-]+\/no_cache\Z/ _, _, blob_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::BlobController.new.fetch(blob_id) when /\A\/?blobs\/[a-zA-Z0-9_-]+\/transform\Z/ _, _, blob_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::BlobController.new.fetch(blob_id, payload[:transformation]) when /\A\/?blobs\/[a-zA-Z0-9_-]+\/meta_data\Z/ _, _, blob_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::BlobController.new.metadata(blob_id) else $stdout.puts "GET: #{resource_path}" original_get(resource_path, payload, options||{}) end end end alias_method :original_delete, :delete def delete(resource_path, payload = nil, options = nil) @number_of_requests += 1 if resource_path == @count_requests if !resource_path.start_with?('/') resource_path = '/' + resource_path end case resource_path when /\A\/?workspaces\Z/ true when /\A\/?workspaces\/[a-zA-Z0-9_-]+\Z/ raise Scrivito::ScrivitoError, 'Delete operation unsupported for workspaces' _, _, workspace_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::WorkspaceController.new.delete(workspace_id) when /\A\/?workspaces\/[a-zA-Z0-9_-]+\/objs\/[a-zA-Z0-9_-]+\Z/ _, _, workspace_id, _, obj_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::ObjController.new.destroy(workspace_id, obj_id) else #$stdout.puts "DELETE: #{resource_path}" original_delete(resource_path, payload, options||{}) end end alias_method :original_post, :post def post(resource_path, payload, options = nil) @number_of_requests += 1 if resource_path == @count_requests if !resource_path.start_with?('/') resource_path = '/' + resource_path end #$stdout.puts "DEBUG POST: #{resource_path}" case resource_path when /\A\/?workspaces\Z/ Fiona7::Controllers::RestAPI::WorkspaceController.new.create(payload) when /\A\/?workspaces\/[a-zA-Z0-9]+\/objs/ _, _, workspace_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::ObjController.new.create(workspace_id, payload) else #$stdout.puts "POST: #{resource_path}" original_post(resource_path, payload, options||{}) end end alias_method :original_put, :put def put(resource_path, payload, options = nil) @number_of_requests += 1 if resource_path == @count_requests if !resource_path.start_with?('/') resource_path = '/' + resource_path end #$stdout.puts "DEBUG PUT: #{resource_path}" case resource_path when /\A\/?workspaces\/[a-zA-Z0-9]+\/objs\/[a-zA-Z0-9]+\Z/ _, _, workspace_id, _, obj_id = *resource_path.split("/") payload[:obj][:_id] = obj_id Fiona7::Controllers::RestAPI::ObjController.new.update(workspace_id, payload) when /\A\/?workspaces\/[a-zA-Z0-9]+\/publish/ _, _, workspace_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::WorkspaceController.new.publish(workspace_id) when /\A\/?workspaces\/[a-zA-Z0-9]+\/rebase/ raise Scrivito::ScrivitoError, 'Rebase operation unsupported for workspaces' _, _, workspace_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::WorkspaceController.new.publish(workspace_id) when /\A\/?workspaces\/[a-zA-Z0-9]+/ _, _, workspace_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::WorkspaceController.new.update(workspace_id) else #$stdout.puts "PUT: #{resource_path}" original_put(resource_path, payload, options||{}) end end end end