class ExternalMemory::WebServer

  type 'ResourceElement',
       {
         :properties => {
           :url => {
             :type => String,
             :description => 'A url',
             :example => 'http://google.com',
           },
           :element => {
             :type => 'Element',
             :description => 'An element',
           },
         },
         :required => [:url, :element],
       }

  type 'DetailedResource',
       {
         :extends => 'Resource',
         :properties => {
           :resource_elements => {
             :type => ['ResourceElement'],
             :description => 'The resource\'s elements',
           },
         },
         :required => [:resource_elements],
       }

  type 'DetailedResourceRoot',
       {
         :properties => {
           :resource => {
             :type => 'DetailedResource',
             :description => 'An resource',
           }
         },
         :required => [:resource],
       }

  type 'Resources',
       {
         :properties => {
           :resources => {
             :type => ['Resource'],
             :description => 'A list of resources',
           }
         },
         :required => [:resources],
       }


  endpoint_tags 'resource'
  endpoint_summary 'Get a list of resources'
  endpoint_response 200, 'Resources', 'The resources'
  endpoint_response 400, 'Error', 'In case of error'
  declare_pagination
  declare_authentication
  get '/api/resource' do
    get_resources
  end

  endpoint_tags 'resource'
  endpoint_summary 'Get a resource by its id'
  endpoint_response 200, 'DetailedResourceRoot', 'The resource'
  endpoint_response 404, 'Error', 'If the resource is not found'
  endpoint_response 400, 'Error', 'In case of error'
  endpoint_parameter :resource_id, Resource::MODEL_INFO[:id][:description], :path, true, Integer
  endpoint_path '/api/resource/{resource_id}'
  declare_authentication
  get %r{/api/resource/(?<resource_id>\d+)\z} do |resource_id|
    get_resource_by_id(resource_id)
  end

  endpoint_tags 'resource'
  endpoint_summary 'Get a resource content'
  endpoint_produces '*', 'application/json'
  endpoint_response 200, 'file', 'The content'
  endpoint_response 404, 'Error', 'If the resource is not found or has no content'
  endpoint_response 400, 'Error', 'In case of error'
  endpoint_parameter :resource_id, Resource::MODEL_INFO[:id][:description], :path, true, Integer
  endpoint_path '/api/resource/{resource_id}/content'
  get %r{/api/resource/(?<resource_id>\d+)/content\z} do |resource_id|
    get_resource_content(resource_id)
  end

  endpoint_tags 'resource'
  endpoint_summary 'Get a resource html content'
  endpoint_produces 'text/html', 'application/json'
  endpoint_response 200, 'file', 'The html content'
  endpoint_response 404, 'Error', 'If the resource is not found or has no html content'
  endpoint_response 400, 'Error', 'In case of error'
  endpoint_parameter :resource_id, Resource::MODEL_INFO[:id][:description], :path, true, Integer
  endpoint_path '/api/resource/{resource_id}/html_content'
  get %r{/api/resource/(?<resource_id>\d+)/html_content\z} do |resource_id|
    get_resource_html_content(resource_id)
  end

  endpoint_tags 'resource'
  endpoint_summary 'Get a resource error content'
  endpoint_produces 'text/plain', 'application/json'
  endpoint_response 200, 'file', 'The error content'
  endpoint_response 404, 'Error', 'If the resource is not found or has no error content'
  endpoint_response 400, 'Error', 'In case of error'
  endpoint_parameter :resource_id, Resource::MODEL_INFO[:id][:description], :path, true, Integer
  endpoint_path '/api/resource/{resource_id}/error_content'
  declare_authentication
  get %r{/api/resource/(?<resource_id>\d+)/error_content\z} do |resource_id|
    get_resource_error_content(resource_id)
  end

  endpoint_tags 'resource'
  endpoint_summary 'Get a resource html error content'
  endpoint_produces 'text/plain', 'application/json'
  endpoint_response 200, 'file', 'The html error content'
  endpoint_response 404, 'Error', 'If the resource is not found or has no html error content'
  endpoint_response 400, 'Error', 'In case of error'
  endpoint_parameter :resource_id, Resource::MODEL_INFO[:id][:description], :path, true, Integer
  endpoint_path '/api/resource/{resource_id}/html_error_content'
  declare_authentication
  get %r{/api/resource/(?<resource_id>\d+)/html_error_content\z} do |resource_id|
    get_resource_html_error_content(resource_id)
  end

  endpoint_tags 'resource'
  endpoint_summary 'Get a resource screenshot'
  endpoint_produces 'application/pdf', 'application/json'
  endpoint_response 200, 'file', 'The screenshot'
  endpoint_response 404, 'Error', 'If the resource or the content is not found'
  endpoint_response 400, 'Error', 'In case of error'
  endpoint_parameter :resource_id, Resource::MODEL_INFO[:id][:description], :path, true, Integer
  endpoint_path '/api/resource/{resource_id}/content'
  declare_authentication
  get %r{/api/resource/(?<resource_id>\d+)/screenshot\z} do |resource_id|
    get_resource_screenshot(resource_id)
  end

end
