Class: Aspera::Api::AoC
- Includes:
- RestList
- Defined in:
- lib/aspera/api/aoc.rb
Overview
Aspera on Cloud API client
Defined Under Namespace
Modules: AppType, Scope Classes: AppInfo
Constant Summary collapse
- PRODUCT_NAME =
'Aspera on Cloud'- DEFAULT_WORKSPACE =
‘”` Use default workspace if it is set, else none
''- SAAS_DOMAIN_PROD =
Production domain of AoC
'ibmaspera.com'- API_V1 =
'api/v1'
Constants included from RestList
RestList::MAX_ITEMS, RestList::MAX_PAGES
Instance Attribute Summary collapse
-
#private_link ⇒ Object
readonly
Returns the value of attribute private_link.
-
#ws_ids ⇒ Object
Returns the value of attribute ws_ids.
Attributes inherited from Rest
#auth_params, #base_url, #headers
Class Method Summary collapse
-
.api_base_url(api_domain: SAAS_DOMAIN_PROD) ⇒ Object
base API url depends on domain, which could be “qa.xxx” or self-managed domain.
-
.call_paging(query: {}) ⇒ Hash
Call ‘block` with same query using paging and response information.
-
.expand_access_levels(levels) ⇒ Array
Expand access levels to full list of levels.
-
.get_client_info(client_name = nil) ⇒ Object
strings /Applications/Aspera\ Drive.app/Contents/MacOS/AsperaDrive|grep -E ‘.100==$’|base64 –decode.
-
.link_info(url) ⇒ Hash{Symbol => String, Hash}
Get information from link.
- .saas_url?(url) ⇒ Boolean
-
.split_org_domain(uri) ⇒ Object
split host of URL into organization and domain.
-
.workspace_access(id) ⇒ Hash
Suitable for permission filtering.
-
.workspace_access?(permission) ⇒ Boolean
‘true` if internal access.
Instance Method Summary collapse
-
#add_ts_tags(transfer_spec:, app_info:) ⇒ Object
Add transfer spec callback in Node (transfer_spec_gen4).
- #additional_persistence_ids ⇒ Object
- #assert_public_link_types(expected) ⇒ Object
-
#create_package_simple(package_data, validate_meta, new_user_option) ⇒ Object
create a package.
-
#current_user_info(exception: false) ⇒ Object
Cached user information.
- #default_workspace? ⇒ Boolean
-
#home ⇒ Object
Cached Home information for current user in Files app.
-
#initialize(scope: nil, subpath: API_V1, secret_finder: nil, url:, auth:, client_id: nil, client_secret: nil, redirect_uri: nil, private_key: nil, passphrase: nil, username: nil, password: nil) ⇒ AoC
constructor
By default: no workspace.
-
#node_api_from(node_id:, workspace_id: nil, workspace_name: nil, scope: Node::Scope::USER, package_info: nil) ⇒ Object
Return a Node API for given node id, in a given context (files, packages), for the given scope.
-
#permissions_send_event(event_data:, app_info:, types: PERMISSIONS_CREATED) ⇒ Object
Callback from Plugins::Node send shared folder event to AoC.
-
#permissions_set_create_params(perm_data:, app_info:) ⇒ Object
Callback from Plugins::Node add application specific tags to permissions creation.
-
#public_link ⇒ Hash?
Cached public link information.
-
#read_with_paging(subpath, query = nil) ⇒ Hash
Read using the query and paging.
-
#resolve_package_recipients(package_data, rcpt_lst_field, new_user_option) ⇒ Object
Normalize package creation recipient lists as expected by AoC API AoC expects {type: , id: }, but ascli allows providing either the native values or just a name in that case, the name is resolved and replaced with {type: , id: }.
-
#update_package_metadata_for_api(pkg_data) ⇒ Object
CLI allows simplified format for metadata: transform if necessary for API.
-
#validate_metadata(pkg_data) ⇒ Object
Check metadata: remove when validation is done server side.
-
#workspace_info ⇒ Hash{Symbol=>String}
Cached workspace information.
Methods included from RestList
#list_entities_limit_offset_total_count, #lookup_entity_by_field, lookup_entity_generic, #lookup_with_q
Methods inherited from Rest
basic_authorization, build_uri, #call, #cancel, #create, #delete, h_to_query_array, io_http_session, #oauth, #params, parse_header, parse_link_header, php_style, query_to_h, #read, remote_certificate_chain, start_http_session, #update
Constructor Details
#initialize(scope: nil, subpath: API_V1, secret_finder: nil, url:, auth:, client_id: nil, client_secret: nil, redirect_uri: nil, private_key: nil, passphrase: nil, username: nil, password: nil) ⇒ AoC
By default: no workspace
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/aspera/api/aoc.rb', line 270 def initialize( scope: nil, subpath: API_V1, secret_finder: nil, # below: OAuth::AUTH_OPTIONS url:, auth:, client_id: nil, client_secret: nil, redirect_uri: nil, private_key: nil, passphrase: nil, username: nil, password: nil ) # Test here because link may set url Aspera.assert(url, 'Missing mandatory option: url', type: ParameterError) Aspera.assert(scope, 'Missing mandatory option: scope', type: ParameterError) # default values for client id client_id, client_secret = self.class.get_client_info if client_id.nil? # access key secrets are provided out of band to get node api access # key: access key # value: associated secret @secret_finder = secret_finder @cache_user_info = nil @cache_url_token_info = nil # @type [Hash] @workspace_info = nil # Used only for init: provided by user @ws_ids = {id: nil, name: nil} @home_info = nil auth_params = { type: :oauth2, params: { client_id: client_id, client_secret: client_secret, scope: scope } } # analyze type of url url_info = AoC.link_info(url) Log.dump(:url_info, url_info) @private_link = url_info[:private_link] auth_params[:grant_method] = if url_info.key?(:token) :url_json else Aspera.assert(auth, 'Missing mandatory option: auth', type: ParameterError) auth end # this is the base API url api_url_base = self.class.api_base_url(api_domain: url_info[:instance_domain]) # auth URL auth_params[:base_url] = "#{api_url_base}/#{OAUTH_API_SUBPATH}/#{url_info[:organization]}" # fill other auth parameters based on OAuth method case auth_params[:grant_method] when :web Aspera.assert(redirect_uri, 'Missing mandatory option: redirect_uri', type: ParameterError) auth_params[:redirect_uri] = redirect_uri when :jwt Aspera.assert(private_key, 'Missing mandatory option: private_key', type: ParameterError) Aspera.assert(username, 'Missing mandatory option: username', type: ParameterError) auth_params[:private_key_obj] = OpenSSL::PKey::RSA.new(private_key, passphrase) auth_params[:payload] = { iss: client_id, # issuer sub: username, # subject aud: JWT_AUDIENCE } # add jwt payload for global client id auth_params[:payload][:org] = url_info[:organization] if GLOBAL_CLIENT_APPS.include?(client_id) auth_params[:cache_ids] = [url_info[:organization]] when :url_json auth_params[:url] = {grant_type: 'url_token'} # Query arguments auth_params[:json] = {url_token: url_info[:token]} # JSON body # password protection of link auth_params[:json][:password] = password unless password.nil? # basic auth required for /token auth_params[:auth] = {type: :basic, username: client_id, password: client_secret} else Aspera.error_unexpected_value(auth_params[:grant_method]){'auth, use one of: :web, :jwt'} end super( base_url: "#{api_url_base}/#{subpath}", auth: auth_params ) end |
Instance Attribute Details
#private_link ⇒ Object (readonly)
Returns the value of attribute private_link.
266 267 268 |
# File 'lib/aspera/api/aoc.rb', line 266 def private_link @private_link end |
#ws_ids ⇒ Object
Returns the value of attribute ws_ids.
267 268 269 |
# File 'lib/aspera/api/aoc.rb', line 267 def ws_ids @ws_ids end |
Class Method Details
.api_base_url(api_domain: SAAS_DOMAIN_PROD) ⇒ Object
base API url depends on domain, which could be “qa.xxx” or self-managed domain
117 118 119 |
# File 'lib/aspera/api/aoc.rb', line 117 def api_base_url(api_domain: SAAS_DOMAIN_PROD) return "https://api.#{api_domain}" end |
.call_paging(query: {}) ⇒ Hash
Call ‘block` with same query using paging and response information. Block must return a 2 element `Array` with data and http response
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/aspera/api/aoc.rb', line 198 def call_paging(query: {}) Aspera.assert_type(query, Hash){'query'} Aspera.assert(block_given?) # set default large page if user does not specify own parameters. AoC Caps to 1000 anyway query['per_page'] = 1000 unless query.key?('per_page') max_items = query.delete(RestList::MAX_ITEMS) max_pages = query.delete(RestList::MAX_PAGES) item_list = [] total_count = nil current_page = query['page'] current_page = 1 if current_page.nil? page_count = 0 loop do new_query = query.clone new_query['page'] = current_page result_data, result_http = yield(new_query) Aspera.assert(result_http) total_count = result_http[HEADER_X_TOTAL_COUNT]&.to_i page_count += 1 current_page += 1 add_items = result_data break if add_items.nil? break if add_items.empty? # append new items to full list item_list += add_items break if !max_items.nil? && item_list.count >= max_items break if !max_pages.nil? && page_count >= max_pages break if total_count&.<=(item_list.count) RestParameters.instance.spinner_cb.call("#{item_list.count} / #{total_count}") unless total_count.eql?(item_list.count.to_s) end RestParameters.instance.spinner_cb.call(action: :success) item_list = item_list[0..max_items - 1] if !max_items.nil? && item_list.count > max_items return {items: item_list, total: total_count} end |
.expand_access_levels(levels) ⇒ Array
Expand access levels to full list of levels.
251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/aspera/api/aoc.rb', line 251 def (levels) case levels when nil, 'edit' then Node::ACCESS_LEVELS when 'preview' then %w[list preview] when 'download' then %w[list preview read] when 'upload' then %w[mkdir write] when Array Aspera.assert_array_all(levels, String){'access_levels'} levels.each{ |level| Aspera.assert_values(level, Node::ACCESS_LEVELS){'access_level'}} levels else Aspera.error_unexpected_value(levels){"access_levels must be a list of #{Node::ACCESS_LEVELS.join(', ')} or one of edit, preview, download, upload"} end end |
.get_client_info(client_name = nil) ⇒ Object
strings /Applications/Aspera\ Drive.app/Contents/MacOS/AsperaDrive|grep -E ‘.100==$’|base64 –decode
111 112 113 114 |
# File 'lib/aspera/api/aoc.rb', line 111 def get_client_info(client_name = nil) client_key = client_name.nil? ? GLOBAL_CLIENT_APPS.first : client_name.to_sym return client_key, DataRepository.instance.item(client_key) end |
.link_info(url) ⇒ Hash{Symbol => String, Hash}
Get information from link
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/aspera/api/aoc.rb', line 140 def link_info(url) final_uri = Rest.new(base_url: url, redirect_max: MAX_AOC_URL_REDIRECT).call(operation: 'GET', ret: :resp).uri Log.dump(:final_uri, final_uri, level: :trace1) org_domain = split_org_domain(final_uri) if (m = final_uri.path.match(%r{/oauth2/([^/]+)/login$})) org_domain[:organization] = m[1] if org_domain[:organization].nil? else Log.log.debug{"path=#{final_uri.path} does not end with /login"} end Aspera.assert(!final_uri.query.nil?, 'AoC shall redirect to login page with a query', type: Error) query = Rest.query_to_h(final_uri.query) Log.dump(:query, query, level: :trace1) # is that a public link ? if query.key?('token') Log.log.warn{"Unknown pub link path: #{final_uri.path}"} unless PUBLIC_LINK_PATHS.include?(final_uri.path) # ok we get it ! return { instance_domain: org_domain[:domain], url: "https://#{final_uri.host}", token: query['token'] } end if query.key?('state') # can be a private link state_uri = URI.parse(query['state']) if state_uri.query && query['redirect_uri'] decoded_state = Rest.query_to_h(state_uri.query) if decoded_state.key?('short_link_url') if (m = state_uri.path.match(%r{/files/workspaces/([0-9]+)/all/([0-9]+):([0-9]+)})) redirect_uri = URI.parse(query['redirect_uri']) org_domain = split_org_domain(redirect_uri) return { instance_domain: org_domain[:domain], organization: org_domain[:organization], url: "https://#{redirect_uri.host}", private_link: { workspace_id: m[1], node_id: m[2], file_id: m[3] } } end end end end Log.dump(:org_domain, org_domain) return { instance_domain: org_domain[:domain], organization: org_domain[:organization] } end |
.saas_url?(url) ⇒ Boolean
131 132 133 134 135 |
# File 'lib/aspera/api/aoc.rb', line 131 def saas_url?(url) URI.parse(url).host&.end_with?(".#{SAAS_DOMAIN_PROD}") rescue URI::InvalidURIError false end |
.split_org_domain(uri) ⇒ Object
split host of URL into organization and domain
122 123 124 125 126 127 128 129 |
# File 'lib/aspera/api/aoc.rb', line 122 def split_org_domain(uri) Aspera.assert_type(uri, URI) Aspera.assert(!uri.host.nil?){"No host found in URL. Please check URL format: https://myorg.#{SAAS_DOMAIN_PROD}"} parts = uri.host.split('.', 2) Aspera.assert(parts.length == 2){"expecting a public FQDN for #{PRODUCT_NAME}"} parts[0] = nil if parts[0].eql?('api') return %i{organization domain}.zip(parts).to_h end |
.workspace_access(id) ⇒ Hash
Returns suitable for permission filtering.
235 236 237 238 239 240 |
# File 'lib/aspera/api/aoc.rb', line 235 def workspace_access(id) { 'access_type' => 'user', 'access_id' => "#{ID_AK_ADMIN}_WS_#{id}" } end |
.workspace_access?(permission) ⇒ Boolean
Returns ‘true` if internal access.
244 245 246 |
# File 'lib/aspera/api/aoc.rb', line 244 def workspace_access?() ['access_id'].start_with?("#{ID_AK_ADMIN}_WS_") end |
Instance Method Details
#add_ts_tags(transfer_spec:, app_info:) ⇒ Object
Add transfer spec callback in Node (transfer_spec_gen4)
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
# File 'lib/aspera/api/aoc.rb', line 644 def (transfer_spec:, app_info:) # translate transfer direction to upload/download transfer_type = Transfer::Spec.direction_to_transfer_type(transfer_spec['direction']) # Analytics tags ################ transfer_spec.deep_merge!({ 'tags' => { Transfer::Spec::TAG_RESERVED => { 'app' => app_info.app, 'usage_id' => "aspera.files.workspace.#{app_info.workspace_id}", # activity tracking 'files' => { 'node_id' => app_info.node_info['id'], 'files_transfer_action' => "#{transfer_type}_#{app_info.app.gsub(/s$/, '')}", 'workspace_name' => app_info.workspace_name, # activity tracking 'workspace_id' => app_info.workspace_id } } } }) # Console cookie ################ # we are sure that fields are not nil = [app_info.app, current_user_info['name'] || 'public link', current_user_info['email'] || 'none'].map{ |e| Base64.strict_encode64(e)} .unshift(COOKIE_PREFIX_CONSOLE_AOC) transfer_spec['cookie'] = .join(':') # Application tags ################## case app_info.app when AppType::FILES file_id = transfer_spec['tags'][Transfer::Spec::TAG_RESERVED]['node']['file_id'] transfer_spec.deep_merge!({ 'tags' => { Transfer::Spec::TAG_RESERVED => { 'files' => { 'parentCwd' => "#{app_info.node_info['id']}:#{file_id}" } } } }) unless transfer_spec.key?('remote_access_key') when AppType::PACKAGES transfer_spec.deep_merge!({ 'tags' => { Transfer::Spec::TAG_RESERVED => { 'files' => { 'package_id' => app_info.package['id'], 'package_name' => app_info.package['name'], 'package_operation' => transfer_type } } } }) end end |
#additional_persistence_ids ⇒ Object
369 370 371 372 |
# File 'lib/aspera/api/aoc.rb', line 369 def additional_persistence_ids return [current_user_info['id']] if public_link.nil? return [] # TODO : public_link['id'] ? end |
#assert_public_link_types(expected) ⇒ Object
365 366 367 |
# File 'lib/aspera/api/aoc.rb', line 365 def assert_public_link_types(expected) Aspera.assert_values(public_link['purpose'], expected){'public link type'} end |
#create_package_simple(package_data, validate_meta, new_user_option) ⇒ Object
create a package
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 |
# File 'lib/aspera/api/aoc.rb', line 605 def create_package_simple(package_data, , new_user_option) (package_data) # list of files to include in package, optional # package_data['file_names']||=[..list of filenames to transfer...] # lookup users resolve_package_recipients(package_data, 'recipients', new_user_option) resolve_package_recipients(package_data, 'bcc_recipients', new_user_option) (package_data) if # tell AoC what to expect in package: 1 transfer (can also be done after transfer) # TODO: if multi session was used we should probably tell # also, currently no "multi-source" , i.e. only from client-side files, unless "node" agent is used # `single_source` is required to allow web UI to ask for CSEAR password on download, see API doc package_data.merge!({ 'single_source' => true, 'sent' => true, 'transfers_expected' => 1 }) # create a new package container created_package = create('packages', package_data) package_node_api = node_api_from( node_id: created_package['node_id'], workspace_id: created_package['workspace_id'], package_info: created_package ) return { spec: package_node_api.transfer_spec_gen4(created_package['contents_file_id'], Transfer::Spec::DIRECTION_SEND), node: package_node_api, info: created_package } end |
#current_user_info(exception: false) ⇒ Object
Cached user information
385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/aspera/api/aoc.rb', line 385 def current_user_info(exception: false) return @cache_user_info unless @cache_user_info.nil? # get our user's default information @cache_user_info = begin read('self') rescue Aspera::RestCallError => e raise if exception || e..include?('invalid_grant') Log.log.debug{"Ignoring error: (#{e.class}) #{e}"} {} end USER_INFO_FIELDS_MIN.each{ |f| @cache_user_info[f] = nil if @cache_user_info[f].nil?} return @cache_user_info end |
#default_workspace? ⇒ Boolean
400 401 402 |
# File 'lib/aspera/api/aoc.rb', line 400 def default_workspace? @ws_ids[:name].eql?(DEFAULT_WORKSPACE) end |
#home ⇒ Object
Cached Home information for current user in Files app
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/aspera/api/aoc.rb', line 437 def home return @home_info unless @home_info.nil? @home_info = if !public_link.nil? assert_public_link_types(['view_shared_file']) { node_id: public_link['data']['node_id'], file_id: public_link['data']['file_id'] } elsif !private_link.nil? { node_id: private_link[:node_id], file_id: private_link[:file_id] } elsif workspace_info[:home_node_id] && workspace_info[:home_file_id] { node_id: workspace_info[:home_node_id], file_id: workspace_info[:home_file_id] } else # not part of any workspace, but has some folder shared user_info = current_user_info(exception: true) rescue {'read_only_home_node_id' => nil, 'read_only_home_file_id' => nil} { node_id: user_info['read_only_home_node_id'], file_id: user_info['read_only_home_file_id'] } end Aspera.assert(!@home_info[:node_id].to_s.empty?, "Cannot get user's home node id, check your default workspace or specify one", type: Error) Log.dump(:context, @home_info) @home_info end |
#node_api_from(node_id:, workspace_id: nil, workspace_name: nil, scope: Node::Scope::USER, package_info: nil) ⇒ Object
Return a Node API for given node id, in a given context (files, packages), for the given scope.
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/aspera/api/aoc.rb', line 476 def node_api_from(node_id:, workspace_id: nil, workspace_name: nil, scope: Node::Scope::USER, package_info: nil) Aspera.assert_type(node_id, String) node_info = read("nodes/#{node_id}") workspace_name = read("workspaces/#{workspace_id}")['name'] if workspace_name.nil? && !workspace_id.nil? app_info = AppInfo.new(self, package_info, node_info, workspace_id, workspace_name) node_params = {base_url: node_info['url']} ak_secret = @secret_finder&.lookup_secret(url: node_info['url'], username: node_info['access_key']) # If secret is available, or no scope, use basic auth if scope.nil? || ak_secret Aspera.assert(ak_secret, "Secret not found for access key #{node_info['access_key']}@#{node_info['url']}", type: Error) node_params[:auth] = { type: :basic, username: node_info['access_key'], password: ak_secret } else # OAuth bearer token node_params[:auth] = auth_params.clone node_params[:auth][:params] ||= {} node_params[:auth][:params][:scope] = Node.token_scope(node_info['access_key'], scope) node_params[:auth][:params][:owner_access] = true if scope.eql?(Node::Scope::ADMIN) # Special header required for bearer token only node_params[:headers] = {Node::HEADER_X_ASPERA_ACCESS_KEY => node_info['access_key']} end node_params[:app_info] = app_info return Node.new(**node_params) end |
#permissions_send_event(event_data:, app_info:, types: PERMISSIONS_CREATED) ⇒ Object
Callback from Plugins::Node send shared folder event to AoC
749 750 751 752 753 754 755 756 757 758 759 760 761 762 |
# File 'lib/aspera/api/aoc.rb', line 749 def (event_data:, app_info:, types: PERMISSIONS_CREATED) Aspera.assert_type(types, Array) Aspera.assert(!types.empty?) event_creation = { 'types' => types, 'node_id' => app_info.node_info['id'], 'workspace_id' => app_info.workspace_id, 'data' => event_data } # (optional). The name of the folder to be displayed to the destination user. # Use it if its value is different from the "share_as" field. event_creation['link_name'] = app_info.opt_link_name unless app_info.opt_link_name.nil? create('events', event_creation, query: {admin: true}) end |
#permissions_set_create_params(perm_data:, app_info:) ⇒ Object
Callback from Plugins::Node add application specific tags to permissions creation
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 |
# File 'lib/aspera/api/aoc.rb', line 702 def (perm_data:, app_info:) defaults = { 'tags' => { Transfer::Spec::TAG_RESERVED => { 'files' => { 'workspace' => { 'id' => app_info.workspace_id, 'workspace_name' => app_info.workspace_name, 'user_name' => current_user_info['name'], 'shared_by_user_id' => current_user_info['id'], 'shared_by_name' => current_user_info['name'], 'shared_by_email' => current_user_info['email'], 'access_key' => app_info.node_info['access_key'], 'node' => app_info.node_info['name'] } } } } } perm_data.deep_merge!(defaults) tag_workspace = perm_data['tags'][Transfer::Spec::TAG_RESERVED]['files']['workspace'] shared_with = perm_data.delete('with') case shared_with when NilClass when '' # workspace shared folder perm_data.merge!(self.class.workspace_access(app_info.workspace_id)) tag_workspace['shared_with_name'] = perm_data['access_id'] else entity_info = lookup_with_q('contacts', value: shared_with, query: {'current_workspace_id' => app_info.workspace_id}) perm_data['access_type'] = entity_info['source_type'] perm_data['access_id'] = entity_info['source_id'] tag_workspace['shared_with_name'] = entity_info['email'] # TODO: check that ??? end if perm_data.key?('as') tag_workspace['share_as'] = perm_data['as'] perm_data.delete('as') end # optional app_info.opt_link_name = perm_data.delete('link_name') end |
#public_link ⇒ Hash?
Cached public link information
376 377 378 379 380 381 382 |
# File 'lib/aspera/api/aoc.rb', line 376 def public_link return unless auth_params[:grant_method].eql?(:url_json) return @cache_url_token_info unless @cache_url_token_info.nil? # TODO: can there be several in list ? @cache_url_token_info = read('url_tokens').first return @cache_url_token_info end |
#read_with_paging(subpath, query = nil) ⇒ Hash
Read using the query and paging
359 360 361 362 363 |
# File 'lib/aspera/api/aoc.rb', line 359 def read_with_paging(subpath, query = nil) return self.class.call_paging(query: query) do |paged_query| read(subpath, query: paged_query, ret: :both) end end |
#resolve_package_recipients(package_data, rcpt_lst_field, new_user_option) ⇒ Object
Normalize package creation recipient lists as expected by AoC API AoC expects {type: , id: }, but ascli allows providing either the native values or just a name in that case, the name is resolved and replaced with {type: , id: }
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'lib/aspera/api/aoc.rb', line 541 def resolve_package_recipients(package_data, rcpt_lst_field, new_user_option) return unless package_data.key?(rcpt_lst_field) Aspera.assert_type(package_data[rcpt_lst_field], Array){rcpt_lst_field} new_user_option = {'package_contact' => true} if new_user_option.nil? Aspera.assert_type(new_user_option, Hash){'new_user_option'} ws_id = package_data['workspace_id'] # list with resolved elements resolved_list = [] package_data[rcpt_lst_field].each do |short_recipient_info| case short_recipient_info when Hash # native API information, check keys Aspera.assert(short_recipient_info.keys.sort.eql?(%w[id type])){"#{rcpt_lst_field} element shall have fields: id and type"} when String # CLI helper: need to resolve provided name to type/id # email: user, else dropbox entity_type = short_recipient_info.include?('@') ? 'contacts' : 'dropboxes' begin full_recipient_info = lookup_with_q(entity_type, value: short_recipient_info, query: {'workspace_id' => ws_id}) rescue EntityNotFound # dropboxes cannot be created on the fly Aspera.assert_values(entity_type, 'contacts', type: Error){"No such shared inbox in workspace #{ws_id}"} # unknown user: create it as external user full_recipient_info = create('contacts', { 'current_workspace_id' => ws_id, 'email' => short_recipient_info }.merge(new_user_option)) end short_recipient_info = if entity_type.eql?('dropboxes') {'id' => full_recipient_info['id'], 'type' => 'dropbox'} else {'id' => full_recipient_info['source_id'], 'type' => full_recipient_info['source_type']} end else Aspera.error_unexpected_value(short_recipient_info.class.name){"#{rcpt_lst_field} item must be a String (email, shared inbox) or Hash (id,type)"} end # add original or resolved recipient info resolved_list.push(short_recipient_info) end # replace with resolved elements package_data[rcpt_lst_field] = resolved_list return end |
#update_package_metadata_for_api(pkg_data) ⇒ Object
CLI allows simplified format for metadata: transform if necessary for API
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
# File 'lib/aspera/api/aoc.rb', line 583 def (pkg_data) case pkg_data['metadata'] when Array, NilClass # no action when Hash = pkg_data['metadata'].map do |k, v| { # 'input_type' => 'single-dropdown', 'name' => k, 'values' => v.is_a?(Array) ? v : [v] } end pkg_data['metadata'] = else Aspera.error_unexpected_value(.class) end return end |
#validate_metadata(pkg_data) ⇒ Object
Check metadata: remove when validation is done server side
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
# File 'lib/aspera/api/aoc.rb', line 505 def (pkg_data) # validate only for shared inboxes return unless pkg_data['recipients'].is_a?(Array) && pkg_data['recipients'].first.is_a?(Hash) && pkg_data['recipients'].first.key?('type') && pkg_data['recipients'].first['type'].eql?('dropbox') = read("dropboxes/#{pkg_data['recipients'].first['id']}")['metadata_schema'] if .nil? || .empty? Log.log.debug('no metadata in shared inbox') return end Aspera.assert(pkg_data.key?('metadata')){"package requires metadata: #{}"} = pkg_data['metadata'] Aspera.assert_type(, Array){'metadata'} Log.dump(:metadata, ) .each do |field| Aspera.assert_type(field, Hash){'metadata field'} Aspera.assert(field.key?('name')){'metadata field must have name'} Aspera.assert(field.key?('values')){'metadata field must have values'} Aspera.assert_type(field['values'], Array){'metadata field values'} Aspera.assert(!.none?{ |i| i['name'].eql?(field['name'])}){"unknown metadata field: #{field['name']}"} end .each do |field| provided = .select{ |i| i['name'].eql?(field['name'])} Aspera.assert(provided.count <= 1, type: Error){"only one field with name #{field['name']} allowed"} Aspera.assert(!provided.empty?, type: Error){"missing mandatory field: #{field['name']}"} if field['required'] end end |
#workspace_info ⇒ Hash{Symbol=>String}
Cached workspace information. Always with ‘:name`. If no workspace, then no `:id`
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/aspera/api/aoc.rb', line 408 def workspace_info return @workspace_info unless @workspace_info.nil? if !public_link.nil? Log.log.debug('Using workspace of public link') @ws_ids[:id] = public_link['data']['workspace_id'] elsif !private_link.nil? Log.log.debug('Using workspace of private link') @ws_ids[:id] = private_link[:workspace_id] elsif @ws_ids[:id] Log.log.debug("Using workspace id: #{@ws_ids[:id]}") elsif default_workspace? if current_user_info['default_workspace_id'].nil? @ws_ids[:name] = nil Log.log.warn('No default workspace') else Log.log.debug('Using default workspace'.green) @ws_ids[:id] = current_user_info['default_workspace_id'] end elsif !@ws_ids[:name].nil? @workspace_info = lookup_with_q('workspaces', value: @ws_ids[:name]) end @workspace_info = read("workspaces/#{@ws_ids[:id]}") unless @ws_ids[:id].nil? @workspace_info ||= {name: 'Shared (no workspace)'} @workspace_info = @workspace_info.slice('name', 'id', 'home_node_id', 'home_file_id').symbolize_keys Log.dump(:workspace_info, @workspace_info) @workspace_info end |