require "fiona7/controllers/rest_api/obj_controller" require "fiona7/controllers/rest_api/blob_controller" require "fiona7/controllers/rest_api/workspace_controller" require "fiona7/controllers/rest_api/session_controller" require "fiona7/controllers/rest_api/error_handler" 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 #Rails.logger.debug "F7 REST GET: #{resource_path}" 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\/by_modified_obj/ Fiona7::Controllers::RestAPI::WorkspaceController.new.by_modified_obj() 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\/mget\Z/ _, _, workspace_id, _, _ = *resource_path.split("/") Fiona7::Controllers::RestAPI::ObjController.new.fetch_multiple2(workspace_id, payload) when /\A\/?workspaces\/[a-zA-Z0-9_-]+\/base_objs\/mget\Z/ Fiona7::Controllers::RestAPI::ObjController.new.fetch_multiple2('published', payload) 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\/resolve_urls\Z/ Fiona7::Controllers::RestAPI::BlobController.new.resolve_urls(payload) 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 original_get(resource_path, payload, options||{}) end end rescue *Fiona7::ErrorHandler::HANDLED_ERRORS => e raise Fiona7::ErrorHandler.new(e).sdk_error 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 original_delete(resource_path, payload, options||{}) end rescue *Fiona7::ErrorHandler::HANDLED_ERRORS => e raise Fiona7::ErrorHandler.new(e).sdk_error 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 #Rails.logger.debug "F7 REST 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 original_post(resource_path, payload, options||{}) end rescue *Fiona7::ErrorHandler::HANDLED_ERRORS => e raise Fiona7::ErrorHandler.new(e).sdk_error 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 #Rails.logger.debug "F7 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) when /\A\/?blobs\/[a-zA-Z0-9]+\/copy/ _, _, blob_id, _ = * resource_path.split("/") Fiona7::Controllers::RestAPI::BlobController.new.copy(blob_id, payload) when /\A\/?sessions\/[a-zA-Z0-9]+/ _, _, session_id = *resource_path.split("/") Fiona7::Controllers::RestAPI::SessionController.new.update(session_id, payload) else original_put(resource_path, payload, options||{}) end rescue *Fiona7::ErrorHandler::HANDLED_ERRORS => e raise Fiona7::ErrorHandler.new(e).sdk_error end end end