Module: Aspera::Rest::List
- Included in:
- Plugins::Aoc::Api, Plugins::Faspex5::Api
- Defined in:
- lib/aspera/rest/list.rb
Overview
List and lookup methods for Rest::Client To be included in classes inheriting Rest::Client that require those methods.
Constant Summary collapse
- MAX_ITEMS =
max: special query parameter: max number of items for list command 'max'- MAX_PAGES =
pmax: special query parameter: max number of pages for list command 'pmax'
Class Method Summary collapse
-
.lookup_entity_generic(field:, value:, entity:) ⇒ Hash
Lookup entity by field and value.
Instance Method Summary collapse
-
#list_entities_limit_offset_total_count(entity:, operation: 'GET', items_key: nil, query: nil) ⇒ Array<(Array<Hash>, Integer)>
Get a (full or partial) list of all entities of a given type.
-
#lookup_entity_by_field(entity:, value:, field: 'name', items_key: nil, query: :default) ⇒ Object
Lookup an entity id from its name.
-
#lookup_with_q(entity, value:, field: 'name', query: {}) ⇒ Hash
Query entity by general search (read with parameter
q).
Class Method Details
.lookup_entity_generic(field:, value:, entity:) ⇒ Hash
Lookup entity by field and value. Extracts a single result from the list returned by the block.
138 139 140 141 142 143 144 145 |
# File 'lib/aspera/rest/list.rb', line 138 def lookup_entity_generic(field:, value:, entity:) Aspera.assert(block_given?, 'block required for lookup_entity_generic') found = yield Aspera.assert_array_all(found, Hash) found = found.select { |i| i[field].eql?(value) } return found.first if found.length.eql?(1) raise EntityNotFound, "#{entity} with #{field}=#{value}: #{found.empty? ? 'not found' : "found #{found.length}"}" end |
Instance Method Details
#list_entities_limit_offset_total_count(entity:, operation: 'GET', items_key: nil, query: nil) ⇒ Array<(Array<Hash>, Integer)>
Get a (full or partial) list of all entities of a given type.
Using query: offset + limit
And response total_count
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/aspera/rest/list.rb', line 60 def list_entities_limit_offset_total_count( entity:, operation: 'GET', items_key: nil, query: nil ) entity = entity.to_s if entity.is_a?(Symbol) items_key = entity.split('/').last if items_key.nil? query = {} if query.nil? Aspera.assert_type(entity, String) Aspera.assert_type(items_key, String) Aspera.assert_type(query, Hash) Log.log.debug { "list_entities t=#{entity} k=#{items_key}" } Log.dump(:query, query) result = [] offset = 0 # Special parameters are not sent to the API (the caller's query is not modified) query = query.dup max_items = query.delete(MAX_ITEMS)&.to_i remain_pages = query.delete(MAX_PAGES)&.to_i # Merge default parameters query = {'limit'=> PER_PAGE_DEFAULT}.merge(query) total_count = nil call_args = { operation: operation, subpath: entity, query: query, headers: {'Accept' => Mime::JSON} } loop do query['offset'] = offset page_result = call(**call_args) page_items = page_result[items_key] Aspera.assert_type(page_items, Array) result.concat(page_items) # Reach the limit set by user ? if !max_items.nil? && (result.length >= max_items) result = result.slice(0, max_items) break end total_count ||= page_result['total_count'] # An empty page ends the list, even if the total count is not reached (e.g. items deleted meanwhile) break if page_items.empty? # Without total count, a partial page is the last one break if total_count.nil? ? page_items.length < query['limit'].to_i : result.length >= total_count remain_pages -= 1 unless remain_pages.nil? break if !remain_pages.nil? && remain_pages <= 0 offset += page_items.length Parameters.instance.spinner_cb.call("#{result.length} / #{total_count || '?'}") end Parameters.instance.spinner_cb.call(action: :success) return result, total_count end |
#lookup_entity_by_field(entity:, value:, field: 'name', items_key: nil, query: :default) ⇒ Object
Lookup an entity id from its name.
Uses query q if query is :default and field is name.
121 122 123 124 125 126 127 |
# File 'lib/aspera/rest/list.rb', line 121 def lookup_entity_by_field(entity:, value:, field: 'name', items_key: nil, query: :default) if query.eql?(:default) Aspera.assert_values(field, ['name']) { 'Default query field' } query = {'q'=> value} end lookup_entity_generic(field: field, value: value, entity: entity) { list_entities_limit_offset_total_count(entity: entity, items_key: items_key, query: query).first } end |
#lookup_with_q(entity, value:, field: 'name', query: {}) ⇒ Hash
Query entity by general search (read with parameter q)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/aspera/rest/list.rb', line 28 def lookup_with_q(entity, value:, field: 'name', query: {}) Aspera.assert_type(query, Hash) { 'query' } Aspera.assert_type(field, String) { 'field' } # returns entities matching the query (it matches against several fields in case insensitive way) # We don't do paging, as anyway, we look for only one match matching_items = read(entity, query.merge({'q' => value})) # API style: {totalcount:, ...} cspell: disable-line: TODO: is that total_count ? # @type [Array<Hash{String => String}>] matching_items = matching_items[entity] if matching_items.is_a?(Hash) Aspera.assert_type(matching_items, Array) case matching_items.length when 1 then return matching_items.first when 0 then raise EntityNotFound, %Q{No such #{entity}: "#{value}"} else # multiple case insensitive partial matches, try case insensitive full match # (AoC does not allow 2 entities with same case insensitive name, except for some, e.g. packages) value_matches = matching_items.select { |i| i[field].casecmp?(value) } case value_matches.length when 1 then return value_matches.first when 0 then raise EntityNotFound, "#{entity}: Multiple case insensitive partial match for: \"#{value}\" in #{matching_items.map { |i| i[field] }.join(', ')} but no case insensitive full match. Please be more specific or give exact #{field}." else raise EntityNotFound, "#{entity}: Multiple entities with #{field}: \"#{value}\". Please use the identifier instead." end end end |