+def api_call(method, parameters:{}, request_body:{})
+ result = $client.execute(:api_method => method,
+ :parameters => parameters,
+ :body_object => request_body,
+ :authenticated => false,
+ :headers => {
+ authorization: "OAuth2 #{ENV['ARVADOS_API_TOKEN']}",
+ })
+
+ begin
+ results = JSON.parse result.body
+ rescue JSON::ParserError => e
+ abort "Failed to parse server response:\n" + e.to_s
+ end
+
+ if results["errors"]
+ abort "Error: #{results["errors"][0]}"
+ end
+
+ return results
+end
+
+def tag_add(tag, obj_uuid)
+ return api_call($arvados.links.create,
+ request_body: {
+ :link => {
+ :name => tag,
+ :link_class => :tag,
+ :head_uuid => obj_uuid,
+ }
+ })
+end
+
+def tag_remove(tag, obj_uuids=nil)
+ # If we got a list of objects to untag, look up the uuids for the
+ # links that need to be deleted.
+ link_uuids = []
+ if obj_uuids
+ obj_uuids.each do |uuid|
+ link = api_call($arvados.links.list,
+ request_body: {
+ :where => {
+ :link_class => :tag,
+ :name => tag,
+ :head_uuid => uuid,
+ }
+ })
+ if link['items_available'] > 0
+ link_uuids.push link['items'][0]['uuid']
+ end
+ end
+ else
+ all_tag_links = api_call($arvados.links.list,
+ request_body: {
+ :where => {
+ :link_class => :tag,
+ :name => tag,
+ }
+ })
+ link_uuids = all_tag_links['items'].map { |obj| obj['uuid'] }
+ end