From 91c054d658ea1afe956d66789ad158a0cb763383 Mon Sep 17 00:00:00 2001 From: Adam Hallett Date: Wed, 13 Mar 2013 16:43:25 -0500 Subject: [PATCH] adding methods for getting the media package xml and removing workflow 'tracks' --- lib/rubyhorn/matterhorn_client.rb | 2 ++ lib/rubyhorn/rest_client/common.rb | 18 ++++++++++++--- lib/rubyhorn/rest_client/exceptions.rb | 7 ++++++ lib/rubyhorn/rest_client/hls_distribution.rb | 23 ++++++++++++++++++++ lib/rubyhorn/rest_client/workflow.rb | 21 ++++++++++++++++++ 5 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 lib/rubyhorn/rest_client/hls_distribution.rb diff --git a/lib/rubyhorn/matterhorn_client.rb b/lib/rubyhorn/matterhorn_client.rb index 34a4ff3..87ca02c 100644 --- a/lib/rubyhorn/matterhorn_client.rb +++ b/lib/rubyhorn/matterhorn_client.rb @@ -3,6 +3,7 @@ require 'rubyhorn/rest_client/ingest' require 'rubyhorn/rest_client/workflow' require 'rubyhorn/rest_client/exceptions' +require 'rubyhorn/rest_client/hls_distribution' module Rubyhorn class MatterhornClient @@ -11,6 +12,7 @@ class MatterhornClient include Rubyhorn::RestClient::Ingest include Rubyhorn::RestClient::Workflow include Rubyhorn::RestClient::Exceptions + include Rubyhorn::RestClient::HLSDistribution # repository configuration (see #initialize) attr_reader :config diff --git a/lib/rubyhorn/rest_client/common.rb b/lib/rubyhorn/rest_client/common.rb index 705aa9f..3cc728d 100644 --- a/lib/rubyhorn/rest_client/common.rb +++ b/lib/rubyhorn/rest_client/common.rb @@ -32,9 +32,11 @@ def execute_request uri, req auth = digest_auth.auth_header uri, res['www-authenticate'], req.method req.add_field 'Authorization', auth res = @http.request req - - if res.code.to_i.between?(500,599) - raise Rubyhorn::RestClient::Exceptions::ServerError.new(res.code) if res.code.to_i.between?(500,599) + code_as_int = res.code.to_i + if code_as_int == 404 + raise Rubyhorn::RestClient::Exceptions::HTTPNotFound.new + elsif code_as_int.between?(500,599) + raise Rubyhorn::RestClient::Exceptions::ServerError.new(code_as_int) else res end @@ -66,6 +68,16 @@ def post url, args = {} return response.body end + def delete( url, args ) + url = config[:url] + url + uri = URI.parse(url) + request = Net::HTTP::Delete.new(uri.request_uri) + request.form_data = args + request['Cookie'] = @cookie + response = execute_request(uri, request) + return response.body + end + def multipart_post url, file, args = {} if args.has_key? "filename" filename = args["filename"] diff --git a/lib/rubyhorn/rest_client/exceptions.rb b/lib/rubyhorn/rest_client/exceptions.rb index 0809bfc..de43188 100644 --- a/lib/rubyhorn/rest_client/exceptions.rb +++ b/lib/rubyhorn/rest_client/exceptions.rb @@ -18,5 +18,12 @@ def initialize(params) super("You failed to include #{params.join(', ')} in your request.") end end + + class HTTPNotFound < RubyhornException + def initialize + super("Not found") + end + end + end end diff --git a/lib/rubyhorn/rest_client/hls_distribution.rb b/lib/rubyhorn/rest_client/hls_distribution.rb new file mode 100644 index 0000000..92572a6 --- /dev/null +++ b/lib/rubyhorn/rest_client/hls_distribution.rb @@ -0,0 +1,23 @@ +module Rubyhorn::RestClient + module HLSDistribution + + def delete_track( media_package, track ) + args = { + :mediapackage => media_package, + :elementId => track + } + + post("distribution/streaming/retract", args) + end + + # example usage of delete_track + # def delete_workflow_tracks(workflow_id) + # media_package = Rubyhorn.client.get_media_package(workflow_id) + # tracks = media_package.xpath('//track').map{|node| node.get_attribute('id')} + + # tracks.each do |track| + # Rubyhorn.client.delete_track(media_package.to_s, track) + # end + # end + end +end \ No newline at end of file diff --git a/lib/rubyhorn/rest_client/workflow.rb b/lib/rubyhorn/rest_client/workflow.rb index 4c1919b..77f8450 100644 --- a/lib/rubyhorn/rest_client/workflow.rb +++ b/lib/rubyhorn/rest_client/workflow.rb @@ -3,23 +3,44 @@ module Workflow def handlers return JSON.parse(get("workflow/handlers.json")) end + def statistics_json return JSON.parse(get("workflow/statistics.json")) end + def statistics_xml #TODO write me and parse into OM document or just use Nokogiri? end + def instance_json id return JSON.parse(get("workflow/instance/#{id}.json")) end + def instance_xml id return Rubyhorn::Workflow.from_xml(get("workflow/instance/#{id}.xml")) end + def instances_json args return JSON.parse(get("workflow/instances.json", args)) end + def stop id return Rubyhorn::Workflow.from_xml(post("workflow/stop", {"id"=>id})) end + + def delete_instance + return Rubyhorn::Workflow.from_xml(delete("workflow/remove", {"id"=>id})) + end + + def get_media_package(workflow_id) + package = Rubyhorn::Workflow.from_xml(get("workflow/instance/#{workflow_id}.xml")) + doc = Nokogiri.XML(package.to_xml) + doc.remove_namespaces! + doc = doc.xpath('//mediapackage') + first_node = doc.first + first_node['xmlns'] = 'http://mediapackage.opencastproject.org' + doc + end + end end