Unverified Commit 6106759a authored by Aluísio Augusto Silva Gonçalves's avatar Aluísio Augusto Silva Gonçalves
Browse files

Revert "Revert "Merge branch 'add-report-endpoints' into 'master'

This reverts commit 99106446, thus
approaching this codebase to what we currently have.
parent c8dd3688
Pipeline #12577 passed with stage
in 33 seconds
...@@ -10,4 +10,4 @@ gem 'activesupport' ...@@ -10,4 +10,4 @@ gem 'activesupport'
group :development, :spec do group :development, :spec do
gem 'minitest', '~> 5.8.3' gem 'minitest', '~> 5.8.3'
gem 'rspec' gem 'rspec'
end end
\ No newline at end of file
...@@ -16,6 +16,63 @@ Or install it yourself as: ...@@ -16,6 +16,63 @@ Or install it yourself as:
$ gem install dspace_rest_client $ gem install dspace_rest_client
## Resources
### Item's resources
* action :all, 'GET /rest/items' (query_keys :expand, :limit, :offset)
* action :find, 'GET /rest/items/:id' (query_keys :expand)
* action :find_by_metadata, 'POST /rest/items/find-by-metadata-field'
* action :metadata, 'GET /rest/items/:id/metadata'
* action :bitstreams, 'GET /rest/items/:id/bitstreams' (query_keys :expand, :limit, :offset)
* action :delete, 'DELETE /rest/items/:id'
* action :clear_metadata, 'DELETE /rest/items/:id/metadata'
* action :delete_bitstream, 'DELETE /rest/items/:id/bitstreams/:bitstream_id'
* action :add_metadata, 'POST /rest/items/:id/metadata'
* action :add_bitstream, 'POST /rest/items/:id/bitstreams' (query_keys :name, :description, :bundle_name)
* action :update_metadata, 'PUT /rest/items/:id/metadata'
### Collection's resources
* action :all, 'GET /rest/collections' (query_keys :expand, :limit, :offset)
* action :find, 'GET /rest/collections/:id' (query_keys :expand)
* action :update, 'PUT /rest/collections/:id'
* action :delete, 'DELETE /rest/collections/:id'
* action :delete_item, 'DELETE /rest/collections/:id/items/:item_id'
* action :items, 'GET /rest/collections/:id/items' (query_keys :expand, :limit, :offset)
* action :create_item, 'POST /rest/collections/:id/items'
### Community's resources
* action :all, 'GET /rest/communities' (query_keys :expand, :limit, :offset)
* action :top_communities, 'GET /rest/communities/top-communities' (query_keys :expand, :limit, :offset)
* action :find, 'GET /rest/communities/:id' (query_keys :expand)
* action :collections, 'GET /rest/communities/:id/collections' (query_keys :expand, :limit, :offset)
* action :sub_communities, 'GET /rest/communities/:id/communities' (query_keys :expand, :limit, :offset)
* action :create, 'POST /rest/communities'
* action :create_subcommunity, 'POST /rest/communities/:id/communities'
* action :create_collection, 'POST /rest/communities/:id/collections'
* action :update, 'PUT /rest/communities/:id'
* action :delete, 'DELETE /rest/communities/:id'
* action :delete_collection, 'DELETE /rest/communities/:id/collections/:collection_id'
* action :delete_subcommunity, 'DELETE /rest/communities/:id/communities/:subcommunity_id'
### Bitstream's resources
* action :all, 'GET /rest/bitstreams' (query_keys :expand, :limit, :offset)
* action :find, 'GET /rest/bitstreams/:id' (query_keys :expand)
* action :policy, 'GET /rest/bitstreams/:id/policy'
* action :retrieve, 'GET /rest/bitstreams/:id/retrieve'
* action :delete, 'DELETE /rest/bitstreams/:id'
* action :delete_policy, 'DELETE /rest/bitstreams/:id/policy/:policy_id'
* action :add_policy, 'POST /rest/bitstreams/:id/policy'
* action :update, 'PUT /rest/bitstreams/:id'}
* action :update_data, 'PUT /rest/bitstreams/:id/data' do
### Authentication's resources
* action :login, 'POST /rest/login'
* action :logout, 'POST /rest/logout'
### Status's resources
* action :test, 'GET /rest/test'
* action :status, 'GET /rest/status'
## Get started ## Get started
Some requests requires user authentication. Some requests requires user authentication.
...@@ -55,4 +112,4 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run ...@@ -55,4 +112,4 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
## Contributing ## Contributing
Bug reports and pull requests are welcome. This project is intended to be a safe, welcoming space for collaboration. Bug reports and pull requests are welcome. This project is intended to be a safe, welcoming space for collaboration.
\ No newline at end of file
...@@ -19,7 +19,7 @@ Gem::Specification.new do |spec| ...@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib'] spec.require_paths = ['lib']
spec.add_runtime_dependency 'json', '~> 1.8', '>= 1.8.3' spec.add_runtime_dependency 'json', '~> 1.8', '>= 1.8.3'
spec.add_runtime_dependency 'resource_kit', '>= 0.1.4' spec.add_runtime_dependency 'resource_kit', '~> 0.1.4'
spec.add_runtime_dependency 'faraday', '~> 0.9.2' spec.add_runtime_dependency 'faraday', '~> 0.9.2'
spec.add_runtime_dependency 'net-http-persistent', '~> 2.9', '>= 2.9.4' spec.add_runtime_dependency 'net-http-persistent', '~> 2.9', '>= 2.9.4'
spec.add_runtime_dependency 'activesupport', '>= 4.2.0' spec.add_runtime_dependency 'activesupport', '>= 4.2.0'
......
...@@ -5,6 +5,9 @@ require "faraday" ...@@ -5,6 +5,9 @@ require "faraday"
require "active_support/all" require "active_support/all"
require_relative "dspace/version" require_relative "dspace/version"
require_relative "dspace/resources/report_resource"
require_relative "dspace/resources/hierarchy_resource"
require_relative "dspace/resources/schema_registry_resource"
require_relative "dspace/resources/community_resource" require_relative "dspace/resources/community_resource"
require_relative "dspace/resources/collection_resource" require_relative "dspace/resources/collection_resource"
require_relative "dspace/resources/authentication_resource" require_relative "dspace/resources/authentication_resource"
...@@ -14,17 +17,26 @@ require_relative "dspace/resources/item_resource" ...@@ -14,17 +17,26 @@ require_relative "dspace/resources/item_resource"
require_relative "dspace/builders/hash_builder" require_relative "dspace/builders/hash_builder"
require_relative "dspace/builders/model_builder" require_relative "dspace/builders/model_builder"
require_relative "dspace/builders/tempfile_builder" require_relative "dspace/builders/tempfile_builder"
require_relative "dspace/site"
require_relative "dspace/community" require_relative "dspace/community"
require_relative "dspace/collection" require_relative "dspace/collection"
require_relative "dspace/item" require_relative "dspace/item"
require_relative "dspace/metadata" require_relative "dspace/metadata"
require_relative "dspace/schema"
require_relative "dspace/metadata_field"
require_relative "dspace/bitstream" require_relative "dspace/bitstream"
require_relative "dspace/policy" require_relative "dspace/policy"
require_relative "dspace/item_filter"
require_relative "dspace/report"
require_relative "dspace/client" require_relative "dspace/client"
module Dspace module Dspace
class NotAuthorizedError < StandardError; end 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 InvalidTokenError < StandardError; end
class InvalidCredentialsError < StandardError; end class InvalidCredentialsError < StandardError; end
......
...@@ -9,12 +9,12 @@ module Dspace ...@@ -9,12 +9,12 @@ module Dspace
:parent_object, :retrieve_link, :check_sum, :parent_object, :retrieve_link, :check_sum,
:sequence_id, :policies :sequence_id, :policies
def initialize args def initialize(args={})
@id = args['id'] @id = args['id'] || args['uuid']
@name = args['name'] @name = args['name']
@type = args['type'] @type = args['type']
@link = args['link'] @link = args['link']
@bundle_name = args['bundleName'] @bundle_name = args['bundleName'] || args['bundle_name']
@description = args['description'] @description = args['description']
@format = args['format'] @format = args['format']
@mime_type = args['mimeType'] @mime_type = args['mimeType']
...@@ -29,7 +29,7 @@ module Dspace ...@@ -29,7 +29,7 @@ module Dspace
def to_h def to_h
{ {
id: @id, uuid: @id,
name: @name, name: @name,
type: @type, type: @type,
link: @link, link: @link,
......
...@@ -29,6 +29,24 @@ module Dspace ...@@ -29,6 +29,24 @@ module Dspace
colls colls
end end
def self.build_schemas(schemas=[])
return schemas unless schemas.is_a? Array
colls = []
schemas.each do |c|
colls << Dspace::Schema.new(c)
end
colls
end
def self.build_fields(fields=[])
return fields unless fields.is_a? Array
colls = []
fields.each do |c|
colls << Dspace::MetadataField.new(c)
end
colls
end
def self.build_bitstreams(bitstreams=[]) def self.build_bitstreams(bitstreams=[])
return bitstreams unless bitstreams.is_a? Array return bitstreams unless bitstreams.is_a? Array
colls = [] colls = []
...@@ -56,6 +74,24 @@ module Dspace ...@@ -56,6 +74,24 @@ module Dspace
colls colls
end end
def self.build_reports(reports=[])
return reports unless reports.is_a? Array
colls = []
reports.each do |c|
colls << Dspace::Report.new(c)
end
colls
end
def self.build_item_filters(item_filters=[])
return item_filters unless item_filters.is_a? Array
colls = []
item_filters.each do |c|
colls << Dspace::ItemFilter.new(c)
end
colls
end
def self.models2hash(list) def self.models2hash(list)
if list.is_a? Array if list.is_a? Array
list.map do |m| list.map do |m|
......
...@@ -2,7 +2,7 @@ module Dspace ...@@ -2,7 +2,7 @@ module Dspace
class Client class Client
DSPACE_API = 'https://demo.dspace.org' DSPACE_API = 'https://demo.dspace.org'
attr_reader :access_token attr_accessor :access_token
def initialize(options = {}) def initialize(options = {})
@access_token = options.with_indifferent_access[:access_token] @access_token = options.with_indifferent_access[:access_token]
...@@ -26,7 +26,10 @@ module Dspace ...@@ -26,7 +26,10 @@ module Dspace
collections: ::Dspace::Resources::CollectionResource, collections: ::Dspace::Resources::CollectionResource,
communities: ::Dspace::Resources::CommunityResource, communities: ::Dspace::Resources::CommunityResource,
status: ::Dspace::Resources::StatusResource, status: ::Dspace::Resources::StatusResource,
authentication: ::Dspace::Resources::AuthenticationResource authentication: ::Dspace::Resources::AuthenticationResource,
schema_registry: ::Dspace::Resources::SchemaRegistryResource,
hierarchy: ::Dspace::Resources::HierarchyResource,
report: ::Dspace::Resources::ReportResource
} }
end end
...@@ -42,36 +45,39 @@ module Dspace ...@@ -42,36 +45,39 @@ module Dspace
resource(:status).test resource(:status).test
end end
def status
resource(:status).status
end
def login(email, password) def login(email, password)
@access_token = resource(:authentication).login(email, password) @access_token = resource(:authentication).login(email: email, password: password)
end end
def logout def logout
resource(:authentication).logout (response = resource(:authentication).logout) && @access_token = nil
@access_token = nil response
end end
private private
def resource(name) def resource(name)
if self.class.resources.keys.include?(name) if self.class.resources.keys.include?(name)
resources[name] ||= self.class.resources[name].new(connection: connection) resources[name] = self.class.resources[name].new(connection: connection)
resources[name]
end end
end end
def connection_options def connection_options
{ {
url: @dspace_api || DSPACE_API, url: @dspace_api || DSPACE_API,
ssl: { ssl: {
verify: false verify: false
}, },
headers: { headers: {
content_type: 'application/json', content_type: 'application/json',
accept: 'application/json', accept: 'application/json',
'rest-dspace-token' => access_token.to_s, user_agent: "dspace-rest-client #{Dspace::VERSION}",
user_agent: "dspace-rest-client #{Dspace::VERSION}" cookie: @access_token.to_s
} }
} }
end end
end end
......
...@@ -9,18 +9,18 @@ module Dspace ...@@ -9,18 +9,18 @@ module Dspace
:parent_community_list, :items, :parent_community_list, :items,
:number_items, :expand :number_items, :expand
def initialize args def initialize(args={})
@id = args['id'] @id = args['id'] || args['uuid']
@name = args['name'] @name = args['name']
@handle = args['handle'] @handle = args['handle']
@type = args['type'] @type = args['type']
@link = args['link'] @link = args['link']
@logo = args['logo'] @logo = Dspace::Bitstream.new(args['logo']) unless args['logo'].nil?
@license = args['license'] @license = args['license']
@copyright_text = args['copyrightText'] @copyright_text = args['copyrightText'] || args['copyright_text']
@introductory_text = args['introductoryText'] @introductory_text = args['introductoryText'] || args['introductory_text']
@short_description = args['shortDescription'] @short_description = args['shortDescription'] || args['short_description']
@sidebar_text = args['sidebarText'] @sidebar_text = args['sidebarText'] || args['sidebar_text']
@number_items = args['numberItems'] @number_items = args['numberItems']
@expand = args['expand'] @expand = args['expand']
...@@ -31,14 +31,14 @@ module Dspace ...@@ -31,14 +31,14 @@ module Dspace
def to_h def to_h
{ {
id: @id, uuid: @id,
name: @name, name: @name,
handle: @handle, handle: @handle,
type: @type, type: @type,
link: @link, link: @link,
logo: @logo, logo: @logo,
parentCommunity: @parent_community, parentCommunity: @parent_community,
parentCommunitList: @parent_community_list, parentCommunityList: @parent_community_list,
items: obj2hash(@items), items: obj2hash(@items),
license: @license, license: @license,
copyrightText: @copyright_text, copyrightText: @copyright_text,
...@@ -51,4 +51,4 @@ module Dspace ...@@ -51,4 +51,4 @@ module Dspace
end end
end end
end end
\ No newline at end of file
...@@ -8,26 +8,35 @@ module Dspace ...@@ -8,26 +8,35 @@ module Dspace
:count_items, :sub_communities, :collections, :expand :count_items, :sub_communities, :collections, :expand
def initialize(args={}) def initialize(args={})
@id = args['id'] @id = args['id'] || args['uuid']
@name = args['name'] @name = args['name']
@handle = args['handle'] @handle = args['handle']
@type = args['type'] @type = args['type']
@link = args['link'] @link = args['link']
@logo = args['logo'] @logo = Dspace::Bitstream.new(args['logo']) unless args['logo'].nil?
@parent_community = Dspace::Community.new(args['parentCommunity']) unless args['parentCommunity'].nil? @parent_community = Dspace::Community.new(args['parentCommunity']) unless args['parentCommunity'].nil?
@copyright_text = args['copyrightText'] @copyright_text = args['copyrightText'] || args['copyright_text']
@introductory_text = args['introductoryText'] @introductory_text = args['introductoryText'] || args['introductory_text']
@short_description = args['shortDescription'] @short_description = args['shortDescription'] || args['short_description']
@sidebar_text = args['sidebarText'] @sidebar_text = args['sidebarText'] || args['sidebar_text']
@count_items = args['countItems'] @count_items = args['countItems']
@sub_communities = Dspace::Builders::ModelBuilder.build_communities(args['subcommunities']) unless args['subcommunities'].nil?
@collections = Dspace::Builders::ModelBuilder.build_collections(args['collections']) unless args['collections'].nil? # subcommunities = All community endpoints
# community = Hierarchy endpoint
sub_communities = args['subcommunities'] || args['community']
@sub_communities = Dspace::Builders::ModelBuilder.build_communities(sub_communities) unless sub_communities.nil?
# collections = All community/collection endpoints
# collection = Hierarchy endpoint
collections = args['collections'] || args['collection']
@collections = Dspace::Builders::ModelBuilder.build_collections(collections) unless collections.nil?
@expand = args['expand'] @expand = args['expand']
end end
def to_h def to_h
{ {
id: @id, uuid: @id,
name: @name, name: @name,
handle: @handle, handle: @handle,
type: @type, type: @type,
...@@ -48,7 +57,7 @@ module Dspace ...@@ -48,7 +57,7 @@ module Dspace
private private
def obj2hash(list) def obj2hash(list)
Dspace::Builders::HashBuilder.models2hash list if list.is_a? Array Dspace::Builders::ModelBuilder.models2hash list if list.is_a? Array
end end
end end
......
...@@ -8,8 +8,8 @@ module Dspace ...@@ -8,8 +8,8 @@ module Dspace
:parent_collection_list, :parent_community_list, :bit_streams, :parent_collection_list, :parent_community_list, :bit_streams,
:expand, :metadata :expand, :metadata
def initialize args def initialize(args={})
@id = args['id'] @id = args['id'] || args['uuid']
@name = args['name'] @name = args['name']
@handle = args['handle'] @handle = args['handle']
@type = args['type'] @type = args['type']
...@@ -22,12 +22,12 @@ module Dspace ...@@ -22,12 +22,12 @@ module Dspace
@archived = args['archived'] @archived = args['archived']
@withdrawn = args['withdrawn'] @withdrawn = args['withdrawn']
@expand = args['expand'] @expand = args['expand']
@metadata = Dspace::Builders::ModelBuilder.build_metadatas(args['metadata']) @metadata = Dspace::Builders::ModelBuilder.build_metadatas(args['metadata']) || []
end end
def to_h def to_h
{ {
id: @id, uuid: @id,
name: @name, name: @name,
handle: @handle, handle: @handle,
type: @type, type: @type,
......
module Dspace
class ItemFilter
attr_reader :category, :filter_name, :title
def initialize(args={})
@category = args['category']
@filter_name = args['filter-name']
@title = args['title']
end
def to_h
{category: @category, 'filter-name': @filter_name, title: @title}
end
end
end
...@@ -2,7 +2,7 @@ module Dspace ...@@ -2,7 +2,7 @@ module Dspace
class Metadata class Metadata
attr_accessor :key, :value, :language attr_accessor :key, :value, :language
def initialize args def initialize(args={})
@key = args['key'] @key = args['key']
@value = args['value'] @value = args['value']
@language = args['language'] || nil @language = args['language'] || nil
......
module Dspace
class MetadataField
include Dspace::Builders::HashBuilder
attr_accessor :name, :element, :qualifier, :description
attr_reader :id, :parent_schema, :expand
def initialize(args={})
@id = args['fieldId']
@name = args['name']
@parent_schema = args['parentSchema'] || args['parent_schema']
@element = args['element']
@qualifier = args['qualifier']
@description = args['description']
@expand = args['expand']
end
def to_h
{
fieldId: @id,
name: @name,
parentSchema: @parent_schema,
element: @element,
qualifier: @qualifier,
description: @description,
expand: @expand
}
end
end
end
module Dspace
class Report
attr_reader :nickname, :url
def initialize(args={})
@nickname = args['nickname']
@url = args['url']
end