lib/openwfe/rest/restclient.rb in openwferu-0.9.10.653 vs lib/openwfe/rest/restclient.rb in openwferu-0.9.11
- old
+ new
@@ -39,11 +39,13 @@
require 'base64'
require 'net/http'
require 'rexml/document'
+require 'openwfe/version'
+
module OpenWFE
#
# A basic REST client for OpenWFE services (control and worklist)
#
@@ -61,29 +63,55 @@
end
#
# Closes this REST client
#
- def close ()
-
- post('endWorkSession', nil, {}, nil)
+ def close
+ get('endWorkSession', nil, {})
end
protected
+ def decode (reply)
+
+ raise "Error : #{reply.code} - #{reply.body}" \
+ if reply.code != "200"
+
+ begin
+ xml = REXML::Document.new reply.body
+ OpenWFE::XmlCodec::decode xml.root
+ rescue Exception => e
+ if $DEBUG
+ #puts
+ #puts e.to_s
+ puts
+ puts "failed to decode reply :"
+ puts
+ puts reply.body
+ puts
+ end
+ raise e
+ end
+ end
+
+ #
+ # GETs a REST operation
+ #
def get (action, subResourceName, params)
- return @httpclient\
- .get(compute_resource(action, subResourceName, params))
+ @httpclient.get(
+ compute_resource(action, subResourceName, params))
end
+ #
+ # POSTs a REST operation
+ #
def post (action, subResourceName, params, data)
- data = data.to_s()
-
- return @httpclient\
- .post(compute_resource(action, subResourceName, params), data)
+ @httpclient.post(
+ compute_resource(action, subResourceName, params),
+ data.to_s)
end
private
def split_url (url)
@@ -93,11 +121,11 @@
@resource = nil
url = url[7..-1] if url[0..6] == 'http://'
i = url.index('/')
- if i < 0
+ unless i
@resource = '/defaultrestresource'
else
@resource = url[i..-1]
url = url[0..i]
end
@@ -115,34 +143,36 @@
@httpclient = Net::HTTP.new(@host, @port)
hs = {}
hs['Authorization'] = \
- 'Basic ' + Base64.encode64(@username+":"+password)
- hs['RestClient'] = 'openwfe-ruby'
+ 'Basic ' + Base64.encode64(@username+":"+password).strip
+ hs['RestClient'] = "openwfe-ruby #{OPENWFERU_VERSION}"
+ hs['User-Agent'] = "openwfe-ruby #{OPENWFERU_VERSION}"
+ #puts "@resource is '#{@resource}'"
+ #puts "hs is '#{hs.inspect}'"
+
r = @httpclient.get(@resource, hs)
- xml = REXML::Document.new(r.body)
+ #xml = REXML::Document.new(r.body)
+ #@session_id = Integer(xml.root.attributes["id"])
- @session_id = Integer(xml.root.attributes["id"])
+ @session_id = decode r
end
- def compute_resource (action, subResourceName, params)
+ def compute_resource (action, sub_resource_name, params)
- resource = @resource
- resource = resource+'/'+subResourceName if subResourceName
+ reso = @resource.dup
+ reso += "/#{sub_resource_name}" if sub_resource_name
- resource = resource+\
- '?session='+@session_id.to_s()+'&action='+action
+ reso += "?session=#{@session_id.to_s}&action=#{action}"
- if params
- params.keys.each do |k|
- resource = resource+'&'+k.to_s()+'='+params[k].to_s()
- end
- end
+ params.each { |k, v|
+ reso += "&#{k.to_s}=#{v.to_s}"
+ } if params
- return resource
+ reso
end
end
end