mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
return response objects, misc cleanup/refactoring
This commit is contained in:
parent
3381648dc4
commit
23799013f4
12 changed files with 128 additions and 89 deletions
|
@ -8,54 +8,69 @@ require 'uri'
|
|||
module Fog
|
||||
module AWS
|
||||
class Connection < EventMachine::Connection
|
||||
include EventMachine::Deferrable
|
||||
include EventMachine::Deferrable
|
||||
|
||||
attr_accessor :headers, :method, :url, :parser, :status
|
||||
attr_accessor :headers, :method, :url, :parser
|
||||
attr_reader :response
|
||||
|
||||
def response
|
||||
@parser.response
|
||||
end
|
||||
def post_init
|
||||
@data ||= nil
|
||||
@headers ||= {}
|
||||
@response ||= Fog::AWS::Response.new
|
||||
end
|
||||
|
||||
def post_init
|
||||
@data = nil
|
||||
@headers = {}
|
||||
end
|
||||
def connection_completed
|
||||
uri = URI.parse(@url)
|
||||
if uri.scheme == 'https'
|
||||
start_tls
|
||||
else
|
||||
request
|
||||
end
|
||||
end
|
||||
|
||||
def connection_completed
|
||||
uri = URI.parse(@url)
|
||||
if uri.scheme == 'https'
|
||||
start_tls
|
||||
else
|
||||
request
|
||||
end
|
||||
end
|
||||
def ssl_handshake_completed
|
||||
request
|
||||
end
|
||||
|
||||
def ssl_handshake_completed
|
||||
request
|
||||
end
|
||||
|
||||
def request
|
||||
uri = URI.parse(@url)
|
||||
path = "#{uri.path}#{uri.query.nil? ? "" : "?#{uri.query}"}"
|
||||
host = "#{uri.host}#{uri.port == 80 ? "" : ":#{uri.port}"}"
|
||||
headers.merge!({'Host' => host})
|
||||
send_data("#{method} #{path} HTTP/1.1\r\n#{headers.collect {|k,v| "#{k}: #{v}\r\n"}.join('')}\r\n")
|
||||
end
|
||||
|
||||
def receive_data(data)
|
||||
unless @data
|
||||
if data =~ /\AHTTP\/1\.[01] ([\d]{3})/
|
||||
@status = $1.to_i
|
||||
else
|
||||
@status = 0
|
||||
end
|
||||
@data = data.split(/<\?xml.*\?>/)[1]
|
||||
Nokogiri::XML::SAX::Parser.new(@parser).parse(@data)
|
||||
set_deferred_status(:succeeded, self)
|
||||
EventMachine.stop_event_loop
|
||||
end
|
||||
end
|
||||
def request
|
||||
uri = URI.parse(@url)
|
||||
path = "#{uri.path}#{uri.query.nil? ? "" : "?#{uri.query}"}"
|
||||
host = "#{uri.host}#{uri.port == 80 ? "" : ":#{uri.port}"}"
|
||||
@headers.merge!({'Host' => host})
|
||||
send_data("#{method} #{path} HTTP/1.1\r\n#{headers.collect {|k,v| "#{k}: #{v}\r\n"}.join('')}\r\n")
|
||||
end
|
||||
|
||||
def receive_data(data)
|
||||
p data
|
||||
unless @data
|
||||
if data =~ /\AHTTP\/1\.[01] ([\d]{3})/
|
||||
@response.status = $1.to_i
|
||||
else
|
||||
@response.status = 0
|
||||
end
|
||||
@headers, @data = data.split(/<\?xml.*\?>/)
|
||||
@headers.split("\r\n").each do |header|
|
||||
if data = header.match(/(.*):\s(.*)/)
|
||||
@response.headers[data[1]] = data[2]
|
||||
end
|
||||
end
|
||||
if @data
|
||||
Nokogiri::XML::SAX::Parser.new(@parser).parse(@data)
|
||||
@response.body = @parser.response
|
||||
end
|
||||
set_deferred_status(:succeeded, self)
|
||||
EventMachine.stop_event_loop
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Response
|
||||
attr_accessor :status, :headers, :body
|
||||
|
||||
def initialize
|
||||
@headers = {}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -2,6 +2,7 @@ require 'rubygems'
|
|||
require 'base64'
|
||||
require 'cgi'
|
||||
require 'hmac-sha1'
|
||||
require 'uri'
|
||||
|
||||
require File.dirname(__FILE__) + '/s3/parsers'
|
||||
|
||||
|
@ -42,10 +43,15 @@ module Fog
|
|||
def put_bucket(name)
|
||||
request('PUT', "#{@scheme}://#{name}.#{@host}:#{@port}/", Fog::Parsers::AWS::S3::BasicParser.new)
|
||||
end
|
||||
|
||||
def delete_bucket(name)
|
||||
request('DELETE', "#{@scheme}://#{name}.#{@host}:#{@port}/", Fog::Parsers::AWS::S3::BasicParser.new)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def request(method, url, parser, data=nil)
|
||||
uri = URI.parse(url)
|
||||
headers = { 'Date' => Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000") }
|
||||
params = [
|
||||
method,
|
||||
|
@ -53,7 +59,7 @@ module Fog
|
|||
content_type = '',
|
||||
headers['Date'],
|
||||
canonicalized_amz_headers = nil,
|
||||
canonicalized_resource = '/'
|
||||
canonicalized_resource = "/#{'s3.amazonaws.com' == uri.host ? "" : "#{uri.host.split('.s3.amazonaws.com')[0]}/"}"
|
||||
]
|
||||
string_to_sign = params.delete_if {|value| value.nil?}.join("\n")
|
||||
hmac = @hmac.update(string_to_sign)
|
||||
|
|
9
spec/aws/s3/delete_bucket_spec.rb
Normal file
9
spec/aws/s3/delete_bucket_spec.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
require File.dirname(__FILE__) + '/../../spec_helper'
|
||||
|
||||
describe 'S3.delete_bucket' do
|
||||
|
||||
it 'should return stuff' do
|
||||
p s3.delete_bucket('fogputbucket')
|
||||
end
|
||||
|
||||
end
|
9
spec/aws/s3/put_bucket_spec.rb
Normal file
9
spec/aws/s3/put_bucket_spec.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
require File.dirname(__FILE__) + '/../../spec_helper'
|
||||
|
||||
describe 'S3.put_bucket' do
|
||||
|
||||
it 'should return stuff' do
|
||||
p s3.put_bucket('fogputbucket')
|
||||
end
|
||||
|
||||
end
|
|
@ -11,25 +11,25 @@ describe 'SimpleDB.batch_put_attributes' do
|
|||
end
|
||||
|
||||
it 'should have no attributes for y before batch_put_attributes' do
|
||||
lambda { sdb.get_attributes('batch_put_attributes', 'a') }.should eventually { |expected| expected[:attributes].should be_empty }
|
||||
lambda { sdb.get_attributes('batch_put_attributes', 'a') }.should eventually { |expected| expected.body[:attributes].should be_empty }
|
||||
end
|
||||
|
||||
it 'should have no attributes for x before batch_put_attributes' do
|
||||
lambda { sdb.get_attributes('batch_put_attributes', 'x') }.should eventually { |expected| expected[:attributes].should be_empty }
|
||||
lambda { sdb.get_attributes('batch_put_attributes', 'x') }.should eventually { |expected| expected.body[:attributes].should be_empty }
|
||||
end
|
||||
|
||||
it 'should return proper attributes from batch_put_attributes' do
|
||||
actual = sdb.batch_put_attributes('batch_put_attributes', { 'a' => { 'b' => 'c' }, 'x' => { 'y' => 'z' } })
|
||||
actual[:request_id].should be_a(String)
|
||||
actual[:box_usage].should be_a(Float)
|
||||
actual.body[:request_id].should be_a(String)
|
||||
actual.body[:box_usage].should be_a(Float)
|
||||
end
|
||||
|
||||
it 'should have correct attributes for a after batch_put_attributes' do
|
||||
lambda { sdb.get_attributes('batch_put_attributes', 'a') }.should eventually { |expected| expected[:attributes].should == { 'b' => ['c'] } }
|
||||
lambda { sdb.get_attributes('batch_put_attributes', 'a') }.should eventually { |expected| expected.body[:attributes].should == { 'b' => ['c'] } }
|
||||
end
|
||||
|
||||
it 'should have correct attributes for x after batch_put_attributes' do
|
||||
lambda { sdb.get_attributes('batch_put_attributes', 'x') }.should eventually { |expected| expected[:attributes].should == { 'y' => ['z'] } }
|
||||
lambda { sdb.get_attributes('batch_put_attributes', 'x') }.should eventually { |expected| expected.body[:attributes].should == { 'y' => ['z'] } }
|
||||
end
|
||||
|
||||
end
|
|
@ -7,17 +7,17 @@ describe 'SimpleDB.create_domain' do
|
|||
end
|
||||
|
||||
it 'should not include test domain in list_domains before create_domain' do
|
||||
lambda { sdb.list_domains }.should_not eventually { |expected| expected[:domains].should_not include('create_domain') }
|
||||
lambda { sdb.list_domains }.should_not eventually { |expected| expected.body[:domains].should_not include('create_domain') }
|
||||
end
|
||||
|
||||
it 'should return proper attributes from create_domain' do
|
||||
actual = sdb.create_domain('create_domain')
|
||||
actual[:request_id].should be_a(String)
|
||||
actual[:box_usage].should be_a(Float)
|
||||
actual.body[:request_id].should be_a(String)
|
||||
actual.body[:box_usage].should be_a(Float)
|
||||
end
|
||||
|
||||
it 'should include test in list_domains after create_domain' do
|
||||
lambda { sdb.list_domains }.should eventually { |expected| expected[:domains].should include('create_domain') }
|
||||
lambda { sdb.list_domains }.should eventually { |expected| expected.body[:domains].should include('create_domain') }
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -12,17 +12,17 @@ describe 'SimpleDB.delete_attributes' do
|
|||
end
|
||||
|
||||
it 'should have attributes for foo before delete_attributes' do
|
||||
lambda { sdb.get_attributes('delete_attributes', 'foo') }.should eventually { |expected| expected[:attributes].should == { 'bar' => ['baz'] } }
|
||||
lambda { sdb.get_attributes('delete_attributes', 'foo') }.should eventually { |expected| expected.body[:attributes].should == { 'bar' => ['baz'] } }
|
||||
end
|
||||
|
||||
it 'should return proper attributes from delete_attributes' do
|
||||
actual = sdb.delete_attributes('delete_attributes', 'foo')
|
||||
actual[:request_id].should be_a(String)
|
||||
actual[:box_usage].should be_a(Float)
|
||||
actual.body[:request_id].should be_a(String)
|
||||
actual.body[:box_usage].should be_a(Float)
|
||||
end
|
||||
|
||||
it 'should have no attributes for foo after delete_attributes' do
|
||||
lambda { sdb.get_attributes('delete_attributes', 'foo') }.should eventually { |expected| expected[:attributes].should be_empty }
|
||||
lambda { sdb.get_attributes('delete_attributes', 'foo') }.should eventually { |expected| expected.body[:attributes].should be_empty }
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -7,17 +7,17 @@ describe 'SimpleDB.delete_domain' do
|
|||
end
|
||||
|
||||
it 'should include delete_domain in list_domains before delete_domain' do
|
||||
lambda { sdb.list_domains }.should eventually { |expected| expected[:domains].should include('delete_domain') }
|
||||
lambda { sdb.list_domains }.should eventually { |expected| expected.body[:domains].should include('delete_domain') }
|
||||
end
|
||||
|
||||
it 'should return proper attributes' do
|
||||
actual = sdb.delete_domain('delete_domain')
|
||||
actual[:request_id].should be_a(String)
|
||||
actual[:box_usage].should be_a(Float)
|
||||
actual.body[:request_id].should be_a(String)
|
||||
actual.body[:box_usage].should be_a(Float)
|
||||
end
|
||||
|
||||
it 'should not include delete_domain in list_domains after delete_domain' do
|
||||
lambda { sdb.list_domains }.should_not eventually { |expected| expected[:domains].should_not include('delete_domain') }
|
||||
lambda { sdb.list_domains }.should_not eventually { |expected| expected.body[:domains].should_not include('delete_domain') }
|
||||
end
|
||||
|
||||
end
|
|
@ -12,29 +12,29 @@ describe 'SimpleDB.domain_metadata' do
|
|||
|
||||
it 'should return proper attributes when there are no items' do
|
||||
results = sdb.domain_metadata('domain_metadata')
|
||||
results[:attribute_name_count].should == 0
|
||||
results[:attribute_names_size_bytes].should == 0
|
||||
results[:attribute_value_count].should == 0
|
||||
results[:attribute_values_size_bytes].should == 0
|
||||
results[:box_usage].should be_a(Float)
|
||||
results[:item_count].should == 0
|
||||
results[:item_names_size_bytes].should == 0
|
||||
results[:request_id].should be_a(String)
|
||||
results[:timestamp].should be_a(String)
|
||||
results.body[:attribute_name_count].should == 0
|
||||
results.body[:attribute_names_size_bytes].should == 0
|
||||
results.body[:attribute_value_count].should == 0
|
||||
results.body[:attribute_values_size_bytes].should == 0
|
||||
results.body[:box_usage].should be_a(Float)
|
||||
results.body[:item_count].should == 0
|
||||
results.body[:item_names_size_bytes].should == 0
|
||||
results.body[:request_id].should be_a(String)
|
||||
results.body[:timestamp].should be_a(String)
|
||||
end
|
||||
|
||||
it 'should return proper attributes with items' do
|
||||
sdb.put_attributes('domain_metadata', 'foo', { :bar => :baz })
|
||||
results = sdb.domain_metadata('domain_metadata')
|
||||
results[:attribute_name_count].should == 1
|
||||
results[:attribute_names_size_bytes].should == 3
|
||||
results[:attribute_value_count].should == 1
|
||||
results[:attribute_values_size_bytes].should == 3
|
||||
results[:box_usage].should be_a(Float)
|
||||
results[:item_count].should == 1
|
||||
results[:item_names_size_bytes].should == 3
|
||||
results[:request_id].should be_a(String)
|
||||
results[:timestamp].should be_a(String)
|
||||
results.body[:attribute_name_count].should == 1
|
||||
results.body[:attribute_names_size_bytes].should == 3
|
||||
results.body[:attribute_value_count].should == 1
|
||||
results.body[:attribute_values_size_bytes].should == 3
|
||||
results.body[:box_usage].should be_a(Float)
|
||||
results.body[:item_count].should == 1
|
||||
results.body[:item_names_size_bytes].should == 3
|
||||
results.body[:request_id].should be_a(String)
|
||||
results.body[:timestamp].should be_a(String)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -11,12 +11,12 @@ describe 'SimpleDB.get_attributes' do
|
|||
end
|
||||
|
||||
it 'should have no attributes for foo before put_attributes' do
|
||||
lambda { sdb.get_attributes('get_attributes', 'foo') }.should eventually { |expected| expected[:attributes].should be_empty }
|
||||
lambda { sdb.get_attributes('get_attributes', 'foo') }.should eventually { |expected| expected.body[:attributes].should be_empty }
|
||||
end
|
||||
|
||||
it 'should have attributes for foo after put_attributes' do
|
||||
sdb.put_attributes('get_attributes', 'foo', { :bar => :baz })
|
||||
lambda { sdb.get_attributes('get_attributes', 'foo') }.should eventually { |expected| expected[:attributes].should == { 'bar' => ['baz'] } }
|
||||
lambda { sdb.get_attributes('get_attributes', 'foo') }.should eventually { |expected| expected.body[:attributes].should == { 'bar' => ['baz'] } }
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -12,13 +12,13 @@ describe 'SimpleDB.list_domains' do
|
|||
|
||||
it 'should return proper attributes' do
|
||||
results = sdb.list_domains
|
||||
results[:box_usage].should be_a(Float)
|
||||
results[:domains].should be_an(Array)
|
||||
results[:request_id].should be_a(String)
|
||||
results.body[:box_usage].should be_a(Float)
|
||||
results.body[:domains].should be_an(Array)
|
||||
results.body[:request_id].should be_a(String)
|
||||
end
|
||||
|
||||
it 'should include list_domains in list_domains' do
|
||||
lambda { sdb.list_domains }.should eventually { |expected| expected[:domains].should include('list_domains') }
|
||||
lambda { sdb.list_domains }.should eventually { |expected| expected.body[:domains].should include('list_domains') }
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -11,17 +11,17 @@ describe 'SimpleDB.put_attributes' do
|
|||
end
|
||||
|
||||
it 'should have no attributes for foo before put_attributes' do
|
||||
lambda { sdb.get_attributes('put_attributes', 'foo') }.should eventually { |expected| expected[:attributes].should be_empty }
|
||||
lambda { sdb.get_attributes('put_attributes', 'foo') }.should eventually { |expected| expected.body[:attributes].should be_empty }
|
||||
end
|
||||
|
||||
it 'should return proper attributes from put_attributes' do
|
||||
actual = sdb.put_attributes('put_attributes', 'foo', { 'bar' => 'baz' })
|
||||
actual[:request_id].should be_a(String)
|
||||
actual[:box_usage].should be_a(Float)
|
||||
actual.body[:request_id].should be_a(String)
|
||||
actual.body[:box_usage].should be_a(Float)
|
||||
end
|
||||
|
||||
it 'should have attributes for foo after put_attributes' do
|
||||
lambda { sdb.get_attributes('put_attributes', 'foo') }.should eventually { |expected| expected[:attributes].should == { 'bar' => ['baz'] } }
|
||||
lambda { sdb.get_attributes('put_attributes', 'foo') }.should eventually { |expected| expected.body[:attributes].should == { 'bar' => ['baz'] } }
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue