Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Using Patron for HTTP access instead of net/http

  • Loading branch information...
commit 9c732e4f83a84d2e60a7ef4d42c77916caaf64e9 1 parent 41bd27d
@greglu authored
View
2  Gemfile
@@ -1,5 +1,7 @@
source :rubygems
+gem "patron"
+
platforms :ruby do
gem "yajl-ruby"
end
View
2  Gemfile.lock
@@ -8,6 +8,7 @@ GEM
rbx-require-relative (> 0.0.4)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
+ patron (0.4.16)
rbx-require-relative (0.0.5)
rspec (2.3.0)
rspec-core (~> 2.3.0)
@@ -39,6 +40,7 @@ PLATFORMS
DEPENDENCIES
json
+ patron
rspec
ruby-debug
ruby-debug19
View
4 lib/stargate.rb
@@ -1,6 +1,8 @@
module Stargate end
-$:.unshift File.dirname(__FILE__)
+require 'pathname'
+pwd = Pathname(__FILE__).dirname
+$:.unshift(pwd.to_s) unless $:.include?(pwd.to_s) || $:.include?(pwd.expand_path.to_s)
require 'stargate/client'
require 'stargate/exception'
View
26 lib/stargate/client.rb
@@ -1,4 +1,4 @@
-require 'net/http'
+require 'patron'
require 'stargate/operation/meta_operation'
require 'stargate/operation/table_operation'
require 'stargate/operation/row_operation'
@@ -19,14 +19,12 @@ def initialize(url = "http://localhost:8080", opts = {})
raise "invalid http url: #{url}"
end
+ @connection = Patron::Session.new
+ @connection.base_url = url
+ @connection.timeout = opts[:timeout] unless opts[:timeout].nil?
+
# Not actually opening the connection yet, just setting up the persistent connection.
- if opts[:proxy]
- proxy_address, proxy_port = opts[:proxy].split(':')
- @connection = Net::HTTP.Proxy(proxy_address, proxy_port).new(@url.host, @url.port)
- else
- @connection = Net::HTTP.new(@url.host, @url.port)
- end
- @connection.read_timeout = opts[:timeout] if opts[:timeout]
+ @connection.proxy = opts[:proxy] unless opts[:proxy].nil?
end
def get(path, options = {})
@@ -66,21 +64,19 @@ def put_response(path, data = nil, options = {})
def safe_response(&block)
begin
yield
- rescue Errno::ECONNREFUSED, SocketError
- raise ConnectionNotEstablishedError, "Connection problem with Stargate server #{@url}"
- rescue Timeout::Error => e
- raise ConnectionTimeoutError, "Connection timed out to Stargate server #{@url}"
+ rescue => e
+ raise ConnectionNotEstablishedError, "Connection problem with Stargate server #{@url}:\n#{e.message}"
end
end
def safe_request(&block)
response = safe_response{ yield block }
- case response
- when Net::HTTPSuccess
+ case response.status
+ when 200
response.body
else
- response.error!
+ raise response.status_line
end
end
View
6 lib/stargate/operation/row_operation.rb
@@ -29,7 +29,7 @@ def show_row(table_name, name, timestamp = nil, columns = nil, options = { })
end
rows
end
- rescue Net::ProtocolError => e
+ rescue => e
# TODO: Use better handling instead of this.
if e.to_s.include?("Table")
raise TableNotFoundError, "Table '#{table_name}' Not Found"
@@ -66,7 +66,7 @@ def create_row(table_name, name, timestamp = nil, columns = nil)
xml_data << "</Row></CellSet>"
Response::RowResponse.new(post_response(request.create(data.map{|col| col[:name]}), xml_data), :create_row).parse
- rescue Net::ProtocolError => e
+ rescue => e
if e.to_s.include?("Table")
raise TableNotFoundError, "Table '#{table_name}' Not Found"
elsif e.to_s.include?("Row")
@@ -81,7 +81,7 @@ def delete_row(table_name, name, timestamp = nil, columns = nil)
begin
request = Request::RowRequest.new(table_name, name, timestamp)
Response::RowResponse.new(delete_response(request.delete(columns)), :delete_row).parse
- rescue Net::ProtocolError => e
+ rescue => e
if e.to_s.include?("Table")
raise TableNotFoundError, "Table '#{table_name}' Not Found"
elsif e.to_s.include?("Row")
View
4 lib/stargate/operation/scanner_operation.rb
@@ -43,7 +43,7 @@ def open_scanner(table_name, options = {})
scanner.table_name = table_name
scanner.batch_size = batch
scanner
- rescue Net::ProtocolError => e
+ rescue => e
raise StandardError, e.to_s
end
end
@@ -56,7 +56,7 @@ def get_rows(scanner, limit = nil)
rows = []
begin
# Loop until we've reached the limit, or the scanner was exhausted (HTTP 204 returned)
- until (limit && rows.size >= limit) || (response = get_response(request_url)).code == "204"
+ until (limit && rows.size >= limit) || (response = get_response(request_url)).status == 204
rows.concat Response::ScannerResponse.new(response.body, :get_rows).parse
rows.each do |row|
View
20 lib/stargate/operation/table_operation.rb
@@ -5,16 +5,15 @@ def show_table(name)
begin
request = Request::TableRequest.new(name)
Response::TableResponse.new(get(request.show)).parse
- rescue Net::ProtocolError
+ rescue => e
raise TableNotFoundError, "Table '#{name}' Not found"
end
end
def create_table(name, *args)
- request = Request::TableRequest.new(name)
-
raise StandardError, "Table name must be of type String" unless name.instance_of? String
+ request = Request::TableRequest.new(name)
begin
xml_data = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><TableSchema name='#{name}' IS_META='false' IS_ROOT='false'>"
for arg in args
@@ -35,13 +34,10 @@ def create_table(name, *args)
end
end
xml_data << "</TableSchema>"
- put_response(request.create, xml_data).is_a?(Net::HTTPSuccess)
- rescue Net::ProtocolError => e
- if e.to_s.include?("TableExistsException")
- raise TableExistsError, "Table '#{name}' already exists"
- else
- raise TableFailCreateError, e.message
- end
+
+ put_response(request.create, xml_data).status == 201
+ rescue => e
+ raise TableFailCreateError, e.message
end
end
@@ -53,7 +49,7 @@ def alter_table(name, *args)
begin
xml_data = construct_xml_stream(name, *args)
Response::TableResponse.new(put(request.update, xml_data))
- rescue Net::ProtocolError => e
+ rescue => e
if e.to_s.include?("TableNotFoundException")
raise TableNotFoundError, "Table '#{name}' not exists"
else
@@ -66,7 +62,7 @@ def delete_table(name, columns = nil)
begin
request = Request::TableRequest.new(name)
Response::TableResponse.new(delete(request.delete(columns)))
- rescue Net::ProtocolError => e
+ rescue => e
if e.to_s.include?("TableNotFoundException")
raise TableNotFoundError, "Table '#{name}' not exists"
elsif e.to_s.include?("TableNotDisabledException")
View
4 lib/stargate/response/basic_response.rb
@@ -22,8 +22,8 @@ def parse
end
def verify_success(response)
- case response
- when Net::HTTPSuccess
+ case response.status
+ when 200
true
else
false
View
20 lib/stargate/response/scanner_response.rb
@@ -11,26 +11,24 @@ def initialize(raw_data, method)
def parse_content(raw_data)
case @method
when :open_scanner
- case raw_data
- when Net::HTTPCreated
- Stargate::Model::Scanner.new(:scanner_url => raw_data["Location"])
+ case raw_data.status
+ when 201
+ Stargate::Model::Scanner.new(:scanner_url => raw_data.headers["Location"])
+ when 404
+ raise TableNotFoundError, "Table #{table_name} Not Found!"
else
- if raw_data.message.include?("TableNotFoundException")
- raise TableNotFoundError, "Table #{table_name} Not Found!"
- else
- raise StandardError, "Unable to open scanner. Received the following message: #{raw_data.message}"
- end
+ raise StandardError, "Unable to open scanner. Received the following message: #{raw_data.status_line}"
end
when :get_rows
# Dispatch it to RowResponse, since that method is made
# to deal with rows already.
RowResponse.new(raw_data, :show_row).parse
when :close_scanner
- case raw_data
- when Net::HTTPOK
+ case raw_data.status
+ when 200
return true
else
- raise StandardError, "Unable to close scanner. Received the following message: #{raw_data.message}"
+ raise StandardError, "Unable to close scanner. Received the following message: #{raw_data.status_line}"
end
else
puts "method '#{@method}' not supported yet"
View
1  spec/hbase-stargate/operation/row_operation_spec.rb
@@ -3,6 +3,7 @@
describe Stargate::Operation::RowOperation do
before :all do
url = ENV["STARGATE_URL"].nil? ? "http://localhost:8080" : ENV["STARGATE_URL"]
+
@client = Stargate::Client.new(url)
table = @client.create_table("test-hbase-stargate", "col1")
View
1  spec/hbase-stargate/operation/table_operation_spec.rb
@@ -16,7 +16,6 @@
:max_cell_size => 2147483647,
:bloomfilter => false
}
-
@client.create_table('test-hbase-stargate', @table_options).should be_true
end
Please sign in to comment.
Something went wrong with that request. Please try again.