schema_registry_resource.rb 4.46 KB
Newer Older
1
2
3
4
5
6
module Dspace
  module Resources
    class SchemaRegistryResource < ResourceKit::Resource

      resources do

7
8
        default_handler(400) { raise InvalidTokenError, 'Invalid access token.' }
        default_handler(403) { raise InvalidCredentialsError, 'Wrong Dspace credentials.' }
9
10
11
12
13
        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' }
14
15
        default_handler { |response| raise StandardError, "#{response.inspect}" }

16
17
18
19
        # GET /registries/schema
        # => Return the list of metadata schemas in the registry
        action :all,
            'GET /rest/registries/schema' do
20
21
          query_keys :expand
          handler(200) do |response|
22
            Dspace::Builders::ModelBuilder.build_schemas(JSON.parse(response.body))
23
24
25
          end
        end

26
27
28
29
        # GET /registries/schema/{schema_prefix}
        # => Returns the specified metadata schema
        action :find_schema,
            'GET /rest/registries/schema/:schema_prefix' do
30
31
          query_keys :expand
          handler(200) do |response|
Bruno Nocera Zanette's avatar
Bruno Nocera Zanette committed
32
            Dspace::Schema.new(JSON.parse(response.body))
33
34
35
          end
        end

36
37
38
39
40
        # POST /registries/schema/
        # => Add a schema to the schema registry
        action :create_schema,
            'POST /rest/registries/schema' do
          body { |object| JSON.generate(object.to_h) }
41
          handler(200) do |response|
42
            Dspace::Schema.new(JSON.parse(response.body))
43
          end
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
        end

        # DELETE /registries/schema/{schema_id}
        # => Delete the specified schema from the schema registry
        action :delete_schema,
            'DELETE /rest/registries/schema/:schema_id' do
          handler(200) { |response| true }
        end

        # GET /registries/metadata-fields/{field_id}
        # => Return the specified metadata field
        action :find_metadata_field,
            'GET /rest/registries/metadata-fields/:field_id' do
          query_keys :expand
          handler(200) do |response|
59
            Dspace::MetadataField.new(JSON.parse(response.body))
60
61
62
63
64
65
66
67
68
          end
        end

        # POST /registries/schema/{schema_prefix}/metadata-fields
        # => Add a metadata field to the specified schema
        action :create_metadata_field,
            'POST /rest/registries/schema/:schema_prefix/metadata-fields' do
          body { |object| JSON.generate(object.to_h) }
          handler(200) do |response|
69
            Dspace::MetadataField.new(JSON.parse(response.body))
70
71
72
73
74
75
          end
        end

        # PUT /registries/metadata-fields/{field_id}
        # => Update the specified metadata field
        action :update_metadata_field,
76
77
78
            'PUT /rest/registries/metadata-fields/:field_id' do
          body { |object| JSON.generate(object.to_h) }
          handler(200) { |response| true }
79
80
81
82
83
84
85
86
87
88
89
90
91
        end

        # DELETE /registries/metadata-fields/{field_id}
        # => Delete the specified metadata field from the metadata field registry
        action :delete_metadata_field,
            'DELETE /rest/registries/metadata-fields/:field_id' do
          handler(200) { |response| true }
        end

        # GET /registries/schema/{schema_prefix}/metadata-fields/{element}
        # => Returns the metadata field within a schema with an unqualified element name
        action :find_unqualified_element,
            'GET /rest/registries/schema/:schema_prefix/metadata-fields/:element' do
92
          query_keys :expand
93
          handler(200) do |response|
94
            Dspace::MetadataField.new(JSON.parse(response.body))
95
96
97
98
99
100
101
          end
        end

        # GET /registries/schema/{schema_prefix}/metadata-fields/{element}/{qualifier}
        # => Returns the metadata field within a schema with a qualified element name
        action :find_qualified_element,
            'GET /rest/registries/schema/:schema_prefix/metadata-fields/:element/:qualifier' do
102
          handler(200) do |response|
103
            Dspace::MetadataField.new(JSON.parse(response.body))
104
105
106
107
108
109
110
111
          end
        end

      end

    end
  end
end