On 03/01/2011 03:02 PM, mzatko(a)redhat.com wrote:
From: Maros Zatko<mzatko(a)redhat.com>
---
src/lib/warehouse_client.rb | 154 +++++++++++++++++++++++++++++++++++++++++++
1 files changed, 154 insertions(+), 0 deletions(-)
create mode 100644 src/lib/warehouse_client.rb
diff --git a/src/lib/warehouse_client.rb b/src/lib/warehouse_client.rb
new file mode 100644
index 0000000..9431e47
--- /dev/null
+++ b/src/lib/warehouse_client.rb
@@ -0,0 +1,154 @@
+require 'rubygems'
+
+require 'rest-client'
+require 'nokogiri'
+
+#TODO: perform iwhd version-dependent URI mapping
+
+module Warehouse
+
+ class BucketObject
+ attr_reader :key
+
+ def initialize(connection, key, bucket)
+ @connection = connection
+ @key = key
+ @bucket = bucket
+ @path = "/#{@bucket.name}/#{(a)key}"
+ end
+
+ def self.create(connection, key, bucket, body, attrs = {})
+ obj = new(connection, key, bucket)
+ obj.set_body(body)
+ obj.set_attrs(attrs)
+ obj
+ end
+
+ def body
+ @connection.do_request @path, :plain => true
+ end
+
+ def set_body(body)
+ @connection.do_request @path, :content => body, :method => :put
+ end
+
+ def attr_list
+ result = @connection.do_request @path, :content => 'op=parts', :method
=> :post
+ return result.xpath('/object/object_attr/(a)name').to_a.map {|item|
item.value}
+ end
+
+ def attrs(list)
+ attrs = {}
+ list.each do |att|
+ attrs[att] = (@connection.do_request("#{@path}/#{att}", :plain =>
true) rescue nil)
+ end
+ attrs
+ end
+
+ def attr(name)
+ attrs([name]).first
+ end
+
+ def set_attrs(hash)
+ hash.each do |name, content|
+ set_attr(name, content)
+ end
+ end
+
+ def set_attr(name, content)
+ path = "#{@path}/#{name}"
+ @connection.do_request path, :content => content, :method => :put
+ end
+
+ def delete!
+ @connection.do_request @path, :method => :delete
+ true
+ end
+
+ end
+
+ class Bucket
+ attr_accessor :name
+
+ def initialize(name, connection)
+ @name = name
+ @connection = connection
+ end
+
+ def to_s
+ "Bucket: #{@name}"
+ end
+
+ def object_names
+ result = @connection.do_request "/#{@name}"
+ result.xpath('/objects/object').map do |obj|
+ obj.at_xpath('./key/text()').to_s
+ end
+ end
+
+ def objects
+ object_names.map do |name|
+ object(name)
+ end
+ end
+
+ def object(key)
+ BucketObject.new @connection, key, self
+ end
+
+ def create_object(key, body, attrs)
+ BucketObject.create(@connection, key, self, body, attrs)
+ end
+ end
+
+ class Connection
+ attr_accessor :uri
+
+ def initialize(uri)
+ @uri = uri
+ end
+
+ def do_request(path = '', opts={})
+ opts[:method] ||= :get
+ opts[:content] ||= ''
+ opts[:plain] ||= false
+ opts[:headers] ||= {}
+
+ result = RestClient::Request.execute :method => opts[:method], :url =>
@uri + path, :payload => opts[:content], :headers => opts[:headers]
+
+ return Nokogiri::XML result unless opts[:plain]
+ return result
+ end
+
+ end
+
+ class Client
+
+ def initialize(uri)
+ @connection = Connection.new(uri)
+ end
+
+ def create_bucket(bucket)
+ @connection.do_request "/#{bucket}", :method => :put
+ Bucket.new(bucket, @connection)
+ end
+
+ def bucket(bucket)
+ Bucket.new bucket, @connection
+ end
+
+ def buckets
+ @connection.do_request.xpath('/api/link[(a)rel="bucket"]').map do
|obj|
+ obj.at_xpath('./(a)href').to_s.gsub(/.*\//, '')
+ end
+ end
+
+ def get_iwhd_version
+ result =
@connection.do_request.at_xpath('/api[@service="image_warehouse"]/@version')
+ raise "Response does not contain<api> tag or version information"
if result == nil
+ return result.value
+ end
+
+ end
+
+end
ACK and pushed to origin/warehouse