Commit 4d08f50a authored by Bruno Nocera Zanette's avatar Bruno Nocera Zanette

fix DefaultHandlers && fix Logout return

parent 68013e33
......@@ -25,6 +25,10 @@ require_relative "dspace/client"
module Dspace
class NotAuthorizedError < StandardError; end
class NotFoundError < StandardError; end
class MethodNotAllowedError < StandardError; end
class UnsupportedMediaTypeError < StandardError; end
class ServerError < StandardError; end
class InvalidTokenError < StandardError; end
class InvalidCredentialsError < StandardError; end
......
......@@ -52,8 +52,8 @@ module Dspace
end
def logout
resource(:authentication).logout
@access_token = nil
(response = resource(:authentication).logout) && @access_token = nil
response
end
private
......
......@@ -3,18 +3,22 @@ module Dspace
class AuthenticationResource < ResourceKit::Resource
resources do
default_handler(400) { raise InvalidTokenError, 'Invalid access token.' }
default_handler(401,403) { raise InvalidCredentialsError, 'Wrong Dspace credentials.' }
default_handler(401) { raise NotAuthorizedError, 'This request requires authentication' }
default_handler(404) { raise NotFoundError, 'The specified object doesn\'t exist' }
default_handler(405) { raise MethodNotAllowedError, 'Wrong request method (GET,POST,PUT,DELETE) or wrong data format (JSON/XML)' }
default_handler(415) { raise UnsupportedMediaTypeError, 'Missing "Content-Type: application/json" or "Content-Type: application/xml" request header' }
default_handler(500) { raise ServerError, 'Likely a SQLException, IOException, more details in the logs' }
default_handler { |response| raise StandardError, "#{response.inspect}" }
action :login, 'POST /rest/login' do
query_keys :email, :password
handler(200, 201) { |response| response['set-cookie'] }
handler(200) { |response| response['set-cookie'] }
end
action :logout, 'POST /rest/logout' do
body { |object| JSON.generate(object.to_h) }
handler(200, 201, 203, 204) { |response| true }
handler(200) { |response| true }
end
end
......
......@@ -3,7 +3,12 @@ module Dspace
class BitstreamResource < ResourceKit::Resource
resources do
default_handler(401) { raise NotAuthorizedError, 'This request requires authentication' }
default_handler(404) { raise NotFoundError, 'The specified object doesn\'t exist' }
default_handler(405) { raise MethodNotAllowedError, 'Wrong request method (GET,POST,PUT,DELETE) or wrong data format (JSON/XML)' }
default_handler(415) { raise UnsupportedMediaTypeError, 'Missing "Content-Type: application/json" or "Content-Type: application/xml" request header' }
default_handler(500) { raise ServerError, 'Likely a SQLException, IOException, more details in the logs' }
default_handler { |response| raise StandardError, "#{response.inspect}" }
action :all, 'GET /rest/bitstreams' do
......@@ -31,26 +36,26 @@ module Dspace
end
action :delete, 'DELETE /rest/bitstreams/:id' do
handler(200, 201, 204) { |response| true }
handler(200) { |response| true }
end
action :delete_policy, 'DELETE /rest/bitstreams/:id/policy/:policy_id' do
handler(200, 201, 204) { |response| true }
handler(200) { |response| true }
end
action :add_policy, 'POST /rest/bitstreams/:id/policy' do
body { |object| JSON.generate(object.to_h) }
handler(200, 201) { |response| true }
handler(200) { |response| true }
end
action :update, 'PUT /rest/bitstreams/:id' do
body { |object| JSON.generate(object.to_h) }
handler(200, 201) { |response| true }
handler(200) { |response| true }
end
action :update_data, 'PUT /rest/bitstreams/:id/data' do
body { |file| file.read }
handler(200, 201) { |response| true }
handler(200) { |response| true }
end
end
......
......@@ -3,7 +3,12 @@ module Dspace
class CollectionResource < ResourceKit::Resource
resources do
default_handler(401) { raise NotAuthorizedError, 'This request requires authentication' }
default_handler(404) { raise NotFoundError, 'The specified object doesn\'t exist' }
default_handler(405) { raise MethodNotAllowedError, 'Wrong request method (GET,POST,PUT,DELETE) or wrong data format (JSON/XML)' }
default_handler(415) { raise UnsupportedMediaTypeError, 'Missing "Content-Type: application/json" or "Content-Type: application/xml" request header' }
default_handler(500) { raise ServerError, 'Likely a SQLException, IOException, more details in the logs' }
default_handler { |response| raise StandardError, "#{response.inspect}" }
action :all, 'GET /rest/collections' do
......@@ -22,15 +27,15 @@ module Dspace
action :update, 'PUT /rest/collections/:id' do
body { |object| JSON.generate(object.to_h) }
handler(200, 201) { |response| true }
handler(200) { |response| true }
end
action :delete, 'DELETE /rest/collections/:id' do
handler(200, 201, 204) { |response| true }
handler(200) { |response| true }
end
action :delete_item, 'DELETE /rest/collections/:id/items/:item_id' do
handler(200, 201, 204) { |response| true }
handler(200) { |response| true }
end
action :items, 'GET /rest/collections/:id/items' do
......@@ -42,7 +47,7 @@ module Dspace
action :create_item, 'POST /rest/collections/:id/items' do
body { |object| JSON.generate(object.to_h) }
handler(200, 201) { |response| Dspace::Item.new(JSON.parse(response.body)) }
handler(200) { |response| Dspace::Item.new(JSON.parse(response.body)) }
end
end
......
......@@ -3,7 +3,12 @@ module Dspace
class CommunityResource < ResourceKit::Resource
resources do
default_handler(401) { raise NotAuthorizedError, 'This request requires authentication' }
default_handler(404) { raise NotFoundError, 'The specified object doesn\'t exist' }
default_handler(405) { raise MethodNotAllowedError, 'Wrong request method (GET,POST,PUT,DELETE) or wrong data format (JSON/XML)' }
default_handler(415) { raise UnsupportedMediaTypeError, 'Missing "Content-Type: application/json" or "Content-Type: application/xml" request header' }
default_handler(500) { raise ServerError, 'Likely a SQLException, IOException, more details in the logs' }
default_handler { |response| raise StandardError, "#{response.inspect}" }
action :all, 'GET /rest/communities' do
......@@ -43,34 +48,34 @@ module Dspace
action :create, 'POST /rest/communities' do
body { |object| JSON.generate(object.to_h) }
handler(200, 201) { |response| Dspace::Community.new(JSON.parse(response.body)) }
handler(200) { |response| Dspace::Community.new(JSON.parse(response.body)) }
end
action :create_subcommunity, 'POST /rest/communities/:id/communities' do
body { |object| JSON.generate(object.to_h) }
handler(200, 201) { |response| Dspace::Community.new(JSON.parse(response.body)) }
handler(200) { |response| Dspace::Community.new(JSON.parse(response.body)) }
end
action :create_collection, 'POST /rest/communities/:id/collections' do
body { |object| JSON.generate(object.to_h) }
handler(200, 201) { |response| Dspace::Collection.new(JSON.parse(response.body)) }
handler(200) { |response| Dspace::Collection.new(JSON.parse(response.body)) }
end
action :update, 'PUT /rest/communities/:id' do
body { |object| JSON.generate(object.to_h) }
handler(200, 201) { |response| true }
handler(200) { |response| true }
end
action :delete, 'DELETE /rest/communities/:id' do
handler(200, 201, 204) { |response| true }
handler(200) { |response| true }
end
action :delete_collection, 'DELETE /rest/communities/:id/collections/:collection_id' do
handler(200, 201, 204) { |response| true }
handler(200) { |response| true }
end
action :delete_subcommunity, 'DELETE /rest/communities/:id/communities/:subcommunity_id' do
handler(200, 201, 204) { |response| true }
handler(200) { |response| true }
end
end
......
......@@ -3,7 +3,12 @@ module Dspace
class ItemResource < ResourceKit::Resource
resources do
default_handler(401) { raise NotAuthorizedError, 'This request requires authentication' }
default_handler(404) { raise NotFoundError, 'The specified object doesn\'t exist' }
default_handler(405) { raise MethodNotAllowedError, 'Wrong request method (GET,POST,PUT,DELETE) or wrong data format (JSON/XML)' }
default_handler(415) { raise UnsupportedMediaTypeError, 'Missing "Content-Type: application/json" or "Content-Type: application/xml" request header' }
default_handler(500) { raise ServerError, 'Likely a SQLException, IOException, more details in the logs' }
default_handler { |response| raise StandardError, "#{response.inspect}" }
action :all, 'GET /rest/items' do
......@@ -41,20 +46,20 @@ module Dspace
end
action :delete, 'DELETE /rest/items/:id' do
handler(200, 201, 204) { |response| true }
handler(200) { |response| true }
end
action :clear_metadata, 'DELETE /rest/items/:id/metadata' do
handler(200, 201, 204) { |response| true }
handler(200) { |response| true }
end
action :delete_bitstream, 'DELETE /rest/items/:id/bitstreams/:bitstream_id' do
handler(200, 201, 204) { |response| true }
handler(200) { |response| true }
end
action :add_metadata, 'POST /rest/items/:id/metadata' do
body { |objects| Dspace::Builders::ModelBuilder.models2hash(objects) }
handler(200, 201) { |response| true }
handler(200) { |response| true }
end
action :add_bitstream, 'POST /rest/items/:id/bitstreams' do
......@@ -65,7 +70,7 @@ module Dspace
action :update_metadata, 'PUT /rest/items/:id/metadata' do
body { |object| JSON.generate(Dspace::Builders::ModelBuilder.models2hash(object))}
handler(200, 201) { |response| true }
handler(200) { |response| true }
end
end
......
......@@ -3,10 +3,16 @@ module Dspace
class StatusResource < ResourceKit::Resource
resources do
default_handler(401) { raise NotAuthorizedError, 'This request requires authentication' }
default_handler(404) { raise NotFoundError, 'The specified object doesn\'t exist' }
default_handler(405) { raise MethodNotAllowedError, 'Wrong request method (GET,POST,PUT,DELETE) or wrong data format (JSON/XML)' }
default_handler(415) { raise UnsupportedMediaTypeError, 'Missing "Content-Type: application/json" or "Content-Type: application/xml" request header' }
default_handler(500) { raise ServerError, 'Likely a SQLException, IOException, more details in the logs' }
default_handler { |response| raise StandardError, "#{response.inspect}" }
action :test, 'GET /rest/test' do
handler(200, 201) { |response| true }
handler(200) { |response| true }
end
action :status, 'GET /rest/status' do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment