lib/globus/client/endpoint.rb in globus_client-0.2.1 vs lib/globus/client/endpoint.rb in globus_client-0.3.0

- old
+ new

@@ -13,15 +13,18 @@ @user_id = user_id @work_id = work_id @work_version = work_version end - # NOTE: This is a temporary method to show parsing of data returned. - def length + def file_count objects["total"] end + def total_size + files.sum { |file| file["size"] } + end + # Create a directory https://docs.globus.org/api/transfer/file_operations/#make_directory def mkdir # transfer API does not support recursive directory creation paths.each do |path| response = connection.post("#{transfer_path}/mkdir") do |req| @@ -43,32 +46,17 @@ UnexpectedResponse.call(response) end end # Assign a user read/write permissions for a directory https://docs.globus.org/api/transfer/acl/#rest_access_create - def set_permissions - response = connection.post(access_path) do |req| - req.body = { - DATA_TYPE: "access", - principal_type: "identity", - principal: identity.get_identity_id(user_id), - path: paths.last, - permissions: "rw", - notify_email: "#{user_id}@stanford.edu" - }.to_json - req.headers["Content-Type"] = "application/json" - end + def allow_writes + access_request(permissions: "rw") + end - return response if response.success? - - # Ignore error if permissions already set for identity - if response.status == 409 - error = JSON.parse(response.body) - return if error["code"] == "Exists" - end - - UnexpectedResponse.call(response) + # Assign a user read-only permissions for a directory https://docs.globus.org/api/transfer/acl/#rest_access_create + def disallow_writes + access_request(permissions: "r") end private attr_reader :config, :user_id, :work_id, :work_version @@ -79,12 +67,12 @@ url: config.transfer_url, headers: {Authorization: "Bearer #{config.token}"} ) end - def identity - Globus::Client::Identity.new(config) + def user + Identity.new(config).get_identity_id(user_id) end # Builds up a path from a list of path elements. E.g., input would look like: # ["mjgiarlo", "work123", "version1"] # And this method returns: @@ -93,27 +81,82 @@ path_segments.map.with_index do |_segment, index| File.join(config.uploads_directory, path_segments.slice(..index)).concat("/") end end + # @see #paths + def full_path + paths.last + end + def path_segments [user_id, "work#{work_id}", "version#{work_version}"] end def objects # List files at an endpoint https://docs.globus.org/api/transfer/file_operations/#list_directory_contents - response = connection.get("#{transfer_path}/ls") + response = connection.get("#{transfer_path}/ls?path=#{full_path}") return JSON.parse(response.body) if response.success? UnexpectedResponse.call(response) end + def files + objects["DATA"].select { |object| object["DATA_TYPE"] == "file" } + end + + def access_request(permissions:) + response = if access_rule_id + connection.put("#{access_path}/#{access_rule_id}") do |req| + req.body = { + DATA_TYPE: "access", + permissions: + }.to_json + req.headers["Content-Type"] = "application/json" + end + else + connection.post(access_path) do |req| + req.body = { + DATA_TYPE: "access", + principal_type: "identity", + principal: user, + path: full_path, + permissions:, + notify_email: "#{user_id}@stanford.edu" + }.to_json + req.headers["Content-Type"] = "application/json" + end + end + + return response if response.success? + + UnexpectedResponse.call(response) + end + + def access_rule + response = connection.get(access_list_path) do |req| + req.headers["Content-Type"] = "application/json" + end + + JSON + .parse(response.body)["DATA"] + .find { |acl| acl["path"] == full_path } + end + + def access_rule_id + access_rule&.fetch("id") + end + def transfer_path "/v0.10/operation/endpoint/#{config.transfer_endpoint_id}" end def access_path "/v0.10/endpoint/#{config.transfer_endpoint_id}/access" + end + + def access_list_path + "/v0.10/endpoint/#{config.transfer_endpoint_id}/access_list" end end end end