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

- old
+ new

@@ -13,38 +13,62 @@ @user_id = user_id @work_id = work_id @work_version = work_version end - # This is a temporary method to show parsing of data returned. + # NOTE: This is a temporary method to show parsing of data returned. def length objects["total"] 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 = call_mkdir(path) + response = connection.post("#{transfer_path}/mkdir") do |req| + req.headers["Content-Type"] = "application/json" + req.body = { + DATA_TYPE: "mkdir", + path: + }.to_json + end + next if response.success? - # if directory already exists + # Ignore error if directory already exists if response.status == 502 error = JSON.parse(response.body) - next if error["code"] == "ExternalError.MkdirFailedExists" + next if error["code"] == "ExternalError.MkdirFailed.Exists" end 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 - path = "#{config.uploads_directory}/#{user_id}/work#{work_id}/version#{work_version}/" - identity = Globus::Client::Identity.new(config) - id = identity.get_identity_id(user_id) - call_access(path:, id:, user_id:) + 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 + + 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) end private attr_reader :config, :user_id, :work_id, :work_version @@ -55,10 +79,14 @@ url: config.transfer_url, headers: {Authorization: "Bearer #{config.token}"} ) end + def identity + Globus::Client::Identity.new(config) + end + # Builds up a path from a list of path elements. E.g., input would look like: # ["mjgiarlo", "work123", "version1"] # And this method returns: # ["/uploads/mjgiarlo/", "/uploads/mjgiarlo/work123/", "/uploads/mjgiarlo/work123/version1/"] def paths @@ -69,51 +97,23 @@ def path_segments [user_id, "work#{work_id}", "version#{work_version}"] end - def endpoint - "/v0.10/operation/endpoint/#{config.transfer_endpoint_id}" - 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") + return JSON.parse(response.body) if response.success? - # @return [Faraday::Response] - def call_mkdir(path) - connection.post("#{endpoint}/mkdir") do |req| - req.headers["Content-Type"] = "application/json" - req.body = { - DATA_TYPE: "mkdir", - path: - }.to_json - end + UnexpectedResponse.call(response) end - # Makes the API call to Globus to set permissions - # @param path [String] the directory on the globus endpoint - # @param id [String] globus identifier associated with the user_id email - # @param user_id [String] user_id, not email address - # @return [Faraday::Response] - def call_access(path:, id:, user_id:) - response = connection.post("#{endpoint}/access") do |req| - req.body = { - DATA_TYPE: "access", - principal_type: "identity", - principal: id, - path:, - permissions: "rw", - notify_email: "#{user_id}@stanford.edu" - }.to_json - req.headers["Content-Type"] = "application/json" - end - UnexpectedResponse.call(response) unless response.success? - - response + def transfer_path + "/v0.10/operation/endpoint/#{config.transfer_endpoint_id}" end - def objects - # List files at an endpoint https://docs.globus.org/api/transfer/file_operations/#list_directory_contents - response = connection.get("#{endpoint}/ls") - UnexpectedResponse.call(response) unless response.success? - JSON.parse(response.body) + def access_path + "/v0.10/endpoint/#{config.transfer_endpoint_id}/access" end end end end